#include<iostream> #include<string> class Pet { public: Pet(std::string thename); void FREE_TEST(); void FOOK_TEST(); void EAT_TEST(); protected: std::string name; }; class dog:public Pet { public: dog(std::string thename); void CLIMB(); }; class cat:public Pet { public: cat(std::string thename); void KIDDING_TEST(); }; Pet::Pet(std::string thename) { name = thename; } void Pet::FREE_TEST() { std::cout<<name <<"\n"<<"FREE TEST OK"<<std::endl; } void Pet::FOOK_TEST() { std::cout<<name <<"FOOK TEST OK"<<std::endl; } void Pet::EAT_TEST() { std::cout<<name <<"EAT TEST OK "<<std::endl; } dog::dog(std::string thename):Pet(thename) { } void dog::CLIMB() { std::cout<<"CLIIMB TEST OK"<<std::endl; } cat::cat(std::string thename):Pet(thename) { } void cat::KIDDING_TEST() { std::cout<<"KIDDING TEST OK "<<std::endl; } int main() { Pet *dog = new dog("123"); Pet *cat = new cat("546456"); //dog->CLIMB(); //dogFREE_TEST(); //dog.FOOK_TEST(); //dog.EAT_TEST(); //cat->KIDDING_TEST(); //cat.FREE_TEST(); //cat.EAT_TEST(); //cat.FOOK_TEST(); return 0; }
最后编译提示 dog和cat 不是一个type
本人愚笨,还请各位高手指点
解决方案
20
发成冲突了,你的变量名和构造函数的调用同名
40
要注意变量命名规则。
#include<iostream> #include<string> class Pet { public: Pet(std::string thename); void FREE_TEST(); void FOOK_TEST(); void EAT_TEST(); protected: std::string name; }; class dog:public Pet { public: dog(std::string thename); void CLIMB(); }; class cat:public Pet { public: cat(std::string thename); void KIDDING_TEST(); }; Pet::Pet(std::string thename) { name = thename; } void Pet::FREE_TEST() { std::cout<<name <<"\n"<<"FREE TEST OK"<<std::endl; } void Pet::FOOK_TEST() { std::cout<<name <<"FOOK TEST OK"<<std::endl; } void Pet::EAT_TEST() { std::cout<<name <<"EAT TEST OK "<<std::endl; } dog::dog(std::string thename):Pet(thename) { } void dog::CLIMB() { std::cout<<"CLIIMB TEST OK"<<std::endl; } cat::cat(std::string thename):Pet(thename) { } void cat::KIDDING_TEST() { std::cout<<"KIDDING TEST OK "<<std::endl; } int main() { Pet *d = new dog("123"); Pet *c = new cat("546456"); //dog->CLIMB(); //dogFREE_TEST(); //dog.FOOK_TEST(); //dog.EAT_TEST(); //cat->KIDDING_TEST(); //cat.FREE_TEST(); //cat.EAT_TEST(); //cat.FOOK_TEST(); return 0; }