-2

I'm trying to make a simple sorting algorithm visualizer in C++ using the SFML library as the graphics engine, but I'm having some issues using global variables.

I keep getting an "[Variable] already defined in main.obj" error, but I don't know any other way to use the variable outside of main. If someone could help guide me to the right direction, that would be great.

Screenshot of the error output

main.cpp

#include <iostream>
#include <SFML/Graphics.hpp>

sf::RenderWindow window(sf::VideoMode(800, 600), "Visualized Sorting Algorithm");
int unsortedArray[] = { 66, 83, 343, 111, 500, 182, 46, 370, 480, 527, 266, 167, 163, 551, 462, 101 };
int arrLen = std::end(unsortedArray) - std::begin(unsortedArray);
int height = 1;
float unit = 800 / arrLen;
bool done = false;

int main() {

    std::cout << arrLen << std::endl;

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }


    }

    return 0;
}

setup.cpp

#include "setup.h"
#include "bar.h"
#include "main.cpp"

void Setup::visualizeArray() {
    for (int i = 0; i < arrLen; i++) {
        Bar bar;
        bar.width = unit - 1;
        bar.length = -unsortedArray[i] * height;
        bar.x = i * unit;
        bar.y = 600;
        window.draw(bar.draw());
        window.display();
    }
}

bar.h

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

class Bar {
public:
    float width;
    float length;
    float x;
    float y;
    float out;
    sf::Color white = sf::Color::White;
    sf::RectangleShape draw() {
        sf::RectangleShape bar(sf::Vector2f(width, length));
        bar.setPosition(sf::Vector2f(x, y));
        bar.setFillColor(white);
        return bar;
    }
};

I tried putting all the variables into a header file, but I still get the same error output.

nekloc
  • 3
  • 2
  • 2
    You have a setup.cpp that includes main.cpp? Can you explain what you expect this to do? – Sam Varshavchik Jun 20 '22 at 00:20
  • 4
    `#include "main.cpp"` is [almost always wrong](https://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header). If you must use global variables, you want there to be exactly one definition of them in all your translation units. That could mean a header file with some `extern` declarations that can be included in multiple source files. – Nathan Pierson Jun 20 '22 at 00:21
  • 1
    Why? You can write very large programs indeed in C++ without ever using global variables. I've written entire compilers that way. What makes you think you need them here? In this case both `height` and `unit` are constants that could be in the class, and the data arrays could be supplied as constructor parameters, starting out life in `main()` as local variables. – user207421 Jun 20 '22 at 00:38
  • @NathanPierson I made a header file with all global variables with ``extern``, I still get the same error. – nekloc Jun 20 '22 at 01:19
  • But did you remove `#include "main.cpp"`? And did you really get the same error? or a different error, as you mention below? – user207421 Jun 20 '22 at 01:29
  • @user207421 Yes. – nekloc Jun 20 '22 at 01:40
  • Yes what? Yes you removed it, yes you got the same error, or yes you got a different error? – user207421 Jun 20 '22 at 02:13

1 Answers1

3

Do not include main.cpp in setup.cpp. This is why it fails because the globals you define in main.cpp are now asso in setup.cpp.

In setup.cpp you will need to refer to the globals as 'extern'

eg

 extern int height;
Victor Eijkhout
  • 5,088
  • 2
  • 22
  • 23
pm100
  • 48,078
  • 23
  • 82
  • 145
  • Good answer. But of course having global variables is bad style. – Victor Eijkhout Jun 20 '22 at 00:56
  • I added extern the the variables in main and removed them from setup.cpp, but now I get an undeclared identifier. – nekloc Jun 20 '22 at 01:16
  • 1
    @nekloc Leave the variables declared in one file (main.cpp) and declare them as extern in every other file that needs them (setup.cpp). That way there are declared in exactly one place. – Jerry Jeremiah Jun 20 '22 at 01:25
  • @JerryJeremiah That seemed to work, thanks! But I still get a "already defined" error for the window variable. – nekloc Jun 20 '22 at 01:36
  • 1
    @nekloc if the variable is only defined in one place (the global in main.cpp) and is extern everywhere else then it only is defined once. So you must have missed a spot. Maybe you can edit your question with your updated code? – Jerry Jeremiah Jun 20 '22 at 01:55