问题描述
这是"server.cpp"文件中的测试列表"功能.
this is the "testlist"function which is in "server.cpp"file.
#include "stdafx.h"
#include "WinSock2Server.h"
//YLN added 2010-09-25
#include <iostream>
#include <string>
using namespace std;
#include "router.h"
// 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";
instructions ();
int choice;
T value;
do
{
cout << "?";
cin >> choice;
switch (choice)
{
case 1:
cout << "enter" << typeName <<":";
cin >> value;
listObject.Register(value);
listObject.print();
break;
case 2:
cout << "enter" << typeName <<":";
cin >> value;
listObject.sendRegistration(value);
listObject.print();
break;
case 3:
if (listObject.sendRegistrationCancellation(value))
cout << value <<" removed from list\n";
listObject.print();
break;
case 4:
if (listObject.sendCancellationConfirmation(value))
cout << value <<" removed from list\n";
listObject.print();
break;
}
} while (choice != 5);
cout << "End list test \n\n";
}
void instructions ()
{
cout << "enter one of the following:\n"
<< " 1 to register node to the list\n"
<< " 2 to sendRegistration to the node \n"
<<" 3 to sendRegistrationCancellation to the list\n"
<<"4 to end list processing \n";
}
//Ended
int _tmain(int argc, _TCHAR* argv[])
{
Router <int > integerRouter;
testList ( integerRouter, "integer");
Router < double > doubleRouter;
testList ( doubleRouter, "double");
WinSock2Server *srv = new WinSock2Server();
srv->STEP1_InitializeWinsock();
srv->STEP2_CreateAndBindSocket();
srv->STEP3_ListenOnSocket();
srv->STEP4_AcceptingConnection();
srv->STEP6_Disconnect();
//
return 0;
}
错误如下:
Got errors as:
1>c:\documents and settings\lina\desktop\winsock2-basicclientserver1\winsock2-basicclientserver\winsock2server\server\winsock2server.cpp(134) : error C3861: 'testList': identifier not found
1>c:\documents and settings\lina\desktop\winsock2-basicclientserver1\winsock2-basicclientserver\winsock2server\server\winsock2server.cpp(137) : error C3861: 'testList': identifier not found
根据显示的错误,编译器无法识别它,但我不知道如何识别它.
According to the errors displayed, the compiler does not recognise it, but i donot know how to make it recognise.
推荐答案
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;
}
testList<int>(integerRouter, "integer");
testList<double>(doubleRouter, "double");
这是服务器"中的测试列表"功能. 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.
这篇关于继续:从套接字编程中调用C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!