c++ - 'class':非法使用这种类型作为表达式我该如何解决?
问题描述
尝试编译我的程序时出现这些错误
1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(11): error C2275: 'Fighter': 非法使用这种类型作为表达式 1>c:\users\danilo\desktop\ lab2\project1\project1\fighter.h(9): 注意: 见'Fighter' 1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(11) 的声明: 错误 C2146: 语法错误: 在标识符 'f1' 1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(12) 之前缺少 ')':错误 C2059:语法错误:'}' 1>c:\users\ danilo\desktop\lab2\project1\project1\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);
};
我的 Fighter.h 看起来像这样
#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
失踪。
推荐阅读
- python - 从 input_t(实际上是三个数字并充当特征)和 output_t 作为输出创建一个数据框
- javascript - JS文件无法访问laravel变量
- three.js - 如何在three.js上通过addEventListener动态更新set.position.x?
- geolocation - 查看和地理位置字段
- mysql - 为什么函数 count() 与 gorup by 出错?
- sql-server - EF Core 错误的连接语句
- nest-thermostat - 试图从新的 google nest e 恒温器中获取详细的数据转储
- java - 使用 volley 解析 json 时遇到问题
- openwhisk - Openwhisk 中是否有设置最小/最大/固定功能/动作副本的功能?
- c++ - C 和 C++ 标准中“表达式”的定义