出错信息:
Compiling...
StdAfx.cpp
c:\vc\socketfunction.h(23) : error C2146: syntax error : missing ';' before identifier 'wsaData'
c:\vc\socketfunction.h(23) : error C2501: 'WSADATA' : missing storage-class or type specifiers
c:\vc\socketfunction.h(23) : error C2501: 'wsaData' : missing storage-class or type specifiers
c:\vc\socketfunction.h(24) : error C2146: syntax error : missing ';' before identifier 'the_s'
c:\vc\socketfunction.h(24) : error C2501: 'SOCKET' : missing storage-class or type specifiers
c:\vc\socketfunction.h(24) : error C2501: 'the_s' : missing storage-class or type specifiers
c:\vc\socketfunction.h(29) : error C2146: syntax error : missing ';' before identifier 'connect'
c:\vc\socketfunction.h(29) : error C2501: 'SOCKET' : missing storage-class or type specifiers
c:\vc\socketfunction.h(53) : error C2146: syntax error : missing ';' before identifier 'connect'
c:\vc\socketfunction.h(53) : error C2501: 'SOCKET' : missing storage-class or type specifiers
c:\vc\socketfunction.h(80) : warning C4183: 'connect': member function definition looks like a ctor, but name does not match enclosing class
Error executing cl.exe.怎么解决?谢谢。
Compiling...
StdAfx.cpp
c:\vc\socketfunction.h(23) : error C2146: syntax error : missing ';' before identifier 'wsaData'
c:\vc\socketfunction.h(23) : error C2501: 'WSADATA' : missing storage-class or type specifiers
c:\vc\socketfunction.h(23) : error C2501: 'wsaData' : missing storage-class or type specifiers
c:\vc\socketfunction.h(24) : error C2146: syntax error : missing ';' before identifier 'the_s'
c:\vc\socketfunction.h(24) : error C2501: 'SOCKET' : missing storage-class or type specifiers
c:\vc\socketfunction.h(24) : error C2501: 'the_s' : missing storage-class or type specifiers
c:\vc\socketfunction.h(29) : error C2146: syntax error : missing ';' before identifier 'connect'
c:\vc\socketfunction.h(29) : error C2501: 'SOCKET' : missing storage-class or type specifiers
c:\vc\socketfunction.h(53) : error C2146: syntax error : missing ';' before identifier 'connect'
c:\vc\socketfunction.h(53) : error C2501: 'SOCKET' : missing storage-class or type specifiers
c:\vc\socketfunction.h(80) : warning C4183: 'connect': member function definition looks like a ctor, but name does not match enclosing class
Error executing cl.exe.怎么解决?谢谢。
#include <winsock.h>
呵呵,这些结构体和函数声明在 winsock.h 文件啊,你要用它,当然就要包含这个文件啦
是因为编译器不能认识 WSADATA 结构
而 WSADATA 结构定义在 WinSock.h 及 WinSock2.h 中
因此你使用
#include <winsock.h>
#pragma comment(lib, "winsock.lib")
或
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
都是可以的
过去在Console或Win32模式下,原来只要一个#include <windows.h>杂七杂八的头文件都包进去了,winsock2.h也被包进去了,自然就没问题。MFC的头文件分得比较开比如那个winsock.h应该被封到afxsock.h里了。这样移植不是挺麻烦,我在主程序里没有调用任何的关于socket的api,全是能过SocketFunction类完成的,但还是要在stdafx.h里包含winsock.h,一个winsock并不可怕,万一有千千万万个winsock出来那不是要麻烦死的?