Wissam Abdelghany
Wissam Abdelghany

Reputation: 13

Expected Identifier before string constant C++ error

So i'm creating this class which contains a string, the class handles creating sprites setting their icon etc etc but I'm running into an error.

Here's the class code:

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);

};

where the error is:

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

the error:

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

any help is much appreciated (yes, i'm slighty a newbie in C++ but i'm getting the hang of it)

Upvotes: 0

Views: 6777

Answers (2)

R Sahu
R Sahu

Reputation: 206627

Your error is similar to what you will see if you compile:

void foo(int, int) {}

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

What you need is something along the lines of:

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

Upvotes: 1

Corbell
Corbell

Reputation: 1393

In your construction of staticmob midGround, you are re-declaring the type of the first parameter (the sf::Sprite part - copy/paste error?), and there's also a name conflict as touched on in the comments (is midGround the staticmob you want to declare, or is it the sf::Sprite instance?) Assuming midGroundSprite is actually the name of the sf::Sprite, something like this should work:

staticmob midGroundMob(midGroundSprite, "Ground", ... etc.)

Upvotes: 0

Related Questions