gpt4 book ai didi

c++ - '类' : illegal use of this type as an expression how do I fix it?

转载 作者:行者123 更新时间:2023-11-28 01:14:18 26 4
gpt4 key购买 nike

在尝试编译我的程序时出现这些错误

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): error C2146: 语法错误: 在标识符 'f1' 前缺少 ')'1>c:\users\danilo\desktop\lab2\project1\project1\main.cpp(12): error 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

关于c++ - '类' : illegal use of this type as an expression how do I fix it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com