c++structtranslation-unit

Class' Struct's function declaration in header and definition in cpp. lnk2019 when using function in separate lib


The following will build fine. When I try to use a function in a different library I get linker errors.

OtherClass.obj : error LNK2019: unresolved external symbol "public: float __cdecl myClass::myClassStruct::myConvoludedFunction(int,int)" (?myConvoludedFunction@myClassStruct@myClass@@QEAAMHH_N@Z) referenced in function "public: __cdecl myNamespace::OtherClass::OtherClassFunc(class myNamespace::Something &)" (??0OtherClass@myNamespace@@QEAA@AEAVDtDe@1@@Z)

Header:

   class MY_EXPORT_MACRO myClass
   {
    public:
        myClass();
        ~myClass();
        struct myClassStruct
        {
            int var1
            int var2
            float myConvoludedFunction(int,int);
        }
    }

cpp

#include "myClass.h"
myClass::myClass()
{
    //something initialisy
}

myClass::~myClass()
{
    //something destructiony
}

float myClass::myClassStruct::myConvoludedFunction(int a, int b)
{
    //Something convoluded and secret
    return 0;

}

In the actual code there is a number of structs defined in the class. So I tried making them all nested classes with public members and member functions. Similar thing: lib builds, lnk2019 when using the functions in a separate lib. The other functions in myClass work fine.

It's being used like this:

#include myClass.h
namespace myNameSpace
{
    OtherClass::OtherClassFunc(Something & athing)
    {
        myClass * object = new myClass();

        myClass::myClassStruct aStruct;

        FunctionThatInitialisesStruct( aStruct);

        aStruct.someConvoludedFunction(1,2);

    }
}

Is there something I'm doing that's obviously wrong? Is it not standard to have struct function definitions declared separately?


Solution

  • You need to export/import the symbols for myClassStruct also.

    Use:

    struct MY_EXPORT_MACRO myClassStruct { ... };