C++ 具有友元函数的模板化类的链接器错误
我正在尝试用正向列表重新创建堆栈。但是,我使用友元函数来重载+和堆栈类中的模板友元声明不太正确。您需要这样声明:C++ 具有友元函数的模板化类的链接器错误,c++,visual-studio-2012,c++11,friend,C++,Visual Studio 2012,C++11,Friend,我正在尝试用正向列表重新创建堆栈。但是,我使用友元函数来重载+和堆栈类中的模板友元声明不太正确。您需要这样声明: template<class T> friend Stack<T> operator+(const Stack<T> &a, const Stack<T> &b); template<class T> friend std::ostream &operator<<(std::ostrea
template<class T>
friend Stack<T> operator+(const Stack<T> &a, const Stack<T> &b);
template<class T>
friend std::ostream &operator<<(std::ostream &output, Stack<T> &s);
模板
友元堆栈运算符+(常量堆栈&a、常量堆栈&b);
模板
friend std::ostream&operator您在哪里为堆栈类实现了操作符+
?在一些.CPP文件中?您想使用friends有什么特别的原因吗?@Ajay no它是在头文件中实现的,在头文件中//实现替代项在上面的代码中。@HolgerKretzschmar
int main(){
Stack<int> st;
st.push(4);
Stack<int> st2;
st2.push(8);
cout<<st + st2<<endl;
return 0;
}
error LNK2019: unresolved external symbol "class Stack<int> __cdecl operator+(class Stack<int> const &,class Stack<int> const &)" (??H@YA?AV?$Stack@H@@ABV0@0@Z) referenced in function _main
error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Stack<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Stack@H@@@Z) referenced in function _main
template<class T>
friend Stack<T> operator+(const Stack<T> &a, const Stack<T> &b);
template<class T>
friend std::ostream &operator<<(std::ostream &output, Stack<T> &s);