"class":非法使用此类型作为表达式 如何解决?
'class': illegal use of this type as an expression how do I fix it?
尝试编译我的程序时,出现以下错误
1>c:\用户\danilo\桌面\lab2\project1\project1\main.cpp(11(:错误 C2275:"战斗机":非法使用此类型作为表达式 1>c:\users\danilo\desktop\lab2\project1\project1\fighter.h(9(:注:参见"Fighter"声明 1>c:\用户\danilo\桌面\lab2\项目1\项目1\main.cpp(11(:错误 C2146:语法错误:标识符"f1"之前缺少"(" 1>c:\用户\danilo\桌面\lab2\project1\project1\main.cpp(12(:错误 C2059:语法错误:"}" 1>c:\用户\danilo\桌面\lab2\项目1\项目1\main.cpp(12(:错误 C2143:语法错误:在"}"之前缺少";">
我的主文件如下所示:
#include "Fighter.h"
#include "Spell.h"
#include "Player.h"
#include "Wizard.h"
#include "Collection.h"
int lastID = 0;
int main{
Fighter f1;
f1("A", 100, 100);
};
我的战斗机看起来像这样
#define FIGHTER_H
#include "Card.h"
#include <string>
#include <iostream>
using namespace std;
class Fighter : public Card {
int power;
public:
virtual string getCategory() const override {
return "FIGHTER";
}
int getPower() const {
return power;
}
Fighter(string Name_, int neededEnergy_, int power_) : Card(Name_, neededEnergy_), power(power_) {}
void operator>(const Fighter& f) const {
if (this->getPower() > f.getPower()) {
cout << this->getName() << " is stronger " << endl;
}
else {
cout << f.getName() << " is stronger " << endl;
};
}
virtual void write(ostream& it) const override {
it << "(power: " << getPower() << ")";
}
};
#endif FIGHTER_H
这里有什么问题?
您的main
函数缺少括号。这
int main{
应该是
int main(){
此外,f1("A", 100, 100)
不是构造函数调用,而是对operator()
的调用,而您没有。请改为执行以下操作:
Fighter f1("A", 100, 100);
此外,请确保您的防护装置保持一致。缺少一个#ifndef FIGHTER_H
。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 两个文件使用彼此的功能-如何解决
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 在java中解决这段代码时面临循环中的问题
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 难以理解某些人解决IOI问题的源代码
- visual c++,如何获取解决方案目录中的代码
- 如何解决错误:SCIP C++中的 SCIP 阶段无效 <10>
- 节俭并发:未解决的外部问题
- IpOpt拒绝解决不受约束的问题
- 如何解决这个超硬恒星的创造问题
- 循环无限运行C++解决骑士之旅问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- 为什么这段代码不起作用,我该如何解决?