-1

In the project I'm working on, I have an outer class named "Camera", and inside this class I am tring to instantiate an object defined by a class named "Frame". The Frame class has this in it's private section:

private:

// No default ctor
Frame();
// No copy ctor
Frame( Frame& );
// No assignment operator
Frame& operator=( const Frame& );

So I get an error when I try to instantiate a frame object since the default constructors are private. In the public section of frame, however, there is this:

public:

Frame( char *pBuffer, int bufferSize );

So that is the constructor I need to call when instantiating Frame from within Camera. Problem is I cannot seem to figure out how to do this. I had the same issue with the Camera class (where the default constructors were in the private section), but all I had to do to fix that was:

Camera::Camera():mCamera(whatever params here){
    //constructor stuff here
}

in the implementation file. When I try to do this same thing for Frame inside of the Camera class, however, I get tons of errors.

So how do I go about calling a particular constructor of an object from inside another object's class?

xcdemon05
  • 1,372
  • 5
  • 25
  • 49
  • 2
    You need to use the member initializer list. – Alok Save Jan 21 '13 at 16:03
  • Please show an [sscce](http://sscce.org/). – chris Jan 21 '13 at 16:03
  • possible duplicate of [What is this weird colon-member syntax in the constructor?](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) – Alok Save Jan 21 '13 at 16:03
  • 1
    @AlokSave, There's something about the last code sample (and the text after) that suggests there's more to it. – chris Jan 21 '13 at 16:04
  • What exactly is your sytax for 'do[ing] this same thing for Frame'? It should be `: mCamera(....), mFrame(....) {` – David Jan 21 '13 at 16:06
  • 1
    What do you mean by *When I try to do this same thing for Frame inside of the Camera class*? Please show the example code. – Masked Man Jan 21 '13 at 16:06
  • 1
    your question is unclear: the easy answer is that you do it exactly the same way you did for `mCamera`. but since you're asking, it's obvious that there's something trickier about `Frame`. so what is the tricky part? it seems to be missing in the question – Andy Prowl Jan 21 '13 at 16:06
  • @chris: There is not enough code to say anything at all than suggest proper use of member initializer list. – Alok Save Jan 21 '13 at 16:09
  • @AlokSave, That's true. That's why I wanted the sscce. – chris Jan 21 '13 at 16:10

1 Answers1

1

I am guessing that you tried using a : for every member in the constructor initialization list Camera::Camera():mCamera():mFrame(NULL, 0). You need to separate the initializers with ,, like so:

Camera::Camera() : mCamera() , mFrame(NULL, 0)

It may also be a good idea to read a good C++ textbook.

Community
  • 1
  • 1
Masked Man
  • 1
  • 7
  • 40
  • 80
  • This fixed the problem. I was trying to do everything in a seperate chunk of code, I didn't know it could be appended to the end of the constructor for the camera class. Thanks! – xcdemon05 Jan 21 '13 at 16:31