且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

继续:从套接字编程中调用C函数

更新时间:2022-04-11 08:59:29

仅作记录,以下缩写形式为您的代码编译成功:
Just for the record, the following shortened form of your code compiles successfully:
using namespace std;

template <typename T>
struct Router
{
    T rval;
};
// Ended
//YLN added 2010-09-25
template <typename T>
void testList(Router < T > &listObject, const string &typeName)
{
    cout <<" Testing a list of " << typeName << " values \n";
    cout << "End list test \n\n";
};
//Ended
int _tmain()
{
//  int integerRouter;
            Router <int > integerRouter;
            testList ( integerRouter, "integer");
            Router < double > doubleRouter;
            testList ( doubleRouter, "double");
    return 0;
}


检查以下各项是否有效-
Check if the following work -
testList<int>(integerRouter, "integer");
testList<double>(doubleRouter, "double");


linayang写道:
linayang wrote:

这是服务器"中的测试列表"功能. cpp文件.

this is the "testlist"function which is in "server.cpp"file.



为什么它没有包含在包含文件中?除非您使用的是基于EDG的编译器,否则您将无法从源文件中导出模板定义,而只能将原型包含在某个地方.无论调用什么测试列表,都需要查看它的完整定义-在您的情况下,需要通过包含或批量剪切和粘贴将其引入winsock2server.cpp中.

干杯,



PS:实际上,当我在谈论这个话题时...

路由器定义也必须对您正在其中编译testList的任何对象可见.



Why isn''t it in an include file? Unless you''re using a compiler based on EDG you can''t export a template definition from a source file and just include the prototype somewhere. Whatever calls testlist needs to see it''s full definition - in your case it needs to be introduced into winsock2server.cpp, either by including it or wholesale cut-and-paste.

Cheers,

Ash

PS: Actually, while I''m on the subject...

The router definition needs to be visible to whatever you''re compiling testList in as well.