[英]Some compiler errors concerning an overloaded operator on a template in c++
我有一些帶有一些錯誤的代碼,我根本不明白如何修復。 我問過我的教授和助教,除了更准確地理解錯誤的含義之外,還沒有運氣咨詢互聯網。 據我所知,編譯器要么將我的重載運算符與內置運算符混淆,要么根本不將其識別為重載運算符。
我收到以下錯誤和警告:
||=== project 4, Debug ===|
\project 4\forest.h|13|warning: friend declaration 'Forest<NODETYPE>& operator+(Forest<NODETYPE>&, Forest<NODETYPE>&)' declares a non-template function|
\project 4\forest.h|13|note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) |
\project 4\forest.h|14|warning: friend declaration 'std::ostream& operator<<(std::ostream&, const Forest<NODETYPE>&)' declares a non-template function|
\project 4\forest.h|15|warning: friend declaration 'std::istream& operator>>(std::istream&, Forest<NODETYPE>&)' declares a non-template function|
\project 4\main.cpp||In function 'int main()':|
\project 4\main.cpp|21|error: ambiguous overload for 'operator>>' in 'file >> intForest'|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|119|note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>|
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>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>|
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<char>] <near match>|
||=== Build finished: 1 errors, 3 warnings ===|
當我嘗試編譯我的代碼時。 相關代碼段如下:
(在森林.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 &);
};
(在forest.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();
}
任何幫助將不勝感激。
編輯:感謝 alex 和 alf,我理解為什么它們不被視為模板函數。 回想起來很明顯,我只是在那些簽名上。 無論如何,我仍然收到有關模糊運算符的錯誤。 為什么編譯器無法識別我的運算符並使用它,而不是嘗試在 3 個不可能將一個參數作為 Forest 參數的運算符的內置版本之間做出決定?
第二個錯誤在這一行:
Forest < char > intForest();
起初這可能令人驚訝,但該行並未聲明Forest<char>
類型的變量,而是聲明了一個不接受任何參數並返回Forest<char>
的函數。 只需從聲明中刪除括號:
Forest < char > intForest;
在第一個警告中,已經解釋過(聲明為朋友的函數不是模板,這意味着您必須為用於實例化Forest<>
每種類型手動實現它(可能您不希望那樣). 另請注意,聲明模板化operator+
然后使模板成為朋友,如@Alf P. Steinbach 回答意味着Forest<int>
將成為Forest<double>
的朋友,這可能不是您需要的。評論中的@Alex 提議只會使模板的特定實例化成為朋友,這可能更接近您想要的,但是您需要在模板類之前聲明模板化運算符,為此您需要轉發聲明模板類...
模板中無友元函數的一個常見模式是就地定義函數:
template <typename T>
class Forest {
// ...
friend Forest& operator+( Forest const & lhs, Forest const & rhs ) [1]
{
// implementation here
}
}
// [1] Note that the arguments are const references (read only), and also note that
// you do not need to provide the type argument inside the template declaration
這允許您將其定義為非模板化函數,同時讓編譯器為您實例化該函數。 此外,在處理模板時,在類定義中內聯定義成員方法通常更簡單。 它使生活更簡單,畢竟在大多數情況下,您確實需要在(相同)頭文件中提供實現。
然而,在定義二元運算符時,更好的方法是將operator+=
定義為成員方法,然后您可以輕松地將operator+
定義為非友元函數。 模式是:
struct test {
test& operator+=( test const & );
};
test operator+( test lhs, test const & rhs ) { // [2]
lhs += rhs;
return lhs;
}
// [2] Note that the first argument is by value, and the second by const reference
// This means that the compiler will copy the first argument for you, and you
// are free to modify it with the += operator and return the copy.
現在,最棘手的部分是混合前兩個建議。 為了能夠在模板定義中定義作為自由函數的operator+
,一個常見的技巧是使其友好,即使出於訪問原因不需要這樣做:
template <typename T>
class Forest {
Forest& operator+=( Forest const & ) {
// implemenation
}
friend Forest operator+( Forest lhs, Forest const & rhs ) { // [3]
return lhs+=rhs;
}
};
// [3] friendship only to allow us to define the free function inside the template
// declaration.
學習C++,我也遇到了同樣的問題,不過我換了個方法解決了!
template <class NODETYPE> class Forest;
template <class NODETYPE> Forest<NODETYPE>& operator+ (Forest<NODETYPE>&, Forest<NODETYPE>&);
template <class NODETYPE> ostream& operator<<(ostream&, const Forest<NODETYPE>&);
template <class NODETYPE> istream& operator>> (istream&, Forest<NODETYPE>&);
template<class NODETYPE>
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 &);
};
(第一個)警告告訴您,被好友化的函數不是模板。
而事實並非如此。
想想你將如何實施它。 它需要為每個可能的 NODE_TYPE 編寫一個這樣的函數。
有幾種方法可以解決這個問題。
一種方法是與函數模板成為朋友,如下所示:
template< class Type >
struct S
{
template< class U > friend void foo( S<U> );
};
template< class Type >
void foo( S< Type > x ) {}
int main()
{}
干杯 & hth.,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.