-1
\$\begingroup\$

So, I needed to access variables from my main.cpp file in another cpp file, and the best solution for that (to me) was to make the main.cpp file a class.

Like this : (sorry for long codes...)

#include <iostream>
#include <SFML/Graphics.hpp> // Always use / instead of \ since \ is windows only!
#include <string>
#include <Box2D\Box2D.h>

#include "Player.h"
#include "Enemy.h"

class Game
{
public:

    sf::RenderWindow window;
    sf::Font font;
    sf::Clock clock;

    /// The number of enemies that ius going to spawn.
    const static int num_of_enemies = 2;

    /// Array of enemies on the map.
    Enemy enemies[num_of_enemies] = {

        Enemy(sf::Color(255, 0, 0, 255), sf::Vector2f(150, 150), sf::Vector2f(20, 20)),
        Enemy(sf::Color(255, 0, 0, 255), sf::Vector2f(225, 150), sf::Vector2f(20, 20))

    };

    // Window variables
    int WIDTH = 1024;
    int HEIGHT = 720;

    sf::Clock timeBetweenFrames;

    int main(int argc, char* argv[])
    {
        std::cout << "Loading..." << std::endl;

        if (!font.loadFromFile("Res/Fonts/arial.ttf"))
            std::cout << "Could not load font : arial.ttf" << std::endl;

        sf::Text text("I am text, not bread.", font, 15);

        sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "SFML Engine");

        Player player = Player(sf::Color(0, 255, 0, 255), sf::Vector2f(50, 50), sf::Vector2f(25, 25));

        std::cout << "Loading done" << std::endl;


        // Game Loop
        while (window.isOpen())
        {
            sf::Time time = timeBetweenFrames.getElapsedTime();
            //std::cout << (time.asSeconds()) << std::endl;

            timeBetweenFrames.restart().asSeconds();

            sf::Event event;
            while (window.pollEvent(event))
            {

                switch (event.type)
                {

                case sf::Event::Closed:
                    window.close();
                    break;

                }
            }

            if (window.hasFocus())
            {
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                {
                    player.move(-1, 0);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                {
                    player.move(1, 0);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                    player.move(0, -1);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                    player.move(0, 1);
                }

            }


            // Drawing
            window.draw(text);
            window.draw(player.getPlayer());

            for (int i = 0; i < num_of_enemies; i++)
                window.draw(enemies[i].getEnemy());

            window.display();
            window.clear();

        }

    }

};

However, when I try to launch the game, visual studio 2015 gives me this error :

1>------ Build started: Project: SFML_Game, Configuration: Debug Win32 ------
1>  main.cpp
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>C:\Users\MyName\Desktop\C++ Projects\SFML_Game\Debug\SFML_Game.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I did some research though, none of the answers on how to fix it worked on me.


Here's a list of things I tried :

  • Changing subsystem to console
  • Checked my linker settings (which seemed alright, I think....)

And I can't figure out more things that I need to do.

This has probably been asked before, though I can't seem to find anything that help my specific problem.

\$\endgroup\$
3
  • \$\begingroup\$ Please consider learning the language constructs and how to use them. Does it compile [y/N]? is a rather unproductive way of getting anything done. \$\endgroup\$ Dec 2, 2015 at 12:46
  • \$\begingroup\$ @LarsViklund What do you mean by constructs? (my english isn't too good... lol ) \$\endgroup\$
    – BiiX
    Dec 2, 2015 at 13:55
  • \$\begingroup\$ The pieces of functionality that together make up the language. A class has a particular structure of members. Not everything that is legal in file scope is legal in class scope. \$\endgroup\$ Dec 2, 2015 at 15:05

1 Answer 1

0
\$\begingroup\$

int main ( int argc, char* argv[] ) must remain as a global function.

If you want to access information somewhere else that you declaring in your main.cpp you may want to use extern. Try this:

in your main.cpp:

int WIDTH = 1024;
int HEIGHT = 720;

in your other file

extern int WIDTH;
extern int HEIGHT;
\$\endgroup\$
2
  • \$\begingroup\$ Ohh, okay! This will probably help! :D \$\endgroup\$
    – BiiX
    Dec 1, 2015 at 22:36
  • \$\begingroup\$ Uhm, another thing. Does extern "link" or just copy the variable? Example : if i change my "entern width" to 100, will my other files original width also change to 100? I'm guessing yes, otherwise it wouldn't really make sense. \$\endgroup\$
    – BiiX
    Dec 1, 2015 at 22:41

Not the answer you're looking for? Browse other questions tagged .