* C++ 之前的预期主表达式

Expected primary expression before * C++

我无法找出以下代码中的错误

#include <iostream>
#include <vector>
using namespace std;

class Spell { 
    private:
        string scrollName;
    public:
        Spell(): scrollName("") { }
        Spell(string name): scrollName(name) { }
        virtual ~Spell() { }
        string revealScrollName() {
            return scrollName;
        }
};

class Fireball : public Spell { 
    private: int power;
    public:
        Fireball(int power): power(power) { }
        void revealFirepower(){
            cout << "Fireball: " << power << endl;
        }
};


class SpellJournal {
    public:
        static string journal;
        static string read() {
            return journal;
        }
}; 
string SpellJournal::journal = "";

void counterspell(Spell *spell) {
    if((Fireball *firespell=dynamic_cast<Fireball*>(spell))!=NULL)
    {
    firespell->revealFirepower();

}

 else     
    {


    string scname=spell->revealScrollName();
    int m = scname.size();
    int n = SpellJournal::journal.size();
    int L[m+1][n+1];
    for(int i=0; i<=m; i++)
    {
        for(int j=0; j<=n; j++)
        {
            if(i==0 || j==0)
                L[i][j] = 0;
            else if(scname[i-1]==SpellJournal::journal[j-1])
                L[i][j] = L[i-1][j-1]+1;
            else
                L[i][j] = max(L[i-1][j],L[i][j-1]);
        }
    }
    cout<<L[m][n];
}

}

class Wizard {
    public:
        Spell *cast() {
            Spell *spell;
            string s; cin >> s;
            int power; cin >> power;
            if(s == "fire") {
                spell = new Fireball(power);
            }

            else {
                spell = new Spell(s);
                cin >> SpellJournal::journal;
            }
            return spell;
        }
};

int main() {
    int T;
    cin >> T;
    Wizard Arawn;
    while(T--) {
        Spell *spell = Arawn.cast();
        counterspell(spell);
    }
    return 0;
}  

错误是主表达式应该在语句中的 * 之前

if((Fireball *firespell=dynamic_cast<Fireball*>(spell))!=NULL)

还有

firespell' was not declared in this scope

我认为第二个错误与第一个错误有关。我不知道我缺少什么概念。我一直关注这个 link http://en.cppreference.com/w/cpp/language/dynamic_cast

请帮忙。

来自if statement

Condition [if ( condition ) statement_true] one of

  • one of expression which is contextually convertible to bool

  • declaration of a single non-array variable with a brace-or-equals initializer.

因此,您不能在单个 if 语句中声明 布尔可转换表达式。

必须先定义if:

Fireball* firespell = dynamic_cast<Fireball*>(spell);
if (firespell != nullptr)
    //Do something
if((Fireball *firespell=dynamic_cast(spell))!=NULL)

替换为

if(Fireball *firespell = dynamic_cast<Fireball*>(spell))

 Fireball *firespell;
 if( ( firespell = dynamic_cast(spell) ) != nullptr)

此外,我不知道这个片段是如何编译的。

    int m = scname.size();
    int n = SpellJournal::journal.size();
    int L[m+1][n+1];

你不能在运行时声明数组大小,使用动态分配(mallocnew)或一些高级容器

编辑: 第二个代码块的可读性下降是有争议的说法。

如果是c++程序就直接写

void counterspell(Spell *spell) {
    if( Fireball *firespell=dynamic_cast<Fireball*>(spell) )
    {
    firespell->revealFirepower();

}

原始代码未编译,因为声明是 if 语句中表达式的操作数。