开发者

C++ Calling Static member function from external file

I have this class defined in Global.h

    class Global
    {
     public:
         static string InttoStr(int num);
    };

In Global.cpp, i have

    Global::InttoStr(int num)
    {
        //Code To convert inte开发者_JAVA百科ger into string.
    }

Now, from SubMove.cpp, when i call Global::InttoStr(num) I get following error:

error LNK2019: unresolved external symbol Global::InttoStr(int) referenced in function SubMove::toString(void)

Then I made the function non-static and called it like so:

      Global g;
      g.InttoStr(num);

But the error still persists.

I thought it had something to do with extern and searched it but i could not make any connection. Please help.


First off, try this:

string Global::InttoStr(int num)
{
    //Code To convert integer into string.
}

Also, are you calling InttoStr from another library ? If so, you'll need to export the class "Global".

Best practice is to use a lib header (in the example below replace LIB_ with the name of the library):

#ifndef SOME_LIB_HEADER
#define SOME_LIB_HEADER

#if defined (LIB_EXPORTS)
    #define LIB_API __declspec(dllexport)
#else
    #define LIB_API __declspec(dllimport)

#endif // SOME_LIB_HEADER

Define LIB_EXPORTS in your project contianing Global, include the lib header in Global.h, then define the class like this

class LIB_API Global
{
    // some code for the class definition
};

Each project should have its own LIB_EXPORTS and LIB_API definition like DLL1_EXPORTS, DLL1_API, DLL2_EXPORTS, DLL2_API etc.

Basically this makes a separate lib treat the previous dll with __declspec(dllimport) and resolve all externs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜