作者wintercobra (wintercobra)
看板EE_DSnP
標題[問題] Homework8第二題的問題
時間Sat Dec 20 20:17:14 2008
我是把程式碼切成三份
main一份
class的宣告一份class.h
member function的定義一份implement.h
class Students裡面有一個setChoices()的function
//in class.h
class Students
{
...
void setChoices();
...
};
//in implement.h
void Students::setChoices()
{
...
}
編譯的時候不過,原因是cannot declare member function'Students::setChoices'
within 'MatchMgr'
其他Students的member function 也有同樣的情形
請問是什麼原因呢?
雖然在class MatchMgr裡面有Students的物件
//in class.h
class MatchMgr
{
...
private:
Students _students[_numstudents];
...
};
但是我覺得那應該沒什麼影響吧
(另一個問題)
MatchMgr也出了點問題
//in class.h
class MatchMgr
{
...
void setChoices();
...
};
//in implement.h
void MatchMgr::setChoices()
{
...
}
編譯不過原因是: 'void MatchMgr::setChoices()' and 'void MatchMgr::setChoices()'
cannot be overloaded
他是在說class MatchMgr裡面的prototype 和我另外在implement.h定義的完整內容會
互相overload嗎?
應該不會吧,class 裡面不是可以只定義prototype的嗎?
請救救我,感激不盡~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.211.52.242
1F:推 ggegge:慣例: implement應該是.cpp 不過重點是cpp中要include .h 12/20 21:54
2F:推 ric2k1:嗯, .h 檔不會被 compile 哦! 12/20 21:58
3F:推 ric2k1:.h 檔請用來 include, 但是不要 include .cpp 檔 12/20 21:59
4F:→ wintercobra:所以做implement的時候做成.cpp檔,然後編譯implement 12/20 22:19
5F:→ wintercobra:後,在main裡面include "implement.h"嗎? 12/20 22:19
6F:推 timrau:#include "class.h". Linker會負責找到你在哪裡定義這些 12/20 22:32
7F:→ timrau:function,所以只要include prototype就好 12/20 22:32