Help with classes

I'm learning how to use classes and everything was going pretty smooth up until the end when i call
 
myGoat.getName();
in main im getting an error that says undefined reference to 'Goat::getName() const', how can i fix this? thanks in advance.

Heres my full code so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;

class Goat {
public:

    Goat(string name, int age);
    Goat();
    void rest() const;
    void bleat() const;

    string getName() const;
    int getAge() const;


private:
    string name;
    int age;

};

Goat::Goat() {
    name = "Kid";
    age = 0;
}

void Goat::rest() const {
    cout << "zZzZzZz" << endl;
}
void Goat::bleat() const {
    cout << "Meeh-eh-eh!" << endl;
}

int main() {

    Goat myGoat;

    cout << "Meet my new goat";
    myGoat.getName();
    cout << "He plays with his mother, a 9 year old Goat named Nanny." << endl;
    cout << "My new goat Kid knows three things: " << endl;
    cout << "1. How to eat. Show them Kid: Chomp, burp!" << endl;
    cout << "2. How to bleat. Show them Kid: ";
    myGoat.bleat();
    cout << "3. How to rest. Show them Kid: ";
    myGoat.rest();




    return 0;
}
Where is your implementation of Goat::getName()?

You declare the function at line 12, but you never define the function.
how about implementation of Goat::getAge() ??? I think this one is missing too :)
however Goat::Goat() have age variable. hmmm :)
okay now after defining the function as you mentioned, i have it working properly :) thank you

1
2
3
void  Goat::getName() const {
    cout <<  "Kid";
}


now i just have another question, is there a way to assign the name inside of
1
2
3
4
Goat::Goat(){
name = "kid";
age = 0;
}

to the getName function instead of using cout to display "Kid"??
Hi Persades

I am learning classes as well so I might not answer your question in best way but I will try :)

I am not sure if I understand your question but what I think you are asking is if you can use getName() to assign name for the goat.

if this is your question then I think answer is yes, you can.
Do you want user to input name via cin >>?

you can have this as call from main() or from Goat()
yes you're understanding me question right but i am trying to do it without using any user input
hmmm then I don't understand your question ;)

what is the difference what you have now and what you want to get?
xxvms okay i got it , idk what i was thinking, basically i wanted it to print the goats name kid without using a literal cout cout << "Kid"; in line 34, so now I've just changed it to
1
2
3
void  Goat::getName() const {
    cout <<  name;
}


haha idk how that flew right over me , everything is working great now :D thanks for everyone's input!:)
Topic archived. No new replies allowed.