首页 > 解决方案 > '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

这里有什么问题?

标签: c++class

解决方案


您的main函数缺少括号。这个

int main{

应该

int main(){

此外,f1("A", 100, 100)不是构造函数调用,而是对 的调用operator(),而您没有。改为这样做:

Fighter f1("A", 100, 100);

此外,确保你的警卫是一致的。有一个#ifndef FIGHTER_H失踪。


推荐阅读