Ich kann den Fehler nicht in dem folgenden Code herauszufindenErwartete primären Ausdruck vor * 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;
}
Der Fehler wird, dass die primäre Ausdruck wird in der Aussage vor * erwartet
if((Fireball *firespell=dynamic_cast<Fireball*>(spell))!=NULL)
auch
firespell' was not declared in this scope
Ich denke, 2. Fehler bezieht sich auf den ersten. Ich weiß nicht, welches Konzept mir fehlt. Ich bin diesem Link gefolgt http://en.cppreference.com/w/cpp/language/dynamic_cast
bitte helfen.
Einige Leser finden es glücklicher als unglücklich.:) – Hurkyl
@Hurkyl Das ist wahr :) – Rakete1111