0

I am a new visual c++ and SFML user (11). I was making a tiled platformer.

There was an "Enemy" class in my project. I was reading the map from something like that: string levelOne [14] = {/* ... */};

And when I tried to use the variable levelOne I got an error that it is an undeclared identifier. I tried initializating the variable in the header file , where I needed levelOne and not including the header with the variable, but I got the same error. And when I was making a very similar function of a Player class, it worked!But not with the Enemy class. Files: main.cpp Headers: player.h (with an inline Player class) platform.h (with an inline Platform class) enemy.h (with an inline Enemy class and two stupid errors about undeclared identifiers) maps.h (with the levelOne variable and some functions for setting up the level)

//Enemy.h
#pragma once
#ifndef ENEMY_H
#define ENEMY_H
#include "SFML\Graphics.hpp"
#include "maps.h"
class Enemy {
    public:
        //...
void checkCollisionByX(){

    for (int i = 0;i < 14;i++)
    {
        for (int j = 0;j < 86; j++)
        {
            if (levelOne[i][j] == '-'){
                if (!standing){
                if (right){
                    if (((j * 50 <= x + width) && (x + width <= j * 50 + 50)) && (((i * 50 <= y) && (y <= i * 50 + 50)) || ((i * 50 <= y + height) && (y + height <= i * 50 + 50)))){
                        x = x - (x + width - j*50);
                        left = true;
                        right = false;
                        return;
                    }
                } else if (left){
                    if (((j * 50 <= x) && (x <= j * 50 + 50)) && (((i * 50 <= y) && (y <= i * 50 + 50)) || ((i * 50 <= y + height) && (y + height <= i * 50 + 50)))){
                        x = x + (j*50 + 50 - x);
                        left = false;
                        right = true;
                        return;
                    }
                }
                }
            }
        }
    }
}
bool checkCollisionByY(){
    bool onground = false;
    for (int i = 0;i < 14;i++)
    {
        for (int j = 0;j < 86; j++)
        {
            if (levelOne[i][j] == '-'){
                if (((j * 50 <= x + width / 2) && (x + width / 2 <= j * 50 + 50)) && ((i * 50 <= y + height) && (y + height <= i * 50 + 50))){
                    y = y - (y + height - i * 50) - 2;
                    onground = true;
                }
                if (((j * 50 <= x + width / 2) && (x + width / 2 <= j * 50 + 50)) && ((i * 50 <= y) && (y <= i * 50 + 50))){
                    y = y + (y - (i*50 + 50));
                }
            }
        }
    }
    return onground;
}
    private:
    float x, y, speed;
    int animCount, width, height;
    bool right, left, standing, onGround;
    sf::Texture texture;
    sf::Sprite sprite;
}
#endif ENEMY_H

maps.h

#pragma once

#ifndef MAPS_H
#define MAPS_H

#include <SFML\Graphics.hpp>
#include "platform.h"
#include "enemy.h"
using namespace std;

Platform platformss [10000];
Enemy enemiess [250];
string levelOne [14] = {"-------------------------------------------------                            -------------------------------------",
"-                              ---          -        -                               -",
"-                             -            ---      ---                              -",
"-                              ---          -        -                               -",
"-                                                                                    -",
"-                                                                                    -",
"-                                                                                    -",
"-                                     E                                              -",
"-                   -- -- -- -- -- -- -- -- -- -- -- -- -- -- --                     -",
"-               --                                               --                  -",
"-             -                                                    -                 -",
"-            --                                     -               --               -",
"-           ---      E        -    --       E       -        E                       -",
"--------------------------------------------------------------------------------------"};


int setupLevelOne(){
/* ... */
}
int getEnemyNumber(){
    /* ... */
}

#endif MAPS_H

Build output:

1>------ Build started: Project: SFML PRACTICE, Configuration: Debug Win32         ------
1>Build started 09.08.2019 21:18:06.
1>ClCompile:
1>  main.cpp
1>c:\users\suzer\appdata\local\programs\python\python35-        32\include\kostya_test_project\c++\hellosfml\hellosfml\enemy.h(82): error C2065: 'levelOne' : undeclared identifier
1>c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\enemy.h(110): error C2065: 'levelOne' : undeclared identifier
1>c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\maps.h(34): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\maps.h(55): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\player.h(116): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\player.h(140): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\main.cpp(30): warning C4244: 'initializing' : conversion from 'sf::Int32' to 'float', possible loss of data
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.87
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The errors:

Error   1   error C2065: 'levelOne' : undeclared identifier c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\enemy.h    82  1   SFML PRACTICE
Error   2   error C2065: 'levelOne' : undeclared identifier c:\users\suzer\appdata\local\programs\python\python35-32\include\kostya_test_project\c++\hellosfml\hellosfml\enemy.h    110 1   SFML PRACTICE

Such stupid errors!Why can't it see the "string levelOne[14] ..." And how do I fix them?

Thank you.

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • 2
    You have a circular dependency between maps. h and enemy.h . Note: It is usually a very bad idea to define variables in headers. It almost certainly leads to a violation of the One Definition Rule. – user4581301 Aug 09 '19 at 19:11
  • Some reading on the problem you can use to better understand the problem and hopefully as inspiration to refactor your code to avoid the problem: [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – user4581301 Aug 09 '19 at 19:15
  • This has nothing to do with the compiler you are using (or the SFML library). – Jesper Juhl Aug 09 '19 at 19:16

0 Answers0