-1

I have 3 classes coded in c++. They are all simple, but compiler gives alot error. Each class in a cpp file and header file. What can be the possible problems? One of it is "base class undefined".

class re {
  int i;
  int j;
  string a;
  re(int,int,int);
  ~re();
}

class Pre:public re {
  int k;
  public:
    Pre(int,int,int);
   ~Pre();
}

class MPre:public Pre {
  int z;
  public:
      MPre(int);
     ~Mpre(int);
}
ds s
  • 3
  • 1
  • 8
    Class definitions should end with a semi-colon(";"). Are you using a semi-colon in your actual code? (you're not using it in your example). – mfontanini Apr 19 '12 at 18:53
  • 2
    can you post the errors, I mean what you have pasted could be because there is no terminating `;` at the end of the class definitions but i's difficult to guess. – EdChum Apr 19 '12 at 18:53
  • I have put semicolon, there are still errors. – ds s Apr 19 '12 at 18:55
  • 3
    you should still post your full code and error output, we cannot guess if your pasted code does not match what you compiled with – EdChum Apr 19 '12 at 18:55
  • 3
    First time I see a destructor with an argument. Are you sure about that? – Some programmer dude Apr 19 '12 at 18:56
  • I wrote default constructor for Pre. All errors gone. Thank you. – ds s Apr 19 '12 at 19:00
  • @dss in the future you should take note of our comments if you want your questions answered correctly and promptly it shouldn't be a guessing game on stackoverflow, still good that you resolved your problem – EdChum Apr 19 '12 at 19:01
  • Yes, i need destructor argument. – ds s Apr 19 '12 at 19:01
  • Sorry, i am new in stackoverflow. – ds s Apr 19 '12 at 19:02
  • This should help a lot http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – DumbCoder Apr 19 '12 at 19:13

3 Answers3

2

Possible mistakes :

  • re class constructor is not public. Are you aware of this?

  • MPre constructor gets one input, so you have to write default constructor for Pre class.

  • Be careful when adding header files. For example, Pre class needs re class' header file.

  • Also, you don't need to write destructor unless you add pointer variables to classes

vkx
  • 424
  • 1
  • 7
  • 17
0

For the error undefined base class, I think you haven't #included the file in which the class re is declared in the files where other classes are.

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
0

The semicolons are missing in the classes declaration. Also for good separate all the classes in the different .h .cpp files so you will avoid to include unwanted code in case you don't need it. Also in case of inheritance make sure that your destructors are virtual otherwise it will produce a memory leaks.

Also re's constructor is not public make it public.

AlexTheo
  • 4,004
  • 1
  • 21
  • 35