【问题标题】:Expected primary expression before * C++* C++ 之前的预期主表达式
【发布时间】:2016-06-12 15:00:40
【问题描述】:

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

#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

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

请帮忙。

【问题讨论】:

    标签: c++ dynamic-cast


    【解决方案1】:
    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)或一些高级容器

    编辑:第二个代码块的可读性降低是有争议的声明。

    【讨论】:

    • 如果只在if语句的语句中需要变量,这是一种很好的编程风格。
    • @VladfromMoscow 但它降低了可读性,所以这是一个哲学问题,看起来像这样的变量定义有限制
    • @Inline,感谢您的帮助,您能否检查一下您的解决方案 if((Fireball *firespell=dynamic_cast(spell))!=NULL),,,这是针对第一个声明的解决方案if 语句中的布尔可转换表达式。 L[m+1][n+1] 也可以编译。
    • @Inline 我不同意它会降低可读性。它在使用它们的范围之外声明变量会降低可读性。L)
    • @DineshSashikanth 是的,在发布解决方案之前,我使用 Microsoft Visual Studio 2013 编译了代码。
    【解决方案2】:

    来自if statement

    条件[if (condition) statement_true] 之一

    • 一种可在上下文中转换为 bool 的表达式

    • 使用大括号或等号初始化器声明单个非数组变量。

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

    你必须在if之前定义它:

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

    【讨论】:

    • @Hurkyl 是真的 :)
    【解决方案3】:

    如果是c++程序,那么你可以写

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

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

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2018-11-27
      • 2022-01-01
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多