简体   繁体   中英

Expected primary expression before * C++

I can't figure out the error in the following code

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

The error is that primary expression is expected before * in the statement

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

also

firespell' was not declared in this scope

I think 2nd error is related to first. I don't know what concept I am missing. I have been following this link http://en.cppreference.com/w/cpp/language/dynamic_cast

kindly help.

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

replace by

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

or by

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

Also, I do not have idea how this segment was compiled.

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

You can not declare array size in runtime, use dynamic allocation ( malloc , new ) or some high-level containers

EDIT: Readability decrease in 2-nd code block is controversial statement.

From 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.

So, you can't have a declaration and a boolean convertible expression in a single if statement.

You have to define it before if :

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

If it is a c++ program then you can just write

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

}

The original code is not compiled because a declaration is an operand of an expression in the if statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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