Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:06:04+00:00 2026-05-31T17:06:04+00:00

First of, I know there are similar questions already on stackoverflow ( this ,

  • 0

First of, I know there are similar questions already on stackoverflow (this, this and this one) and that is why I understand the why of my problem. Unfortunately, that doesn’t help me to solve it.

While the above questions are all concerning the default, no-parameter constructor, I have a problem while using a two parameter constructor with default values – I tried to construct an object calling the constructor with only the first value given, and it is parsed as a function declaration instead of an object.

Here are some snippets of my code (I renamed the class names because they’re long and not relevant):

class algoContainer{
public:
algoContainer(algo1Virtual &alg1 = algo1Concrete::emptyInstance(),
      algo2Virtual &alg2 = algo2Concrete::instance());

someUsefulFunction();
};

class algo1Concrete : public algo1Virtual{
    private:
    algo1Concrete();
    public:
    static algo1Concrete &emptyInstance(); // "empty" instance treated
                                           // specifically
                                           //  -- uses private no arg constructor
    algo1Concrete(const std::vector<data> &myData); // construcotr
};

class algo1Virtual{
    // ... all functions virtual, no implementations ...
};


// ... similar for algo2Virtual/Concrete ...

All the functions in the Concrete classes are implemented, while none of them in the Virtual classes are (except the constructors and the destructors).

So, my problem now is that I want to do something like:

std::vector <data> workData;
// fill workData
algoContainer myAC(algo1Concrete(workData));
myAC.someUsefulFunction(); // this line gives compile error

Nice, cute and ellegant, but it does not work (error the same as all the questions I linked). I’ve found this forum-tutorial that does refer to the problem as most vexing parse, but it’s solution (put parenthesis around argument) doesn’t solve the problem (it’s a long bunch of error msgs in that case, but I can edit it in the question later if it helps – those are all related to inheriting a virtual function).

I’ve tested my code if I use the constructor with all the parameters on default, and even if I just construct the first parameter separately:

std::vector <data> workData;
// fill workData
algo1Concrete myA1(workData);
algoContainer myAC(myA1);

myAC.someUsefulFunction(); // now it works fine

algoContainer myAC2;
myAC2.someUsefulFunction(); // this also works

I can use the code as it is, but it would be greatly appreciated if somebody could give me a more elegant solution to the one I’m using right now.


EDIT: error msgs I get when I fix the most vexing parse

If I use the code with parenthesis:

algoContainer myAC((algo1Concrete(workData)));

My errors are:

/some_path/main.cpp:47:65: error: no matching function for call to ‘algoContainer::algoContainer(algo1Concrete)’
/some_path/main.cpp:47:65: note: candidates are:
/some_path/algo/algocont.h:45:5: note: algoContainer::algoContainer(algo1Virtual&, algo2Virtual&)
/some_path/algo/algocont.h:45:5: note:   no known conversion for argument 1 from ‘algo1Concrete’ to ‘algo1Virtual&’
/some_path/algo/algocont.h:36:7: note: algoContainer::algoContainer(const algoContainer&)
/some_path/algo/algocont.h:36:7: note:   no known conversion for argument 1 from ‘algo1Concrete’ to ‘const algoContainer&’

I renamed the paths and inserted the example file and class names (the same as above) for readability. Just a remark: line 45 is the definition of the constructor in question. On the other hand, line 36 is the line class algoContainer.

I also tried with this code:

algoContainer myDect((algo1Virtual)(algo1Concrete(workData)));

And then the errors are completely different:

/some_path/main.cpp:47:86: error: cannot allocate an object of abstract type ‘algo1Virtual’
/some_path/algo/alg1/algo1virtual.h:31:7: note:   because the following virtual functions are pure within ‘algo1Virtual’:
/some_path/algo/alg1/algo1virtual.h:42:8: note:     virtual algo1Virtual::~algo1Virtual()
/some_path/algo/alg1/algo1virtual.h:39:18: note:    virtual void algo1Virtual::someAlgo1Function(std::vector<data>&)
/some_path/main.cpp:47:87: error: no matching function for call to ‘algoContainer::algoContainer(algo1Virtual)’
/some_path/main.cpp:47:87: note: candidates are:
/some_path/algo/algocont.h:45:5: note: algoContainer::algoContiner(algo1Virtual&, algo2Virtual&)
/some_path/algo/algocont.h:45:5: note:   no known conversion for argument 1 from ‘algo1Virtual’ to ‘algo1Virtual&’
/some_path/algo/algocont.h:36:7: note: algo1Virtual::algo1Virtual(const algo1Virtual&)
/some_path/algo/algocont.h:36:7: note:   no known conversion for argument 1 from ‘algo1Virtual’ to ‘const algo1Virtual&’

Hope this helps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:06:06+00:00Added an answer on May 31, 2026 at 5:06 pm

    The issue seems to be due to the arguments taken by the constructor:

    algoContainer( algo1Virtual &alg1,
                   algo2Virtual &alg2 );
    

    Note: I have removed the default arguments for brevity.

    this takes the arguments as non-const references. So, when you make a call like:

    algoContainer myAC(algo1Concrete(workData));
    

    the construction of:

    algo1Concrete(workData)
    

    leads to the construction of an anonymous temporary. Anonymous temporaries cannot bind to non-const references, simple because they are temporary and any changes you might make to them will instantly go away (thats not the real reason, but seems to make sense. It doesn’t mean anything to modify an anonymous temporary as you have no way to use it later (no name) or eventually (its temporary)). Actually, non-const references can only bind to l-values, and anonymous temporaries are r-values. (Details: Non-const reference may only be bound to an lvalue)

    In general, this kind of use means that one wants to give complete ownership of the object being constructed to the function. This can be done by either passing-by-value (expensive), or in C++11, by passing by rvalue reference.

    Passing by value will look like:

    algoContainer( algo1Virtual alg1,
                   algo2Virtual alg2 );
    

    This will result in unnecessary copies.

    The other option is to pass by rvalue reference in C++11 like:

    algoContainer( algo1Virtual &&alg1,
                   algo2Virtual &&alg2 );
    

    Now your first usage will work out of the box:

    std::vector <data> workData;
    // fill workData
    algoContainer myAC(algo1Concrete(workData));
    myAC.someUsefulFunction();
    

    but you second usage will need to be modified so that your object is “moved” into the constructor, and the algoContainer takes ownership of the data (the names local variable is then “bad” and should NOT be used at all.

    std::vector <data> workData;
    // fill workData
    algo1Concrete myA1(workData);
    algoContainer myAC(std::move(myA1)); //NOTICE THE std::move call.
    //myA1 is now a dummy, and unusable as all the internals have gone.
    myAC.someUsefulFunction(); 
    

    For this above example to work, you will have to implement a move constructor for algo1Concrete with the following signature:

    algo1Concrete ( algo1Concrete&& other )
    

    which will simply transfer the internals to the current and leave the “other” in an undefined state. (Details: http://msdn.microsoft.com/en-us/library/dd293665.aspx)

    NOTE: Regarding default arguments.

    I generally suggest that default arguments to functions be avoided, as they lead to more confusion than convenience. All default parameters can be “simulated” simply by overloading the function. Thus, in your case, you would have three ctors:

    algoContainer(); //This assumes that the args were both the statics
    algoContainer( algo1Virtual alg1 ); //This assumes that arg2 was the static.
    algoContainer( algo1Virtual alg1, algo2Virtual alg2 ); //This uses both input.
    

    I agree its more verbose, and not a lot of compilers currently implement inheriting constructors, so we also quite often have copied code. But this will isolate one from a number of debugging/magic value issues which pop up while investigating an issue. But, FWIW, its just an opinion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all: I do know that there are already many questions and answers
I know that there are similar questions which are already answered, but I am
First of all, I know there are a few quite similar questions here on
I know this is the repeat question.There are many similar questions are there in
I know there's already tons of similar questions, but I checked over twenty answers
I know there are already articles that covered this, but I'm having an issue
I know there are similar questions on stackoverflow - and I looked through them
There are questions LIKE this one, but they are not similar enough to my
I know there are similar questions out there, but I'm having trouble with this
Before you tell me that there is already a similar question, yes, i know,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.