domlao
domlao

Reputation: 16029

Undefined Reference with inline Destructor

I have a class that I inline'd the constructor and the destructor and I also have a static method for that class. I called that static method inside of the inline destructor, but I'm having a linker error undefined reference for that static method. I'm pretty sure it is compiled and link with the object. Here is the code,

// CFoo.h
namespace barname {
class CFoo
{
public:
    CFoo();
    ~CFoo();

    static void fooMethod();
};

inline CFoo::CFoo()
{
}

inline CFoo::~CFoo()
{
    fooMethod();
}
}

// SomeOtherSource.cpp
namespace barname
{
    void CFoo::fooMethod()
    {
    }
}

It seems the code is fine and compiling.

Upvotes: 0

Views: 183

Answers (2)

wshcdr
wshcdr

Reputation: 967

#ifndef con001_CFoo_h
#define con001_CFoo_h

namespace barname {
class CFoo
{
public:
    CFoo();
    ~CFoo();


    static void fooMethod();
};
inline CFoo::CFoo()
{

}

inline CFoo::~CFoo()
{

}

}

#endif

in other file

#include "CFoo.h"
namespace barname
{
void CFoo::fooMethod()
{
}
}

compiled ok in XCode 4.6

Upvotes: 1

user554180
user554180

Reputation:

Here is a slightly modified working version of the code

Coliru example

#include <iostream>
#include <string>
#include <vector>

namespace barname 
{
    class CFoo
    {
    public:
        CFoo();
        ~CFoo();

        static void fooMethod();
    };

    inline CFoo::CFoo()
    {
    }

    inline CFoo::~CFoo()
    {
        fooMethod();
    }

    void CFoo::fooMethod()
    {
        std::cout << "in fooMethod" << std::endl;
    }
}


int main()
{
    barname::CFoo *f = new barname::CFoo();
    delete f;
}

Upvotes: 1

Related Questions