C++ a value of type "SnakeHead(*)()" cannot be used to initialize an entity of type "SnakeHead*"

Viewed 59

I'm having a bit of trouble with sfml and the error that is the title is currently being thrown at me.

This is the class I have created

#include <SFML/Graphics.hpp>

#ifndef SNAKEHEAD_H
#define SNAKEHEAD_H
class SnakeHead
{
private:
    int iPosX;
    int iPosY;
public:
    sf::RectangleShape snakeHead;
    SnakeHead();
    SnakeHead(int iX, int iY);
    bool wallCheck();
    bool collideCheck(int iX, int iY);
    void move(char cMovement);
    sf::RectangleShape createSquare();

};
#endif

This is the important parts of the main code

SnakeHead player1();
SnakeHead* player2 = player1;

On a side note, I only tried to do this to fix a different issue. The other error I got was: "expression must have a class type"

Here is the important part of the class functions

sf::RectangleShape SnakeHead::createSquare()
{
    snakeHead.setSize(sf::Vector2f(20.f, 20.f));
    snakeHead.setOrigin(0.f, 0.f);
    snakeHead.setPosition(iPosX, iPosY);
    snakeHead.setFillColor(sf::Color::Red);
    return snakeHead;
}

Here is the important part of the main code

window.draw(player1.createSquare());
1 Answers

Your following code doesn't do what you expect:

SnakeHead player1();

The code above declares a function, player1(), that takes no arguments and returns a SnakeHead object by value. This is due to C++'s most vexing parse.

You could use curly braces instead of parenthesis for creating a SnakeHead object, player1:

SnakeHead player1{};

Or simply remove the parenthesis at all:

SnakeHead player1;
Related