Reputation: 4948
I am more a C guy but I am currently working on a C++ project. That is why i am getting a little confused:
we have an inheritance like this:
Node->MultiNode->RoundAbout
Now some code(for simplicity, I ommitted almost all but the constructors):
class MultiNode : public sim_mob::Node {
public:
MultiNode(int x, int y) : Node(x, y) {}
...
}
and
class Roundabout : public sim_mob::MultiNode {
public:
Roundabout() : MultiNode() {}
}
As you can see, the Roundabout() constructor calls MultiNode() constructor without any argument while MultiNode has only one constructor with 2 args :
MultiNode(int x, int y)
Is such a scenario possible? what is the expanation please?
This code compiles well in its original place but when I copied the folder to a testing area to start editing, I get this error(which is sensible to me): error: no matching function for call to ‘sim_mob::MultiNode::MultiNode()
there is no other similar file or class to suspect misplacing.
error for your reference:
~/workspace/parser5/geospatial$ make
Scanning dependencies of target driver
[ 33%] Building CXX object CMakeFiles/driver.dir/geo5-pskel.cxx.o
In file included from /home/vahid/workspace/parser5/geospatial/geo5-pskel.hxx:134:0,
from /home/vahid/workspace/parser5/geospatial/geo5-pskel.cxx:39:
/home/vahid/workspace/parser5/geospatial/Roundabout.hpp: In constructor ‘sim_mob::Roundabout::Roundabout()’:
/home/vahid/workspace/parser5/geospatial/Roundabout.hpp:34:27: **error: no matching function for call to ‘sim_mob::MultiNode::MultiNode()’**
/home/vahid/workspace/parser5/geospatial/Roundabout.hpp:34:27: note: candidates are:
/home/vahid/workspace/parser5/geospatial/MultiNode.hpp:42:2: note: sim_mob::MultiNode::MultiNode(int, int)
/home/vahid/workspace/parser5/geospatial/MultiNode.hpp:42:2: note: candidate expects 2 arguments, 0 provided
/home/vahid/workspace/parser5/geospatial/MultiNode.hpp:40:7: note: sim_mob::MultiNode::MultiNode(const sim_mob::MultiNode&)
/home/vahid/workspace/parser5/geospatial/MultiNode.hpp:40:7: note: candidate expects 1 argument, 0 provided
make[2]: *** [CMakeFiles/driver.dir/geo5-pskel.cxx.o] Error 1
make[1]: *** [CMakeFiles/driver.dir/all] Error 2
make: *** [all] Error 2
thank you
Upvotes: 0
Views: 1114
Reputation: 44488
Are you sure your build process is not modifying the code in some way before compiling it? Is there some pre-processor macro that creates the default constructor (yuck) that does not get triggered because some project setting is not defined?
Those kinds of things are the only things I can think of that would cause the code to compile.
As you said, that code should not compile because the default constructor does not exist.
Upvotes: 1
Reputation: 34581
No, you're right, that doesn't make sense. If MultiNode doesn't have a default constructor, you can't call one from a derived constructor. The error message you're getting is what I'd expect, so it's not clear what your question is.
You said "this code compiles well in its original place but when I copied the folder to a testing area to start editing, I get this error". That makes me wonder what its "original place" is, and why you had to copy it somewhere to edit it. Are you using a version control system, or is it more like a single copy of the code in a shared folder that everyone hacks on? If it's the latter, you don't have a way to ensure that the shared copy is up-to-date and consistent with everyone's changes; maybe MultiNode does have a default constructor in that copy of the code.
Upvotes: 1
Reputation: 6052
Or you can pass the default values for X,Y from roundabout
class Roundabout : public sim_mob::MultiNode {
public:
Roundabout() : MultiNode(0, 0) {}
Roundabout(int x, int y) : MultiNode(x, y) {}
}
Upvotes: 1
Reputation: 40643
This error is as expected.
You can only construct an object using a constructor which exists. In some situations, a default constructor (zero argument constructor) will be implicitly generated as default
, but that does not apply in this case because you already have a user-defined constructor.
Upvotes: 1
Reputation: 1844
No, you cant do that because no default constructor is available for MultiNode. create a default constructor also that accepts no arguments or provide some default arguments to MultiNode Constructor.
i.e.
class MultiNode : public sim_mob::Node {
public:
MultiNode(int x, int y) : Node(x, y) {}
...
MultiNode(){
//....
}
};
or
class MultiNode : public sim_mob::Node {
public:
MultiNode(int x = def_x, int y = def_y) : Node(x, y) {}
...
MultiNode(){
//....
}
};
Upvotes: 1