Benyamin Jane
Benyamin Jane

Reputation: 407

pass Enumeration to function in c++

i am really confusing about how to pass an enum type to a function in c++. i am googling and test all the soloutions presented but non of them solve my problem. in the SocketInfo.h , i have an enum named SocketType that declared globally as :

    typedef enum SocketTypeEnum
{
    SOCKET_TYPE_IPSEC
} SocketType;

in the SocketInfo.h i have a class named SocketInfo :

class SocketInfo
{
public:
    SocketInfo(const char* ip,unsigned short fd,SocketType stype);
}

in the SocketInfo.cpp :

SocketInfo::SocketInfo(const char* ip, unsigned short fd,SocketType stype)
{
    //some work done here
}

i build this class without any error

now for test this class i create a win32 console application.in the _tmain i write this code

#include "SocketInfo.h"

void Test_Socket()
{
    SocketInfo* si = new SocketInfo(NULL,5060,SOCKET_TYPE_IPSEC);
}

int _tmain(int argc, _TCHAR* argv[])
{
    Test_Socket();
    getch();
    return 0;
}

after running above code i got these errors:

Error 4 error LNK2019: unresolved external symbol "public: __thiscall SocketInfo::SocketInfo(char const *,unsigned short,enum SocketTypeEnum)" (??0SocketInfo@@QAE@PBDGW4SocketTypeEnum@@@Z) referenced in function "void __cdecl Test_Socket(void)" (?Test_Socket@@YAXXZ)

Error 5 error LNK1120: 1 unresolved externals

how i can solve these errors.

all codes compile on visual studio 2010 Ultimate.

Upvotes: 0

Views: 189

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258618

The problem isn't in how you pass the enum as parameter, which is ok, but that you don't export symbols from your project or import them in your test project.

You need to add the lib file generated by the project defining SocketInfo to the additional dependencies of the test project.

You also need to export the class:

_declspec(dllexport) class SocketInfo
{
public:
    SocketInfo(const char* ip,unsigned short fd,SocketType stype);
};

and import it in the test project with _declspec(dllimport). This duality is usually achieved through macros - look it up.

Upvotes: 2

Related Questions