0

For some reason whenever I try running my code it always call the default constructor but it should be calling the constructor with parameters.

#include "pokemon.h"

 int main()
{
    int choice;

    cout<<"input 1 2 or 3"<<endl;
    cin>>choice;

    if(choice==1||choice==2||choice==3)
    {
        pokemon(choice);
    }

}

in my headerfile i have

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

class pokemon{
public:
    pokemon();//default constructor
    pokemon(int a);
    ~pokemon();//desconstructor
    pokemon(const pokemon& c);
    void train();
    void feed();
    bool isnothappy();
    string getName();//accessor for the name
    int getPowerlevel();//accessor for the power level
    string getColor();//accessor for the color
    string getType();//accessor
    int getHappylevel();//accessor
    static int getNumObjects();
    void set_type(string);//mutator
    void set_color(string);//mutator
    void set_power_level(int);//mutator
    void set_happy_level(int);//mutator
    void set_name(string);//mutator
private:
    string name;
    string color;
    string type;
    int power_level;
    int happy_level;
    static int numberobject;
};

and in my other .cpp file i have

int pokemon::numberobject=0;//initialize static member variable

pokemon::pokemon(){//default constructor
    name="pikachu";
    color="yellow";
    type="electric";
    power_level=0;
    happy_level=1;

    cout<<"The default constructor is being called"<<endl;
    ++numberobject;
}

pokemon::pokemon(int a)
{

    if(a==0)
    {
        name="Pikachu";
        color="yellow";
        type="electric";
        power_level=1;
        happy_level=1;
    }


    else if(a==1)
    {
        name="Bulbasaur";
        color="green";
        type="grass";
        power_level=1;
        happy_level=1;
    }


    else if(a==2)
    {
        name="Charmander";
        color="red";
        type="fire";
        power_level=1;
        happy_level=1;
    }

    else if(a==3)
    {
        name="Squritle";
        color="blue";
        type="water";
        power_level=1;
        happy_level=1;
    }

    cout<<"Congratulations you have chosen "<<getName()<<". This " <<getColor()<<" "<<getType()<<" pokemon is really quite energetic!"<<endl;
    ++numberobject;
}

pokemon::~pokemon()
{
    //cout<<"the destructor is now being called"<<endl;
    //cout<<"the number of objects before the destructor is "<<pokemon::getNumObjects()<<endl;
    --numberobject;
    cout<<"Now you have a total number of "<<pokemon::getNumObjects()<<endl;
}

pokemon::pokemon(const pokemon& c)//copy constructor
{
    name=c.name;
    color=c.color;
    type=c.type;
    power_level=c.power_level;
    happy_level=c.happy_level;
    ++numberobject;
}

I have both my constructors declared and defined in my other files but this darn thing always calls the default constructor

6
  • pokemon(choice); What does this line do? Commented May 28, 2015 at 21:52
  • @PaulMcKenzie pokemon(choice) doesnt even get excuted for some reason it just goes to the default constructor Commented May 28, 2015 at 21:55
  • Can you show us a snippet of pokemon.h Commented May 28, 2015 at 21:55
  • 1
    That's not what I really asked. What is that line supposed to do? How do you create objects in C++? Where is the instance of the pokemon object where that is being assigned? I don't see it. Shouldn't it look something like this: pokemon myObject(choice);? Also, please post pokemon.h. Commented May 28, 2015 at 21:56
  • 1
    Assuming pokemon is your class name, did you try pokemon a_pokemon(choice);? Commented May 28, 2015 at 22:01

1 Answer 1

3

This code:

pokemon(choice); 

means the same as:

pokemon choice;

It declares a variable called choice of type pokemon, and there are no arguments given to the constructor. (You're allowed to put extra parentheses in declarations in some places).


If you meant to declare a variable where choice is a constructor argument then you have to write:

pokemon foo(choice);

If you meant to create a temporary object (which will be immediately destroyed) with choice as argument, you can write (pokemon)choice;, or pokemon(+choice);, or since C++11, pokemon{choice};.


This issue with ambiguity between declarations and non-declarations can arise any time a statement begins with a type-name followed by (. The rule is that if it is syntactically correct for a declaration then it is treated as a declaration. See for other such cases.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.