views:

42

answers:

1

Hi. I have some strange linking problem in my Visual Studio 2005 C++ project. As always, I declare class in a header and define it's methods in cpp. A have all these files included in my project. And I still have the unresolved external symbol calcWeight. It appears if I actually use this class in my main function. calcWeight() is declared as virtual in the parent class CHDRGenerator If I comment a code in cpp and define calcWeight in a class body, it works fine. But i really don't like this magic. Can someone help?

Here is the part of a code:

//mann-pickard.h
#include "stdafx.h"
#include "simple.h"

class CHDRGenerator_Mann_Pickard : public CHDRGenerator
{
public:
    /// @name Constructors
    /// @{
    /// @brief a constructor using prepared imaged sequence
    CHDRGenerator_Mann_Pickard(CSimpleImageFile * imSeq, int seqL) : CHDRGenerator(imSeq, seqL)
    {
    }

    /// @brief a constructor using filenames
    CHDRGenerator_Mann_Pickard(std::string * filenames, int seqL) : CHDRGenerator(filenames, seqL)
    {
    }

    /// @brief a constructor, CFileNameSequence object parameter
    CHDRGenerator_Mann_Pickard(CFileNameSequence & const fileseq) : CHDRGenerator(fileseq)
    {
    }
    /// @}

private:
    inline double calcWeight(double val);
};


//mann-pickard.cpp  
#include "mann-pickard.h"
//=======================Class CHDRGenerator_Mann_Pickard methods=====================//

//...

inline double CHDRGenerator_Mann_Pickard::calcWeight(double val)
{
    const double gamma = 2.2f;
    return gamma * pow(val, gamma - 1);
}
//=====================End of Class CHDRGenerator_Mann_Pickard methods=================//

"simple.h" is a header with CHDRGenerator class implementation. I know it should work...as it always worked. Maybe I have some stupid hard-to-find mistake?..

A: 

What happens if you remove "inline" from your declaration and definition of calcWeight?

janks
Hmm... Error has gone. But why can't I declare calcWeight as inline?
Irene
If you declare a method as inline it needs to be either in the same file as the class declaration or included after the class declaration. Compilers have a hard time inlining methods that are defined in external translation units.
Thomas Matthews
The standardese is complicated, but essentially, inline means that every "translation unit", (roughly speaking, every .cc file after #includes have been processed), has to be able to see the definition of that inline function. So merely including a .hh file that only provides a declaration, but not a definition, as is the typical usage of a header file, isn't sufficient. Inline behaves very much like static, in the sense that it tells the linker "ignore this function, nobody from outside of this translation unit will call it, everyone who uses this function uses his own local copy of it".
janks
Ah, I see now. Thank you!
Irene