出错信息:
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.怎么解决?谢谢。

解决方案 »

  1.   

    少了头文件. 在stdafx.h里加:
    #include <winsock.h>
      

  2.   

    谢谢,能解释下为什么吗?=====
    呵呵,这些结构体和函数声明在 winsock.h 文件啊,你要用它,当然就要包含这个文件啦
      

  3.   

    missing ';' before identifier 'wsaData'
    是因为编译器不能认识 WSADATA 结构
    而 WSADATA 结构定义在 WinSock.h 及 WinSock2.h 中
    因此你使用 
    #include <winsock.h>
    #pragma comment(lib, "winsock.lib")

    #include <winsock2.h>
    #pragma comment(lib, "ws2_32.lib")
    都是可以的
      

  4.   

    winsock 和 winsock2 的区别在于 winsock 的版本分别是 1.1 和 2.2
      

  5.   

    嗯,我终于明白了,
    过去在Console或Win32模式下,原来只要一个#include <windows.h>杂七杂八的头文件都包进去了,winsock2.h也被包进去了,自然就没问题。MFC的头文件分得比较开比如那个winsock.h应该被封到afxsock.h里了。这样移植不是挺麻烦,我在主程序里没有调用任何的关于socket的api,全是能过SocketFunction类完成的,但还是要在stdafx.h里包含winsock.h,一个winsock并不可怕,万一有千千万万个winsock出来那不是要麻烦死的?