Thread: class 'Unit' does not have any field named 'N' ???

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    119

    class 'Unit' does not have any field named 'N' ???

    Code:
    #include <iostream>
    #include <string>
    
    class Mn{
       private:
         double mn;
         double X;
         double Y;
       public:
         Mn(double Xx, double Yy) { X=Xx; Y=Yy; mn=X*Y; std::cout << "Mn\n";}
         double daj(){std::cout << "daj()\n"; return mn;}
    
    };
    
    
    
    
    
    class Unit 
    {
    protected:
           int health;
           std::string Nazwa;
    public:
           void showHealth() { std::cout << "Unit health: " << health << std::endl; };
           Unit(std::string N): health(10), N(Nazwa){ std::cout << "Unit(): health(10) " << std::endl;}       
           Unit(int a, std::string N): health(a), N(Nazwa) {std::cout << "Unit(int a): health(a) " << std::endl; } 
    };
    
    
    class Soldier : public Unit
    {
    private:
           int damage;
    public:
           void showDamage(){std::cout << "Sodier damage: " << damage << std::endl;}
           Soldier(std::string N): Unit(N),damage(20) {std::cout << "Soldier():damage(20) " << std::endl; }
           Soldier(int a, std::string N):Unit(N),damage(a) {std::cout << "Soldier(int a):damage(a) " << std::endl; } //также health=10
           Soldier(int a, int b, std::string N):Unit(a,N),  damage(b)  {std::cout << "Soldier(int a, int b):Unit(a),damage(b) " << std::endl; }
    };
    
    
    int main()
    {
        Mn mu(5,5);
        std::cout << mu.daj()<<"\n"<<"\n"<<"\n";
    
    
        Soldier hercules(30,"hercules");
           hercules.showHealth();
           hercules.showDamage();
        Soldier achilles(40,50,"achilles");
           achilles.showHealth();
           achilles.showDamage();
    
    return 0;
    }
    Last edited by Dmy; 03-30-2017 at 07:23 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Unit(std::string N): health(10), N(Nazwa)
    N is the parameter, and Nazwa is the member var.
    So
    Nazwa(N)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Thank you! Thank you very much!

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    How correctly? How to realize is it good or even beautifully?

    Code:
    #include <iostream>
    #include <string>
    
    class Mn{
       private:
         double mn;
         double X;
         double Y;
       public:
         Mn(double Xx, double Yy) { X=Xx; Y=Yy; mn=X*Y; std::cout << "Mn\n";}
         double daj(){std::cout << "daj()\n"; return mn;}
    };
    
    class Sum : public Mn
    {
        private:
         double sum;
        public:
         Sum(double Xx, double Yy) {sum=X+Y;}: Mn(Xx,Yy) { X=Xx; Y=Yy; mn=X*Y; std::cout << "Sum(): Mn\n";}
         double daj(){std::cout << "daj()\n"; return sum;}
    };
    error: 'Xx' was not declared in this scope ... .... ... ... ... .

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps if you formatted the code better, it would be obvious.
    Code:
    // what's this!? ---------vvvvvvvvvv
    Sum(double Xx, double Yy) {sum=X+Y;} : Mn(Xx,Yy) {
       X=Xx;
       Y=Yy;
       mn=X*Y;
       std::cout << "Sum(): Mn\n";
    }
    You can't have code, followed by a constructor list, followed by yet more code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Thank Salem!
    I know that I was asking nonsense.
    I need to read books, including the basics.
    Do not think me moron.
    Last edited by Dmy; 04-06-2017 at 07:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'struct class’ has no member named ‘teach’
    By justine in forum C Programming
    Replies: 5
    Last Post: 12-07-2012, 08:51 AM
  2. Unit Testing Abstract Class
    By mconflict in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2012, 04:47 PM
  3. Replies: 1
    Last Post: 10-19-2008, 12:10 PM
  4. ideas for a generalized unit-handling class
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 06-19-2008, 09:42 AM
  5. Class named Stack
    By cheeisme123 in forum C++ Programming
    Replies: 1
    Last Post: 05-18-2002, 01:08 PM

Tags for this Thread