I have defined a class Cat which has a member function void Cat::miao() in Cat.h file. Then , I implement this function in Cat.cpp as the following code.
However, while compiling and linking, I got an error , say , "undefined reference to `Cat::miao()" .What wrong with the code?
My compiler is GNU c11.
-----Cat.h
#include<iostream>
#include<string>
using namespace std;
class Cat
{
string name;
public:
Cat(const string&n):name(n){};
void miao();
};
-----Cat.cpp
#include"Cat.h"
void Cat::miao()
{
cout << name << endl;
}
-----main.cpp
#include"Cat.h"
int main()
{
Cat tom("tom");
tom.miao();
return 1;
}
C:\Users****:K.o:main.cpp:(.text+0x69): undefined reference to `Cat:: miao()' collect2.exe: error: ld returned 1 exit status
Aucun commentaire:
Enregistrer un commentaire