为什么向导Wiz0;不等于向导Wiz0();在我的代码中?C2228错误
Why is Wizard wiz0; not equivalent to Wizard wiz0(); in my code? C2228 Error
可能的重复:
没有辩论的构造函数上没有语言标准的括号?
好吧,我有泡菜。这是一个错误C2228问题,我已经查看了其他问题和答案,而且给出的提示似乎对我来说是不起作用的 - 这是我的第一个问题,我是新手,所以请保持温柔!注意:如果我使用
,该程序将编译Wizard wiz0;
,但不是我使用
Wizard wiz0();
我正在使用的书告诉我,这两个陈述应该等效,所以我试图弄清楚为什么我可以使用一个而不是另一个。
首先,这是我尝试使用向导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
这是(我认为的)第5.cpp的相关代码是:
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();
另外,这是wiz.h文件的信息:
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;
...最后,来自wiz.cpp文件的信息!
//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
}
ph ...我认为这就是一切。如果你们能弄清楚我做错了什么,我会非常感谢!
Wizard wiz0;
实例化一个称为 wiz0
Wizard
对象 Wizard wiz0();
声明函数wiz0
返回 Wizard
乘值。这是最烦人的解析的简化版本。
这是解析C 的奇怪部分。带有括号的一个被解释为函数原型。您应该使用没有括号的一个。
相关文章:
- Metatrader 5 向导无法识别我的自定义交易信号模块
- VS2017 MFC类向导 - 如何在VS2015中使用它
- 编译向导生成的MFC应用程序时出错;缺少CDocument声明
- 为什么向导Wiz0;不等于向导Wiz0();在我的代码中?C2228错误
- 在 WTL 中使向导的"关闭"按钮灰显
- 使用属性表将向导定位在屏幕 win32 应用程序的中心
- 向导中的必填字段
- 如何从无法注册到函数向导的 xll 加载项调用函数
- Qt 向导后退按钮 UI 问题
- 使用 excel sdk 用 c++ 编码的 Excel 函数:我希望它在 excel 的函数向导中使用时不会触发计算
- 如何在Qt Creator项目向导中添加自定义构建步骤
- Eclipse C++ 项目创建向导中的"Finish"命令不起作用
- 为什么当我从向导在Visual Studio 2013中创建c ++类时,我不需要包含来自stdafx.h的标头
- Visual Studio 2015编译c++向导项目出错
- Visual Studio 2010:当创建新项目时,vs2010要打开向导的html文件
- 无法在mfc中看到添加变量向导
- visual studio 2013 c++应用向导无限循环
- 如何在Qt Creator 5.7中制作一个只有一个窗口的c++ GUI程序,但每次点击都会使窗口有其他元素-如设置向导
- 如何修改visual studio项目向导创建的窗口的标题
- 为什么VS2010向导在ctor和dtor中添加void类型