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

1> c:\ users \ danilo \ desktop \ lab2 \ project1 \ project1 \ main.cpp(11):错误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

10-06 15:39