Reputation: 1103
I have this code :
#include <iostream>
class ZombieFetus{
private:
public:
ZombieFetus();
};
ZombieFetus::ZombieFetus(){
std::cout << "http://www.metal-archives.com/band/view/id/55878" << std::endl;
};
class FaceOfAVirus{
private:
public:
FaceOfAVirus(int);
};
FaceOfAVirus::FaceOfAVirus(int i){
std::cout << "http://www.metal-archives.com/band/view/id/74239" << std::endl;
};
int main(int argc, char **argv){
std::cout << "some random bands :" << std::endl;
ZombieFetus band1();
FaceOfAVirus band2(0);
}
to compil :
$ g++ main.cc -Wall
When I run it I got :
some random bands :
http://www.metal-archives.com/band/view/id/74239
what the heck with ZombieFetus band1();
? what does the program ? it's sound to be a beginner question, if it's already answered on stackoverflow, plz give me the link... I don't find the answer...
(you are a little too numerous to thx one by one)
Upvotes: 3
Views: 586
Reputation: 158599
The problem is that this:
ZombieFetus band1();
is a intepreted as a function declaration, you have two possible fixes in C++11:
ZombieFetus band1{} ;
or pre C++11:
ZombieFetus band1;
clang
is a little more helpful here and warns:
warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
ZombieFetus band1();
^
Upvotes: 8
Reputation: 324
ZombieFetus band1();
declares a function named band1, which takes no parameter and returns a value of type ZombieFetus
.
If you want to used the default constructor, 'ZombieFetus band1;' will be fine.
Hope this will help.
Upvotes: 2
Reputation: 326
Change:
ZombieFetus band1();
to
ZombieFetus band1;
When instantiating an object with no arguments, you should not use parentheses.
Upvotes: 2
Reputation: 14718
The default constructor does not take parameters, so remove the () like
ZombieFetus band1;
and you get
make -k x; ./x
g++ x.cc -o x
some random bands :
http://www.metal-archives.com/band/view/id/55878
http://www.metal-archives.com/band/view/id/74239
But this is a "forward" declaration of a function band1 which returns ZombieFetus
ZombieFetus band1();
Upvotes: 3