Search code examples
c++compiler-errorsvisual-c++-2010-express

Why is Wizard wiz0; not equivalent to Wizard wiz0(); in my code? C2228 Error


Possible Duplicate:
Is no parentheses on a constructor with no arguments a language standard?

Okay, I've got a pickle. It's an error C2228 issue, and I've looked at other questions and answers, and none of the tips given seem to work for me- this is my first question, and I'm a newbie, so please be gentle! Note: The program WILL compile if I use

Wizard wiz0; 

but NOT if I use

Wizard wiz0();

The book I am using tells me that these two statements should be equivalent, so I am trying to figure out why I can use one and not the other.

First off, here is the error when I attempt to use Wizard wiz0():

1>  Chapter5.cpp
1>Chapter5.cpp(14): error C2228: left of '.fight' must have class/struct/union
1>Chapter5.cpp(15): error C2228: left of '.talk' must have class/struct/union
1>Chapter5.cpp(17): error C2228: left of '.setArmor' must have class/struct/union
1>Chapter5.cpp(19): error C2228: left of '.getName' must have class/struct/union
1>Chapter5.cpp(21): error C2228: left of '.castSpell' must have class/struct/union

Here is (what I think) the relevant code from Chapter5.cpp is:

Wizard wiz0(); //declares a variable (wiz0) of type Wizard.

wiz0.fight();
wiz0.talk();

wiz0.setArmor(10);

cout << "Player's Name: " << wiz0.getName() << endl;

wiz0.castSpell();

Also, here is the information from wiz.h file:

public
//Constructor
Wizard();

//Overloaded Constructor
Wizard(std::string name, int hp, int mp, int armor);

//Destructor
~Wizard();

//Methods
void fight();
void talk();
void castSpell();
void setArmor(int mArmor);
std::string Wizard::getName();

private:
//Data members
std::string mName;
int mHitPoints;
int mMagicPoints;
int mArmor;

...and finally, information from the wiz.cpp file!

//Wiz.cpp implementation file

#include "stdAfx.h"
#include "wiz.h"
using namespace std;

//The Constructor call
Wizard::Wizard()
{
/*If the client calls a constructor without
specifying values, these will be the default
values that the program will use */
mName = "DefaultName";
mHitPoints = 1;
mMagicPoints = 1;
mArmor = 0;
}

Wizard::Wizard(std::string name, int hp, int mp, int armor)
{
//Client called constructor WITH values, so create an
//object with them.
mName = name;
mHitPoints = hp;
mMagicPoints = mp;
mArmor = armor;
}

void Wizard::fight()
{
    cout << "Fighting." << endl;
}

void Wizard::talk()
{
    cout << "Talking." << endl;
}

void Wizard::castSpell()
{
    if (mMagicPoints < 4)
        cout << "Casting spell." << endl;
    else
        cout << "Not enough MP!" << endl;
}

void Wizard::setArmor(int armor)
{
    if(armor >= 0)
        mArmor = armor;
} 

std::string Wizard::getName()
{
return mName;
}

Wizard::~Wizard()
{
//Not using dynamic memory- nothing to clean
}

Phew... I think that's everything. If you guys can figure out what it is I'm doing wrong, I'd greatly appreciate it!


Solution

  • It's a weird part of the way C++ is parsed. The one with parentheses is interpreted as a function prototype. You should use the one without the parentheses.