I have a weird problem in C++. Probably I am just doing something wrong, but I can't figure out, what. I am using Microsoft VS2013 if that is important.
I created a new class with two constructors:
class CtVolume{
public:
CtVolume();
CtVolume(std::string path);
...
The first one should overwrite the default constructor. This is the one that does not work as I expected. If I do something like:
CtVolume myVolume();
then I think this should call the default constructor that I defined and execute the code I implemented for it. But it doesn't. The code in my Constructor is not executed. What is even more puzzling is that I can't use the myVolume
like an object afterwards. If I attempt to call one of the methods implemented in the class:
myVolume.reconstructVolume();
The compiler does not let me compile my code. It says:
Error 5 error C2228: left of '.reconstructVolume' must have class/struct/union
So as it seems, this way I can't create an object.
Now, if I create the object with the keyword 'new' instead, my constructor code is being executed and everything works as expected. Example:
CtVolume* myVolume = new CtVolume();
myVolume->reconstructVolume();
This works fine.
Now another thing: if I use the other constructor I defined, both ways of creating the object work equally good, meaning this example:
CtVolume myVolume("sourcefiles/data/skullPhantom");
myVolume.reconstructVolume();
compiles and works as expected.
Now I tried to find out what type of myVolume
is in the different cases, by calling typeid(myVolume).name()
. This returns the type class CtVolume __cdecl(void)
in the case that I described, where it "doesn't work" and class CtVolume
in the "working" case where I used the other constructor.
Now can anybody tell me, what the __cdecl(void)
means and what I did wrong?
What also puzzles me is that I think I did that a lot of times before and it always worked for me until now.
Thanks!
The line:
CtVolume myVolume();
is interpreted by the compiler as a function declaration. Take a look at most vexing parse.
Just do:
CtVolume myVolume;
The above code will call the default constructor appropriately.