我有一些錯誤代碼的代碼我不明白如何解決。我問過我的教授和助教,除了更準確地理解錯誤的含義外,沒有運氣就諮詢了互聯網。從我所知道的來看,編譯器會讓我的重載操作符與內置操作符混淆,或者它根本不認爲它是一個重載操作符。關於C++模板上的重載操作符的一些編譯錯誤
我收到以下錯誤和警告:
|| ===項目4項,調試=== | \ project 4 \ forest.h | 13 | warning:friend declaration'Forest & operator +(Forest &,Forest &)'聲明瞭非模板函數|注意:(如果這不是你想要的,請確保函數模板已經被聲明,並且在函數名稱後面加上<>)|
\ project 4 \ forest.h | 13 |
\項目4 \ forest.h | 14 |警告:朋友宣言 '的std :: ostream的&操作< <(STD :: ostream的&,常量森林&)' 聲明一個非模板函數|
\項目4 \ forest.h | 15 |警告:朋友宣言 '的std :: istream的&操作>>(的std :: istream的&,森林&)' 聲明一個非模板函數|
\ project 4 \ main.cpp ||在函數'int main()'中: \ project 4 \ main.cpp | 21 | error:'文件>> intForest'中'操作符'的模糊重載|
c:\ program files(x86)\ codeblocks \ mingw \ bin .. \ lib \ gcc \ mingw32 \ 4.4.1 \ include \ C++ \ istream | 119 |注意:考生是:std :: basic_istream < _CharT ,_Traits> &的std :: basic_istream < _CharT,_Traits> ::運算>>(標準:: basic_istream < _CharT,_Traits> &(*)(標準:: basic_istream < _CharT,_Traits> &))[用_CharT =炭,_Traits = std :: char_traits] |
c:\ program files(x86)\ codeblocks \ mingw \ bin .. \ lib \ gcc \ mingw32 \ 4.4.1 \ include \ C++ \ istream | 123 | note:std :: basic_istream < _CharT,_Traits> &的std :: basic_istream < _CharT,_Traits> ::運算>>(標準:: basic_ios < _CharT,_Traits> &(*)(標準:: basic_ios < _CharT,_Traits> &))[用_CharT =炭,_Traits = std :: char_traits] |
c:\ program files(x86)\ codeblocks \ mingw \ bin .. \ lib \ gcc \ mingw32 \ 4.4.1 \ include \ C++ \ istream | 130 | note:std :: basic_istream < _CharT,_Traits> & std :: basic_istream < _CharT,_Traits> :: operator >>(std :: ios_base &(*)(std :: ios_base &))[with _CharT = char,_Traits = std :: char_traits] | || ===構建完成:1個錯誤,3個警告=== |
當我嘗試編譯我的代碼。相關的代碼段如下:
(在forest.h)
template< typename NODETYPE > class Forest
{
public:
friend Forest<NODETYPE>& operator+(Forest<NODETYPE>&, Forest<NODETYPE>&);
friend ostream& operator<<(ostream&, const Forest<NODETYPE>&);
friend istream& operator>>(istream&, Forest<NODETYPE>&);
Forest();
Forest(const Forest& otherForest);
~Forest();
void nodes(int&) const;
private:
ForestNode<NODETYPE> *root;
ForestNode<NODETYPE> *getNewNode(const NODETYPE &);
};
(在森林
。CPP)template<typename NODETYPE> istream& operator>>(istream& file, const Forest<NODETYPE>& f1)
{
istream file2 = file;
int nodeCount = 0;
string blah = ' ';
while(getline(file2,blah))
{
nodeCount++;
}
ForestNode<NODETYPE> *forestNodeArray[nodeCount];//holds pointers to last node of depth i
getline(file,*f1.root.tag);
forestNodeArray[0] = &(*f1.root);
inputHelper(file, 0, *f1.root, forestNodeArray, nodeCount);
return file;
}
(終於,在main.cpp中)
int main()
{
Forest <char> intForest();
filebuf fb;
fb.open ("forest1.txt",ios::in);
istream file(&fb);
cout << typeid(intForest).name() << endl;
cout << typeid(file).name() << endl;
file >> intForest;
fb.close();
}
任何幫助將不勝感激。
編輯:感謝亞歷克斯和阿爾夫,我明白他們爲什麼不被視爲模板功能。回想起來很明顯,我只是設置了這些簽名。無論如何,我仍然得到關於模糊運算符的錯誤。爲什麼編譯器不能識別我的操作符並使用它,而不是試圖在操作符的3個內置版本之間做出決定,這些操作符不可能有一個參數爲Forest?
嘗試'模板<>朋友森林與運營商+' –
2010-10-28 05:28:13
@alex啊。這解決了警告。謝謝。我有點希望聲明保持原來的樣子,這是由於我的教授給出的項目指南中指定的函數簽名,但我開始越來越關注遵守這些函數簽名和更多關於有工作代碼。再次感謝。 – joedillian 2010-10-28 05:36:28
好吧,如果你仔細想想,朋友函數的簽名取決於你的模板參數'NODETYPE',所以它們也必須是模板。沒有辦法繞過它。 – 2010-10-28 16:32:29