Thread: Where am I going wrong?

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    99

    Where am I going wrong?

    Code:
    #include<string>#include<iostream>
    using namespace std;
    
    
    class Dog
    {
    	
    	void setValues( int, int, string);
    	
    	public:
    	void bark(){cout << "WOOF!" << endl;}
    	void setAge(int yrs) {age = yrs;}
    	void setWeight ( int ibs ) { weight = ibs ; }
    	void setColor (string hue ) { color = hue ; }
    	
    	int getAge() { return age ; }
    	int getWeight() { return weight ; }
    	string getColor() { return color ; }
    	
    };
    void Dog::setValues ( int age, int weight, string color)
    {
    	this->age = age;
    	this->weight = weight;
    	this->color = color;
    }
    
    
    int main()
    {
    		
    	Dog fido;
    	fido.setValues( 3, 15, "brown");
    	
    	cout << "Fido is a " << fido.getColor() << " dog " << endl;
    	cout << "Fido is " << fido.getAge() << " years old."  << endl;
    	cout << "Fido weighs  " << fido.getWeight() << " pounds " << endl;
    	
    	fido.bark();
    	
    	Dog pooch;
    	pooch.setValues( 4, 18, "gray" );
    	
    	cout << "Pooch is a " << pooch.getAge();
    	cout << " year old " << pooch.getColor();
    	cout << " dog who weighs " << pooch.getWeight();
    	cout << " pounds. ";
    	
    	pooch.bark();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Code:
    main.cpp: In member function ‘void Dog::setAge(int)’:
    main.cpp:12:24: error: ‘age’ was not declared in this scope
       12 |  void setAge(int yrs) {age = yrs;}
          |                        ^~~
    main.cpp: In member function ‘void Dog::setWeight(int)’:
    main.cpp:13:31: error: ‘weight’ was not declared in this scope
       13 |  void setWeight ( int ibs ) { weight = ibs ; }
          |                               ^~~~~~
    main.cpp: In member function ‘void Dog::setColor(std::string)’:
    main.cpp:14:32: error: ‘color’ was not declared in this scope
       14 |  void setColor (string hue ) { color = hue ; }
          |                                ^~~~~
    main.cpp: In member function ‘int Dog::getAge()’:
    main.cpp:16:24: error: ‘age’ was not declared in this scope
       16 |  int getAge() { return age ; }
          |                        ^~~
    main.cpp: In member function ‘int Dog::getWeight()’:
    main.cpp:17:27: error: ‘weight’ was not declared in this scope
       17 |  int getWeight() { return weight ; }
          |                           ^~~~~~
    main.cpp: In member function ‘std::string Dog::getColor()’:
    main.cpp:18:29: error: ‘color’ was not declared in this scope
       18 |  string getColor() { return color ; }
          |                             ^~~~~
    main.cpp: In member function ‘void Dog::setValues(int, int, std::string)’:
    main.cpp:23:8: error: ‘class Dog’ has no member named ‘age’
       23 |  this->age = age;
          |        ^~~
    main.cpp:24:8: error: ‘class Dog’ has no member named ‘weight’
       24 |  this->weight = weight;
          |        ^~~~~~
    main.cpp:25:8: error: ‘class Dog’ has no member named ‘color’
       25 |  this->color = color;
          |        ^~~~~
    main.cpp: In function ‘int main()’:
    main.cpp:32:32: error: ‘void Dog::setValues(int, int, std::string)’ is private within this context
       32 |  fido.setValues( 3, 15, "brown");
          |                                ^
    main.cpp:21:6: note: declared private here
       21 | void Dog::setValues ( int age, int weight, string color)
          |      ^~~
    main.cpp:41:33: error: ‘void Dog::setValues(int, int, std::string)’ is private within this context
       41 |  pooch.setValues( 4, 18, "gray" );
          |                                 ^
    main.cpp:21:6: note: declared private here
       21 | void Dog::setValues ( int age, int weight, string color)       |      ^~~

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    You have a bunch of member functions, but no member variables.

    > main.cpp:23:8: error: ‘class Dog’ has no member named ‘age’
    > 23 | this->age = age;
    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.

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by Salem View Post
    You have a bunch of member functions, but no member variables.

    > main.cpp:23:8: error: ‘class Dog’ has no member named ‘age’
    > 23 | this->age = age;
    But the book showed the code in that form. The member variables go into the main block or the class block?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    Maybe you need a better book.
    Code:
    class Dog
    {
        // Member Vars
        int age;
        int weight;
        string color;
    
        // Member functions     
        void setValues( int, int, string);
         
        public:
        void bark(){cout << "WOOF!" << endl;}
        void setAge(int yrs) {age = yrs;}
        void setWeight ( int ibs ) { weight = ibs ; }
        void setColor (string hue ) { color = hue ; }
         
        int getAge() { return age ; }
        int getWeight() { return weight ; }
        string getColor() { return color ; }
         
    };
    Maybe you mis-read also.
    The abbreviation for 'pounds' is 'lbs', not 'ibs'.
    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
    Dec 2019
    Posts
    99
    Quote Originally Posted by Salem View Post
    Maybe you need a better book.


    Maybe you mis-read also.
    The abbreviation for 'pounds' is 'lbs', not 'ibs'.
    Now you are just being smug and not helpful. I could use a new book, but still. It won't help if I didn't get something as simple as encapsulation and how to do it right.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    900
    Post a picture of the code from the book? Then we can see if the book is maybe worth learning from.

  8. #8
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by christop View Post
    Post a picture of the code from the book? Then we can see if the book is maybe worth learning from.
    I originally could compile it successfully, only when I added more did things go wrong...
    Attached Images Attached Images Where am I going wrong?-screenshot1-jpg Where am I going wrong?-screenshot-2-jpg Where am I going wrong?-screenshot-3-jpg 

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    > I originally could compile it successfully, only when I added more did things go wrong...
    Maybe you should show that as well.
    Because what you posted makes no mention of the need to have member variables.

    Bullet point 4 suggests you need them.
    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.

  10. #10
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by Salem View Post
    > I originally could compile it successfully, only when I added more did things go wrong...
    Maybe you should show that as well.
    Because what you posted makes no mention of the need to have member variables.

    Bullet point 4 suggests you need them.

    Code:
    #include<string>#include<iostream>
    using namespace std;
    
    
    class Dog
    {
        
        int age, weight;
        string color;
        
        public:
        void bark(){cout << "WOOF!" << endl;}
        void setAge(int yrs) {age = yrs;}
        void setWeight ( int ibs ) { weight = ibs ; }
        void setColor (string hue ) { color = hue ; }
        
        int getAge() { return age ; }
        int getWeight() { return weight ; }
        string getColor() { return color ; }
    };
    
    
    int main()
    {
        Dog fido;
        fido.setAge( 3 );
        fido.setWeight( 15 );
        fido.setColor( "brown");
        
        cout << "Fido is a " << fido.getColor() << " dog " << endl;
        cout << "Fido is " << fido.getAge() << " years old."  << endl;
        cout << "Fido weighs  " << fido.getWeight() << " pounds " << endl;
        
        fido.bark();
        
        return 0;
    }

    Where am I going wrong?-screenshot-201-jpg
    Attached Images Attached Images Where am I going wrong?-screenshot-202-jpg Where am I going wrong?-screenshot-2024-jpg Where am I going wrong?-screenshot-203-jpg 

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    Right, so in between the code in post #10 and the code in post #1, why did you delete the member variables?
    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.

  12. #12
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Quote Originally Posted by Salem View Post
    Right, so in between the code in post #10 and the code in post #1, why did you delete the member variables?
    It said to do it.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    Where?

    I saw no mention of telling you to delete or change anything to do with the member variables.

    Perhaps you need a better book (still).
    Just copying from a book which basically amounts to telling you what to type isn't teaching you how to program.
    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.

  14. #14
    Registered User
    Join Date
    Dec 2019
    Posts
    99
    Ok, can you suggest a book?

    Quote Originally Posted by Salem View Post
    Where?

    I saw no mention of telling you to delete or change anything to do with the member variables.

    Perhaps you need a better book (still).
    Just copying from a book which basically amounts to telling you what to type isn't teaching you how to program.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why so wrong numbers? As I write so wrong?
    By Dmy in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2017, 02:10 PM
  2. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

Tags for this Thread