This question already has answers here:
Closed 14 years ago.
What does it mean to call a class like this:
class Example
{
public:
Example(void);
~Example(void);
}
int main(void)
{
Example ex(); // <<<<<< what is it called to call it like this?
return(0);
}
Like it appears that it isn't calling the default constructor in that case. Can someone give a reason why that would be bad?
Thanks for all answers.
Currently you are trying to call the default constructor like so.
Example ex();
This is not actually calling the default constructor. Instead you are defining a function prototype with return type Example and taking no parameters. In order to call the default constructor, omit the ()'s
Example ex;
This declares a function prototype for a function named ex, returning an Example! You are not declaring and initializing a variable here.
Does it even compile? Anyway, see this related topic.
As has been noted Example ex(); declares a function prototype. Not what anyone would expect. This C++ wart will be fixed by the new C++0x standard. In the future the preferred syntax will be Example ex{};. The new uniform construction has many other nice features, see more here.
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem โ and include valid code to reproduce it โ in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to implement a Menu system for a school project, where I have a STL Map containing function pointers in the form of
map<string, YMenu>
When I try to call a member function from within YMenu to add a new function pointer to a vector through the map, I get an error saying that it's an "invalid use of void expression." I have attached the relevant code below.
Vector holding arrays:
vector<void (*) () > nextAction;
Function to add function pointers:
void YMenu::addNextAction(int index, void (*Action)() )
{
nextAction[index] = Action;
}
Syntax used when calling member function which generated the Error:
Menus["0.0"].addNextAction(1, Menus["0.1"].show());
Any ideas for possible solutions? I have tried to find on google and searching here but can't seem to find an answer. :/
SOLVED: Thanks guys, apparently I made some stupid mistakes back there. I have finally managed to implement the code and get it to compile and run by using std::function and std::bind().
The type of Menues["0.1"].show() isn't a function pointer. It is the result of calling show() on a specific object. It seems, your show() function returns int. You won't have much fun trying to shove the function into a void(*)() in any shape or form!
What you are probably looking for is std::function<void()> which can be used to invoke a nullary function object, i.e., something which can be called without a parameter. The function you want to pass seems to be a member function and as such it actually has a parameter, the implicitly passed this pointer. You also want to ignore the return type which std::function<void()> will happily to do for you. That is, your nextAction vector would be declared as
std::vector<std::function<void()> nextAction;
However, to actually add the show function with the correct object, you need to also construct a suitbale std::function<void()> object: since your member function needs an object, you'll need to bind the object to the function, e.g.:
Menues["0.0"].addNextAction(1, std::bind(&YMenu::show, &Menus["0.1"]));
Also note that using nextAction[index] is only valid if index < nextAction.size() prior to this operation, i.e., std::vector<T> doesn't automatically resize to the proper size to accommodate a new index: that has to be explicitly for std::vector<T>.
With Menus["0.1"].show() you are calling the function.
You also can't store pointers to member functions like you try to do, unless you actually have the object instance when you finally call the member function pointer. I suggest you look into std::function and std::bind:
std::vector<std::function<void()> nextAction;
nextAction.push_back(std::bind(&YMenu::show, &Menus["0.1"]));
Instead of using C-style function pointers, use a combination of std::function and std::bind as follows:
std::vector<std::function<void()> > nextAction;
void YMenu::addNextAction(int index, std::function<void()> Action)
{
nextAction[index] = Action;
}
Menus["0.0"].addNextAction(1, std::bind(&YMenu::show, &Menus["0.1"]));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I used clang3.3 with netbeans on linux. All in C++11. And I have a question about initialization
int main()
{
int i();
}
The following code is compiled but not work properly. This value will not defined by debugger and cannot be printable. I wanted describe int with default value. Instead I can write "int{}" and it will be a perfect default initialization. But I want understand what I wrote here, just want.
Second question. Its about lambda. I want to know how lamda can be described without auto keyword.
auto lambda = [&]()mutable->int{};
Simple, what I can write here instead auto and compiler will not give me an error ? I just want understand.
Ad 1.
You've been bitten by the most vexing parse. Basically, C++ grammar causes ambiguities between statements and declarations in certain cases. In such cases, the input is interpreted as a declaration. Since int i() can be interpreted as an integer variable definition, or a function declaration, it is interpreted as a declaration of parameterless function i, returning int.
Ad 2.
As for the second question, C++11 Standard ยง5.1.2/3 says it all:
The type of the lambda-expression (...) is a unique, unnamed non-union class type โ called the closure type (...)
So, there is no way to refer to it other than using auto.
Thats not a variable default initialization, its a function declaration, thanks to most-vexing-parse.
In a few words, the standard says if an expression can be evaluated as a function declaration, or as something else, it will be evaluated as a function declaration.
In your case, a function a without parameters and int as return value.
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.
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.