C++類外定義
#include?<stdlib.h> #include?<iostream> #include<string> using?namespace?std; class?zuobaio?{ public?: int?x; int?y; void?printfx() { cout<<x<<endl; } void?printfy?() { cout?<<y<<endl; } }; class?Student??{ public?: void?ste_strname?(string?_name?) { m_strName=_name; } string?get_name?() { return??m_strName; } void?set_strsex(string?_sex) { m_strSex=_sex; } string?getsex() { return?m_strSex;?? } void?initScore() { i_Score=0; } int?setScore(int?score) { i_Score=score; } int?Study(int?k) { i_Score+=k; return?0; } int?getscore() { return?i_Score; } private: string?m_strName?; string?m_strSex; int?i_Score; }; class?Teacher{ public: void?setm_Name(string?_name); string?getm_Name(); void?set_Sex(string?_sex); string?get_Sex(); string?get_Name(); void??setm_Age(int?age); int?getm_Age(); void?teahe(); private: string?m_name; string?m_sex; int?m_age; }; void?Teacher::setm_Name(string?_name) { m_name?=?_name; } string?Teacher::getm_Name(){ return?m_name; } void??Teacher::setm_Age(int?age) { m_age=age; } int?Teacher::getm_Age(){ return?m_age; } void?Teacher::teahe() { cout?<<"現在上課了"<<endl; } void?Teacher::set_Sex(string?_sex) { m_sex=_sex; } string?Teacher::get_Sex() { return?m_sex; } int?main(void) { zuobaio?zuobai; zuobai.x=50; zuobai.y=100; zuobai.printfx(); zuobai.printfy(); zuobaio?*p?=?new?zuobaio(); if(p==NULL) return?0; p->x=100; p->y=200; p->printfx(); p->printfy(); delete?p; p=NULL; string?name; cout?<<"請輸入您的名字"<<endl; getline(cin?,name?); if(name.empty()){ cout<<"您輸入的字符串為空!"<<endl; } if(name=="imooc") cout?<<"您是管理員"<<endl; cout<<"hello?"??+name<<?endl; cout?<<name.size()<<endl; cout?<<?name?[0]<<?endl?; Student?*stu=?new?Student(); cout?<<"請輸入學生姓名"<<endl; stu->ste_strname("zhangsan?"); stu->set_strsex("女"); stu->initScore(); stu->Study(5); stu->Study(3); cout?<<stu->get_name()<<endl; cout?<<stu->getsex()<<endl; cout?<<?stu->getscore()<<endl; delete?stu; stu=NULL; Teacher?t; t.setm_Name("孔子"); t.set_Sex("男"); t.setm_Age(30); t.get_Name(); t.get_Sex(); t.getm_Age(); t.teahe(); system("pause"); return?0; }
編譯沒有錯鏈接出錯,求指導??!
2015-05-25
?t.get_Name(); ?get_Name()這個方法在teacher類中沒有實現
2015-10-26
不是這個原因吧,我試了一下代碼,是你頭文件的問題