简体   繁体   English

在类声明外定义函数体未编译

[英]Defining function body outside class declaration is not compiling

I was trying to define function that was declared in class (in header .h file). 我试图定义在类中声明的函数(在标头.h文件中)。 But there are still compiling problems with this separated definition. 但是,使用这种单独的定义仍然存在编译问题。 No matter if it is in these same file with declaration or in separated .cpp it still return these same error 无论是在带有声明的这些相同文件中还是在单独的.cpp中,它仍会返回这些相同的错误

||=== Build: Debug in Server (compiler: GNU GCC Compiler) ===| || ===构建:在服务器中调试(编译器:GNU GCC编译器)=== |

obj\\Debug\\SocketManager.o||In function obj \\ Debug \\ SocketManager.o ||在函数中

`ZN13SocketManager12setUpWINSOCKEv':| `ZN13SocketManager12setUpWINSOCKEv“:|

J:\\CodeBlocksProjects\\Server\\SocketManager.h|18|undefined reference to J:\\ CodeBlocksProjects \\ Server \\ SocketManager.h | 18 |未定义的引用

`WSAStartup@8'| `调用WSAStartup @ 8' | ||error: ld returned 1 exit status| ||错误:ld返回1个退出状态| ||=== Build || ===构建

failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 失败:2个错误,0个警告(0分钟,0秒)=== |

#include <iostream>
#include <winsock2.h>

using namespace std;

class SocketManager
{
private:
   WSADATA wsaData;
public:
   void setUpWINSOCK();
   SocketManager(int = 27015, string = "127.0.0.1");
};

void SocketManager::setUpWINSOCK()
{
int result = WSAStartup( MAKEWORD( 2, 2 ), & wsaData );
if( result != NO_ERROR )
   cout<<"Initialization error.\n"<<endl;
}

With small change its compilling propetly, but i would like to make it work with separated definition in diffrent .cpp file 进行较小的更改即可正确编译,但我想使它与diffrent .cpp文件中的单独定义一起使用

#include <iostream>
#include <winsock2.h>

using namespace std;
class SocketManager
{
private:
   WSADATA wsaData;
public:
   void setUpWINSOCK()
   {
      int result = WSAStartup( MAKEWORD( 2, 2 ), & wsaData );
      if( result != NO_ERROR )
         cout<<"Initialization error.\n"<<endl;
   }
   SocketManager(int = 27015, string = "127.0.0.1");
};

Im aware that im probably messing up something very basic, but i have no idea how to even search for this. 我知道我可能搞砸了一些非常基本的东西,但我不知道该如何搜索。 Im new to Cpp, and even if i know Java its not helping with this kind of errors. 我是Cpp的新手,即使我知道Java也无助于此类错误。 Also sorry for my english, im not used to using it in written form. 也为我的英语感到抱歉,我不习惯以书面形式使用它。 I have never asked any question before on any other forum. 我从未在任何其他论坛上问过任何问题。 Thats why im asking for patience :) 这就是为什么我要耐心的:)

All help and links appreciated. 感谢所有帮助和链接。

WSAStartup@8 is a C++ mangled name. WSAStartup@8是C ++混乱的名称。 But the winsock library exports a C function WSAStartup. 但是winsock库导出C函数WSAStartup。 You have somewhere defined a prototype of WSAStartup before (or instead) including the "winsock.h" or "winsock2.h" header file. 您已经在某个地方定义了WSAStartup的原型,或者在其中包括“ winsock.h”或“ winsock2.h”头文件。

Removing that wrong prototype will solve this linker problem. 删除该错误的原型将解决此链接器问题。

Further make sure that you link with ws2_32.lib . 进一步确保您与ws2_32.lib链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM