I'm trying to make a snake game using a linked list in C++ and SFML; I have no idea why I get the error "redefinition, different basic types"

Viewed 42

Here's the code for Snake.h (not nearly finished)

#pragma once
#include <SFML/Graphics.hpp>

#define MAX 100

using namespace sf;

class Snake
{
public:
    
    struct node {
        node* next;
        node* prev; 
        int order;
        float m_PositionX;
        float m_PositionY;
        RectangleShape m_Square;
    };
    node* createNewBlock(int x, int y, node* next);
    Snake();
    

};

class food
{
private:
    Vector2f m_Position;
    RectangleShape m_Sprite;
public:
    void spawnFood();
    Vector2f getPosition();
    
};

class floor
{
private:
    int arr[10][10];
};

And here's the code for Snake.cpp

#include "Snake.h"
#include <SFML/Graphics.hpp>

using namespace sf;

node* Snake::createNewBlock(int x, int y, node* next)
{
    node* newNode = new node*;
    newNode->order = next->order + 1;
    newNode->m_PositionX = x;
    newNode->m_PositionY = y;
    return newNode;
}

Snake::Snake()
{
    struct node* head;
}

Here's the main.cpp

#include <SFML/Graphics.hpp>
#include "Snake.h"

using namespace sf;

int main()
{
    Snake snake;
}

I tried to use the built-in "help" (screwdriver thing) in visual basic to define the function, but there's still an error? I've tried taking out the struct parts of the declaration and definition, using new instead of malloc(sizeof()),

EDIT: Here's the debug information

'm_PositionX': undeclared identifier LINE 13
'm_PositionY': undeclared identifier LINE 18
'Snake::createNewBlock': redefinition; different basic types LINE 26
'node *Snake::createNewBlock(int,int,Snake::node *)'overloaded function differs only by return type from 'Snake::node *Snake::createNewBlock(int,int,Snake::node *) LINE 27
'head': unreferenced local variable LINE 8
'=' conversion from 'int' to 'float', possible loss of data LINE 30
'=' conversion from 'int' to 'float', possible loss of data LINE 30
identifier "node" is undefined LINE 6
a value of type "Snake::node **" cannot be used to initialize an entity of type "Snake::node" LINE 8
declaration is incompatible with "Snake::node *Snake::createNewBlock(int x, int y, Snake ::node *next)" (declared at line 20 of (this is just the directory where "Snake.h" is) LINE 6

Sorry if it looks bad. C++ isn't my forte, nor is any language, but I'm trying to learn it. In short, I was trying to use a linked list for the snake to work on data structure skills.

I used this website to learn (it uses malloc() sometimes so I thought the difference between new and malloc() wouldn't be too bad). However, I am sorry for using malloc(), because well, I DID read a book that uses new instead.

https://www.programiz.com/dsa/linked-list

EDIT2: I now realize that malloc() should only be used for C and not C++. I didn't get that the first time around.

EDIT3: When I went to make a minimal reproducible example, I semi-fixed it...somehow. I just put the definition of the node* inside of the Snake class. Something weird happens when I declare it in the Snake class and define it outside of it in a separate cpp file.
Here's the code I got:

class Snake
{
public:
    struct node
    {
        node* next;
        node* prev;
        int order;
        float Xpos;
        float Ypos;
    };
    node* createNewNode(int x, int y, node* next)
    {
        node* newNode;
        newNode = new node;
        newNode->order = next->order + 1;
        newNode->Xpos = x;
        newNode->Ypos = y;
    }
};

works fine, but this:

snake.h:

#pragma once
#include <SFML/Graphics.hpp>

#define MAX 100

using namespace sf;

class Snake
{
public:
    struct node
    {
        node* next;
        node* prev;
        int order;
        float Xpos;
        float Ypos;
    };
    node* createNewNode(int x, int y, node* next);
};

snake.cpp

#include "Snake.h"
#include <SFML/Graphics.hpp>

using namespace sf;

node* Snake::createNewNode(int x, int y, node* next)
{
    node* newNode;
    newNode = new node;
    newNode->order = next->order + 1;
    newNode->Xpos = x;
    newNode->Ypos = y;
}

doesn't.

0 Answers
Related