Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有友元函数的模板化类的链接器错误_C++_Visual Studio 2012_C++11_Friend - Fatal编程技术网

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);