2014-09-18 196 views
0

所以我创建这个类包含一个字符串,类处理创建精灵设置他们的图标等等,但我遇到了一个错误。字符串常量之前的预期标识符C++错误

下面是类代码:

class staticmob{ 
public: 
    sf::Sprite icon; 
    sf::Texture iconTexture; 
    std::string object_name; 
    bool density = false; 


    staticmob(sf::Sprite mIcon, 
      std::string mName, 
      std::string fileName, 
       const bool dense, 
       bool inObjList, 
       turf *object_list); 

};

错误所在:

staticmob midGround(sf::Sprite midGround, 
       "Ground", 
       "tileset.png", 
       true, 
       true, 
       background); 

错误:

error: expected identifier before string constant 
error: expected ',' or '...' before string constant 

任何的帮助深表感谢(是的,我略低在C++新手,但我得到的窍门它)

+0

什么是'midGround'咋办至 是?你在两个变量声明中使用了它的名字? – 2014-09-18 21:45:48

+0

通过const引用传递非基本类型以避免副本。 – 2014-09-18 23:39:23

回答

0

你的错误是类似于如果你编译你会看到:

void foo(int, int) {} 

int main() 
{ 
    foo(int i, 0); // "int i" is not an expression. It is not a declaration either. 
    return 0; 
} 

你需要的是沿着线的东西:

sf::Sprite midGroundSprit; 
staticmob midGround(midGroundSprite, 
        "Ground", 
        "tileset.png", 
        true, 
        true, 
        background); 
0

在您构建staticmob midGround,您重新声明了第一个参数(sf :: Sprite part - 复制/粘贴错误?)的类型,并且在注释中还有一个名称冲突(是你想声明的staticmob的midGround,还是sf :: Sprite实例?)假设midGroundSprite实际上是sf :: Sprite的名称,这样的事情应该可以工作:

staticmob midGroundMob(midGroundSprite, "Ground", ... etc.) 
相关问题