Friday, November 22, 2013

May I know why Header file name must tally with CPP file name?

I must be very long time didn't code C++. Why this error could happened on yesterday? In my memory, it is not suppose be an error, is it only happened in Eclipse? Is it because I'm a long time Visual Studio fans?

The problem is very simple, I have a base class with virtual destructor declare in Base.h
    #ifndef BASE_H_
    #define BASE_H_

    class Base {
        public:
            Base();
            virtual ~Base();
    };
    #endif

And then I have a Child class inherited Base class declare in Child.h
    #ifndef CHILD_H_
    #define CHILD_H_

    #include "base.h"

    class Child : public Base {  // (1)
        public:
            Child();
    };
    #endif
Now make a main.cpp and put the implementation of Base class virtual destructor.
    #include "Base.h"

    Base::Base() {}

    Base::~Base() {}
When building the source code, there is an error complaining that undefined reference to 'Base::Base()' at (1). If I change main.cpp to Base.cpp, the error will gone. There are 2 possibility, it is either a new C++ specification or there isn't a compilation rules define for main.cpp in makefile. Later I found that the second option is making more sense on this. I didn't resolve the problem since the makefile is auto generated, if I make modification on it, I'm afraid this will generate another problem and my development time will be drag.

No comments: