C++ VS2013: overwriting default constructor not working [duplicate] - c++

This question already has answers here:
Default constructor with empty brackets
(9 answers)
Closed 8 years ago.
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.

Related

Visual Studio Intellisense suddenly shows copy constructor and not parametrized constructor

I don't know what changed, but my VS IntelliSense during C++ coding suddenly started showing a different order of class constructors.
I would expect it would show my defined parametrized constructors first, as it used to, but now there is a copy constructor in the first place and my constructors after that.
A minor hindrance, but now I have to go down the list to find what I need every time.
My Google-fu didn't give me a solution apart from defining the copy constructor and then hiding it from IntelliSense manually.
I found out this behaviour is not solution-specific.
What did I do wrong to achieve this behaviour and how can I change things back to showing parametrized constructor in the first place?
EDIT:
What I tried so far (and did not help):
restarting Visual Studio
deleting .vs folder
Please check if you have modified the code that involves the constructor. The order of constructors defined will affect the order IntelliSense display. Here's a workaround to set your defined parametrized constructors first.
change the order of constructor defined in your class file. Define the copy constructor in the first place and define parametrized constructors after that. Then you'll see IntelliSense automatically show parametrized constructors in the first place. There seems an inverted order mapping between class constructors and IntelliSense display.
Note: please make backup before you modify your code.
For example:
Hope it can help you.

What is the difference between setting the copy constructor to private and =delete? [duplicate]

This question already has answers here:
delete modifier vs declaring function as private
(4 answers)
Closed 3 years ago.
I have seen a lot of books recommend using =delete, is this just clear what it means? (making the program more readable) rather than saying that it is a bad thing to set the copy constructor to private?
Thinks your answers
class A {
A(const A&);
// some functions and variable
public:
// or you can A(const A&)=delete;
// do something
};
This is a relatively new functionality (added in the 2011 revision of C++) whose main motivation surely was readability and clarity of intent. However, the difference is more than just cosmetic.
Remember that with a constructor declared in the class, nothing prevents some other translation unit from actually providing the definition. It is quite usual to just list a class' member functions in a header file and implement them in a separate .cpp. If someone uses the copy constructor from inside the class, the compiler will complain that the definition is missing ("undefined reference to..."). If a naive programmer somehow reaches the conclusion that you forgot to implement it because you never needed it, they can go ahead and do so. Suddenly your class is copyable, even though only from within its own member functions (and friends). The =delete constructor prevents this, and the compiler errors are nicer (usually along the lines of "the object can't be copied because the copy constructor was declared as deleted" rather than "undefined reference to ..." or "A::A is private within this context").

What Does THIS Do? C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is this weird colon-member syntax in the constructor?
C++ initialization
I have just received a header file in a C++ program, and I cannot figure out what this line of code does:
Card(Value faceValue=deuce, Suit suit = clubs):
suit(suit), faceValue(faceValue) {}
What does the : mean, and why does replacing it with a ; (as I thought I should) break the code?
Sorry for the generalness of this question, but could someone please explain the purpose of these two lines?
Thank you for your time.
This looks like a constructor for the Card class. The part after the : is an initializer list, initializing the values of member variables (or parent classes, but I don't think that's applicable in this case). The body of the constructor is empty because everything it needed to do was done in the initializer list.
The : and what follow is the initialization list. The reason you use it instead of assigning the member variables in the constructor's body is that if you do it inside the constructors body, the default constructor will be called first and then the copy constructor or assignment operator will be called afterwards. By using the initialization list you skip the first step.
Initialization lists. It's the preferred way to initialize class constructors in C++.
It is used because it allows the initialization of const members of the class without compilation error.

xcode - "attempt to use a deleted function" - what does that mean?

I am writing a C++ library in Xcode 4.2.
One of my classes won't compile with this error:
attempt to use a deleted function
There is no specific indication what function it's talking about.
I don't want to post the class code here, but does anybody have any idea what this error means?
I had a similar message with threads (C++11). It turned out that I was passing the wrong number of parameters to the function called by the thread so the thread did not find any function suitable and gave that message.
To add to Carlos' answer, I had the right number of arguments but one of the arguments was being passed by reference. Adding ref() around the variable fixed it for me. See here.
In C++11 you can declare functions as deleted:
struct Foo {
Foo(const Foo &) = delete;
};
Attempting to use such a function is an error. The purpose of doing this is so that, in this example, copy construction of this type is not possible. This is a more direct replacement for the non-copyable trick used pre-C++11.
Also, there are rules in the C++ spec that lead to member functions being implicitly deleted.
The error is telling you that your program attempts to use a deleted function. You'll have to post the error you're getting for more detailed help.
For me It solved it when I passed "this" pointer as a parameter to the function.
For me, the issue was that one of the arguments was a pointer, and I passed NULL directly as an argument. To solve this, I simply created a new NULL pointer which I passed to the function as an l-value instead.

How am I supposed to initialize this? [duplicate]

This question already has answers here:
this pointer to base class constructor?
(6 answers)
Closed 8 years ago.
I have an object in my class that only has one constructor, and this constructor takes a pointer to an interface implemented by my class. So I want to put it on the initializer list:
: thatObject(this)
But that gives me a warning and I can understand why giving a pointer to an unconstructed class is not a good idea. So the question is, what do I do? Should I do:
: thatObject(NULL)
{
thatObject = TheClass(this);
}
What is the proper way of dealing with this?
Thanks
If thatObject is guaranteed to never dereference the given pointer until after it's constructor is complete, then ignore or suppress the warning. If you don't feel safe guaranteeing that it will never in the future dereference the pointer, than go with the second option.
In MSVC the code to supress a warning is:
#pragma warning(supress:4355)
: thatObject(this)
GCC is more complicated: (and untested, I don't have GCC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winit-self"
: thatObject(this)
#pragma GCC diagnostic pop
In general, it's not a good idea to have a pointer to child in the father class. If you need to use that pointer to obtain some information or do something, write a virtual function in father, implement in child and call that.
If you really really have to do that, the method you suggested yourself works fine:
: thatObject(NULL)
{
thatObject = TheClass(this);
}