简体   繁体   English

'class':非法使用这种类型作为表达式我该如何解决?

[英]'class': illegal use of this type as an expression how do I fix it?

When trying to compile my program I get these errors尝试编译我的程序时出现这些错误

1>c:\\users\\danilo\\desktop\\lab2\\project1\\project1\\main.cpp(11): error C2275: 'Fighter': illegal use of this type as an expression 1>c:\\users\\danilo\\desktop\\lab2\\project1\\project1\\fighter.h(9): note: see declaration of 'Fighter' 1>c:\\users\\danilo\\desktop\\lab2\\project1\\project1\\main.cpp(11): error C2146: syntax error: missing ')' before identifier 'f1' 1>c:\\users\\danilo\\desktop\\lab2\\project1\\project1\\main.cpp(12): error C2059: syntax error: '}' 1>c:\\users\\danilo\\desktop\\lab2\\project1\\project1\\main.cpp(12): error C2143: syntax error: missing ';' 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:语法错误:缺少“;” before '}'前 '}'

My main file looks like this:我的主文件如下所示:

#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);
}; 

and my Fighter.h looks like this我的 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

What is the problem here?这里有什么问题?

Your main function is missing its parentheses.您的main功能缺少括号。 This这个

int main{

should be应该

int main(){

Also, f1("A", 100, 100) isn't a constructor call, but a call to operator() , which you don't have.此外, f1("A", 100, 100)不是构造函数调用,而是对operator()的调用,而您没有调用它。 Do this instead:改为这样做:

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

Also, ensure that your guards are consistent.另外,请确保您的守卫是一致的。 There's a #ifndef FIGHTER_H missing.缺少#ifndef FIGHTER_H

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM