linker error: undefined reference - error in easy class

Hey guys,

i'm using Dev-C++, and everytime i try to make some classes, it's one and the same error. Here is an example of my Code - its pretty easy, but the error is also included in bigger programs i've written.
I've written two different constructors. CelestialBody() as the default-constructor, and CelestialBody(double radius, bool selfLuminous); to get the possibility to initialize with arguments. Same error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

class CelestialBody{
      
      private:
              double radius;
              bool selfLuminous;
      public:
             CelestialBody();
             //CelestialBody(double radius, bool selfLuminous);
             void setRadius(double m) {radius = m;}
             void setSelfLuminous(bool k) { selfLuminous = k;}
             double getRadius() { return radius;}
             bool getSelfLuminous() { return selfLuminous; }
             //~CelestialBody();
};                     
                                    

int main(){
    CelestialBody celest;
    celest.setRadius(3.5);
    celest.setSelfLuminous(0);
    //CelestialBody* celest = new CelestialBody(3.5,0);
    cout << "Radius: "  << celest.getRadius() << endl;
    cout << "Planet Leuchtet: " << celest.getSelfLuminous() << endl;
    //delete celest;
    
    system ("PAUSE");
    return EXIT_SUCCESS;
}


Now the error is:
  [Linker error] undefined reference to `CelestialBody::CelestialBody()' 


I'm pretty new to C++, so i would be glad if you can help me to find my mistake.
This error means that the linker is unable to find the compiled code that is the constructor function; that is, it needs to call CelestialBody::CelestialBody() and you haven't written it.

You declared it in the class definiton,

CelestialBody();

If you declare any constructors in a class, there will be no default constructor created by the compiler; you must also write it.

Looking at your code, I see no reason to define your own default constructor; I think you could just remove that declaration, and the constructor will be written for you.
Thanks a lot. It works fine now.
Can't believe such a mistake happened to me :-/

Thanks again.
Topic archived. No new replies allowed.