Firstly, I know that there are similar questions already about stackoverflow ( this , this and this ), and that is why I understand the cause of my problem. Unfortunately, this does not help me solve this problem.
While the above questions are about the default no-parameter constructor, I have a problem when using two parameter constructors with default values ββ- I tried to build an object that calls the constructor with only the first value given, and this is parsed as declaring a function instead of an object .
Here are some snippets of my code (I renamed the class names because they are 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();
All functions in the Concrete
classes are implemented, while none of them in the Virtual
classes (except for constructors and destructors).
So now my problem is that I want to do something like :
std::vector <data> workData;
Nice, cute and ellegant, but it doesnβt work (the error is the same as all the issues that I have connected ). I found this tutorial forum that refers to the problem as the most unpleasant parsing, but this solution (putting parentheses around the argument) t solve the problem (this is a long group of error messages in this case, but I can edit it in the question later if this helps - all this is related to the inheritance of a virtual function).
I tested my code if I use a constructor with all the default parameters, and even if I just build the first parameter separately:
std::vector <data> workData;
I can use the code as is, but it would be very appreciated if someone could give me a more elegant solution for the one that I'm using right now.
EDIT : The error messages I get when I fix the most unpleasant parsing
If I use the code with parenthesis:
algoContainer myAC((algo1Concrete(workData)));
My mistakes:
/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 sample files and class names (same as above) for readability. Just a note: line 45
is the definition of the constructor in question. line 36
, on the other hand, is a class algoContainer
line.
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.