-1

What is the syntax for dynamically allocating an object with an overloaded constructor in C++?

If I have a class Foo:

class Foo{
public:
    Foo(string str, int nbr);    // Overloaded constructor
};

And a second class Foo2 (using Foo):

#include "Foo"
class Foo2{
public:
    Foo* myFoo;    // Wrong!
    Foo2(){
        myFoo = new Foo(myStr, myNbr);
    }
};

The error displayed is the following :

no matching function for call to "Foo::Foo()"

How can I, when creating myFoo object pointer, specify that it will use the Foo(string str, int nbr) constructor, not Foo() constructor.

Is it possible without using constructor delegation?

feronjb
  • 255
  • 2
  • 4
  • 16
  • 3
    what error are you getting? – WeaselFox Feb 27 '14 at 11:55
  • It's not wrong syntax. The syntax is correct. Maybe it's the missing semicolon at the end of the class definition? Also the constructor is private, it should be public to be callable. – dornhege Feb 27 '14 at 11:59
  • Given that error, are you sure that you're compiling the code you think you are? If yes, are you sure that the error is on the line marked "Wrong syntax". – eerorika Feb 27 '14 at 14:30
  • The declaration `Foo* myFoo;` and the call to the constructor is correct in your new `Foo2` example too. It should compile if you fix the missing semicolons and visibility keywords. – eerorika Feb 28 '14 at 14:10

3 Answers3

2

Your syntax to construct the object is correct.

It's hard to say for sure since you haven't told the error, but my guess is that your problem is that the constructor is private. That means you cannot use the constructor outside the class.

Edit concerning the error message:

Here's a complete example that compiles. I've added some example lines that would produce the error: no matching function for call to 'Foo::Foo()'.

#include <string>

class Foo{
public:
    Foo(std::string str, int nbr);
};

// empty definition
Foo::Foo(std::string str, int nbr) {}

// delegating constructor (c++11 feature)
// this would produce error: no matching function for call to 'Foo::Foo()'
//Foo::Foo(std::string str, int nbr) : Foo::Foo() {}

int main() {
    Foo* myFoo;
    myFoo = new Foo("testString", -1); // this is correct
    // trying to construct with default constructor
    //Foo myFoo2; // this would produce error: no matching function for call to 'Foo::Foo()'
    //Foo* myFoo3 = new Foo(); // so would this
}

Given the error, your code is trying to use the default constructor somewhere.

Edit2 concerning your new Foo2 example. Your declaration of Foo* and the call to the constructor are still correct and the code should compile if you fix the method visibility and missing semicolons. Following example compiles:

#include <string>

class Foo{
public:
    Foo(std::string str, int nbr);    // Overloaded constructor
};

Foo::Foo(std::string str, int nbr){}

class Foo2{
    Foo* myFoo;    // This is still correct
public:
    Foo2() {
        myFoo = new Foo("", 1);
    }
};

int main() {
    Foo2 foo2;
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Syntax is correct. There are many possibilities as you have not written full class definition. 1. check whether default cnstsructor is written. 2. Check whether both constructors are inside public section. 3. Alternatively you can change call to constructor as below,

Foo* myFoo = new Foo("testString", -1);

The following code should work.

class Foo
{

string str;
int num;
public:
    Foo(string p_str, int nbr):str(p_str),num(nbr){};    // Overloaded constructor
    Foo() {};    // Default constructor
};
Singh
  • 361
  • 1
  • 8
  • I don't define the default constructor. Is it mandatory? – feronjb Feb 27 '14 at 13:04
  • Yes, In the case you written above it is mandatory, Otherwise call constructor as I mentioned by passing parameters. – Singh Feb 28 '14 at 05:28
  • You are getting below error as `Foo* myFoo;` is calling default constructor " no matching function for call to "Foo::Foo()" " – Singh Feb 28 '14 at 08:51
-1

The correct way is initializing the members in each constructor. You could extract their common code in a private init() member function and call it in each constructor like the following:

class Foo {
    public:
        Foo(string x);
        Foo(string x, int y);
        ...
    private:
        void init(string x, int y);
};

Foo::Foo(string x)
{
    init(x, int(x) + 3);
    ...
}

Foo::Foo(string x, int y)
{
    init(x, y);
    ...
}

void Foo::init(string x, int y)
{
    ...
} 
Vijay Kumar
  • 345
  • 1
  • 7
  • welcome to SO. Since there are lots of questions, it is considered polite to: a. answer the question in the most direct way possible. b. not divert from the subject of the question. Your answer, while suitable to questions phrased like "How can I avoid code duplication in multiple ctors", does not do either. Please rephrase. – Fox Feb 27 '14 at 13:03