Domanda

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...

thx to everyone who answered and all comentators

(you are a little too numerous to thx one by one)

È stato utile?

Soluzione

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();
                        ^

Altri suggerimenti

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();

Change:

ZombieFetus band1();

to

ZombieFetus band1;

When instantiating an object with no arguments, you should not use parentheses.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top