类模板中的friend函数出错 [英] Error in friend function in class templates

查看:92
本文介绍了类模板中的friend函数出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>


template<class T>
class students
{
    friend void shows(students<T> &);

private:
    T name;
    void ShowInfo();

public:
        students(T x)
    {
        name=x;
    }
};

template<class T>
 void shows(students<T> &stu)
{
    cout<<stu.name;

}





int _tmain(int argc, _TCHAR* argv[])

{



    students <int>  mat(10);
    shows(mat);

    return 0;
}







我收到此错误:

错误1错误LNK2019:未解析的外部符号void __cdecl show(class students< int>&)(?显示@@ YAXAAV?$ students @ H @@@ Z)在函数_wmain




Im getting this error:
Error 1 error LNK2019: unresolved external symbol "void __cdecl shows(class students<int> &)" (?shows@@YAXAAV?$students@H@@@Z) referenced in function _wmain

推荐答案

students @ H @@@ Z
students@H@@@Z) referenced in function _wmain


我认为朋友不是问题,尽管模板朋友是错误的在某种程度上,在2012年之前的所有版本的Visual Studio中,据我所知。

问题似乎是编译器可能没有为show模板函数生成代码,因为它并不认为它被调用而是它正在寻找一个具有不同签名的功能,该功能尚未编写,试图从可用库中导入并失败。

I会尝试删除朋友声明并做这样的事情:



temp迟< T级>

无效显示(T&stu)

{

cout<< stu.name;

}



int _tmain(int arg,_TCHAR * argv [])

{

学生< int> mat(10);

显示<学生>(垫);



返回0;

}



这应该会给你一个错误,说你不能访问学生的私人成员。然后你可以根据它在错误信息中显示的功能签名重新输入朋友声明。



模板非常适合伸展大脑。祝你好运。
I don''t think the friend-ness is the issue here although template friends are buggy to some extent in all versions of Visual Studio before 2012 as far as I can tell.
The problem seems to be that the compiler is ''probably'' not generating code for the shows template function because it doesn''t think it''s being called instead it''s looking for a function with a different signature which has not been written, trying to import it from the available libraries and failing.
I would try removing the friend declaration and doing something like this:

template< class T >
void shows( T& stu )
{
cout << stu.name;
}

int _tmain( int arg, _TCHAR* argv[] )
{
students< int > mat(10);
shows< students >( mat );

return 0;
}

This should then give you the error saying you can''t access a private member of students. Then you can put the friend declaration back in based on the function signature it shows you in the error message.

templates are great for stretching the brain. good luck.


一个有趣的问题:C ++和友谊......



这是完整的代码:



An interesting question: C++ and friendship...

Here is the complete code:

#include "stdafx.h"
#include <iostream>
using namespace std;

template<class T>
class students
{
public:
    friend void shows(T&);
    T name;
    void ShowInfo();
public:
    students(T x)
    {
        name=x;
    }
};

template<class T>
void shows(students<T> &stu)
{
    cout << stu.name << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{

    students<int> mat(10);
    shows(mat);

    return 0;
}





这也是造成问题的私密性。



祝你好运



It was also the privateness which made problems.

Best regards


这篇关于类模板中的friend函数出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆