1

please how can i write definition of constructor with parameters outside class declaration?

class A{
 public:
  A(int x);
  ~A();
}

A::A(int x) { ... }
A::~A(){...}


class B : public A
{
 public:
  B(int x):A(x){ this work fine}
  ~B(){}

}

and this not work

class B : public A
{
 public:
  B(int x):A(x); // error here
  ~B();

}

B::B(int x):A(x){ this not work };
B::~B();
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Pepík Samků
  • 31
  • 1
  • 6
  • possible duplicate of [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Lightness Races in Orbit Feb 05 '13 at 15:12
  • 1
    Unrelated: the destructor of B isn't needed at all (all it does it confused the trained eyes of good C++ programmers that will be lead to believe the class violates the rule of three.) – R. Martinho Fernandes Feb 05 '13 at 15:14
  • @R.MartinhoFernandes: Still, best to have it so that it can be given a non-trivial implementation later without breaking the API! Though, sure, by the same logic we should declare a destructor, copy constructor and assignment op for _every_ type which would be daft. Hmm. If it were appropriate on SO I'd post a question about this. – Lightness Races in Orbit Feb 05 '13 at 15:16
  • Thanks for answers . I solved it. I rewrite a program and problem was elsewhere. The compiler said no ; after B(int x) and redefinition for B::B(int x):A(x). – Pepík Samků Feb 05 '13 at 15:35
  • @PepíkSamků: Don't forget to write an answer and accept it. Except... you were already given the answer. – Lightness Races in Orbit Feb 05 '13 at 15:53

5 Answers5

4

This question seems to be collecting an unusual number of incorrect answers (though some look like they're probably a result of oversight, not misunderstanding).

The situation is pretty simple. Inside the class definition you can have either a member function declaration or a member function definition. Outside the class definition you can only have a function definition (in which case, inside the class definition, you must only declare that function, not define it). That means the corrected code looks something like this:

class B : public A {  // This is the class definition.
public:
    B(int x);    // declare member functions here.
    ~B();
};

// define member functions here.
//
B::B(int x) : A(x) { /* body */ }
B::~B() { /* body */ }      // a declaration like `B::~B();` is not allowed here.
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
3

B(int x):A(x); // error here

You removed the body of the method, but you forgot to remove the member initialization list:

B(int x); // No error

In addition, you need to put semicolons after the last closing brace of your class definitions.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You're mixing declaration with implementation. It should be:

class B : public A
{
 public:
  B(int x);
  ~B();    
}

B::B(int x):A(x){ /*this works */ };
B::~B() {}
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

This is your code:

class B : public A
{
 public:
  B(int x):A(x); // error here
  ~B();

}

When you do something like ~B(); you are declarating the existence of the destructor, but since you ended up there (note the ";") you did not define it. The same goes on the constructor, in the line that you marked you you invoked the super constructor, which is a definition, but you did not provide the method body, which is only expected on declarations.

To fix your problem just leave the declaration as B(int x);, without specifying how B relates with A, this will be later specified later in the definition.

Calling the super constructor is part of the generated code, the caller does not need to know about that, that why you can declare a constructor without define how it will construct it's super classes.

André Puel
  • 8,741
  • 9
  • 52
  • 83
  • 2
    -1: This is not valid C++ code. – Lightness Races in Orbit Feb 05 '13 at 15:14
  • @LightnessRacesinOrbit I copy pasted the Pepík Samků code and changed only the part concerning his questions... Are you going to downvote me because I did not fix the missing ';'? – André Puel Feb 05 '13 at 15:49
  • Yes. And because you did not write any prose in your answer. Good answers _explain_ the problem, then _explain_ the solution, then provide code to demonstrate the solution. – Lightness Races in Orbit Feb 05 '13 at 15:51
  • I will remove my downvote only when your answer does not suggest using invalid C++ code. Hopefully this will serve as a lesson not to simply copy/paste potentially broken code from the web! – Lightness Races in Orbit Feb 05 '13 at 15:52
  • I just though he would prefer a quick answer to his problem. If it may concern you, I find it offensive that you suggest that I "simple copy/paster broken code from the web", of course I don't do that... But, if you insist, I gave an insight on _why_ his problem was happening... – André Puel Feb 05 '13 at 16:14
  • `I copy pasted the Pepík Samků code`; the code was broken, and Stack Overflow is a website. So... yes, you did! – Lightness Races in Orbit Feb 05 '13 at 16:45
  • `just leave the definition as B(int x);` No, that makes it _not a definition_, but a _declaration_. – Lightness Races in Orbit Feb 05 '13 at 16:46
0

Try

class B : public A
{
 public:
  B(int x);
  ~B();
};

B::B(int x):A(x){ }
B::~B() {}