-2

enter image description hereOk first off I have a feeling that what ever I did is ridiculously stupid but I can't seem to figure it out.

Any way I am new to C++ so thanks for teaching me.

I declared a public typedef struct in a header file.

I use it in the header file but never use it in my main. For some reason the file with my main is throwing a error that some aspects of it are never defined but I don't define them because I don't use the,

there's a good bit more but its irrelevant

Main.cpp just has sfml code that opens a window and runs a few voids bassed on user input. Those voids do use the structure but never Main.cpp. Can someone help me fix the error.

Thanks sorry if the answer is easy and this is a waste of time.

My Simplified SFML Code

    typedef struct KeyStruct {
        static sf::Image Img;
        static sf::Texture Tex;
        static sf::Sprite Sprite;
    }Key;

static void DrawKey(string key)
    {
        sf::Clock clock;
        //Declair Key
        Key Key;
        Key.Img.loadFromFile("Assets/Images/A.png");
        if (key == "A")
            Key.Img.loadFromFile("Assets/Images/A.png");
        else if (key == "D")//ect
        Key.Tex.loadFromImage(Key.Img);
        Key.Sprite.setTexture(Key.Tex);

        //Open Window
        sf::RenderWindow window(sf::VideoMode(Key.Img.getSize().x, Key.Img.getSize().y, 32), "Key", sf::Style::None);

        //Handle Events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        //While Open Loop
        while (window.isOpen())
        {
            window.clear(sf::Color::Transparent);
            window.draw(Key.Sprite);
            window.display();

        }
    }

Main

int main()
{
    sf::RenderWindow window(sf::VideoMode(100, 100, 32), "Main Window", sf::Style::None);
    while (window.isOpen())
    {
        sf::Event event;
        //Handle Events
        while (window.pollEvent(event))
        {
            //Key Presses
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::A)
                    MakeKey::DrawKey("A");
                else if (event.key.code == sf::Keyboard::D)
                    MakeKey::DrawKey("D");//ect
            }
            //Close
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::Transparent);
        window.display();
    }
    return EXIT_SUCCESS;
}
Jacob Krumholz
  • 43
  • 1
  • 1
  • 9
  • 1
    Can you try to reduce your code to create an example we could throw straight into a compiler and get the arrors you suggest? –  Mar 20 '20 at 18:35
  • Honestly I feel like doing that would be hard but i can easily just plug in my sfml code if u want – Jacob Krumholz Mar 20 '20 at 18:47
  • I wouldn't necessarily suggest doing that. Rather try to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) if you can. –  Mar 20 '20 at 18:49
  • sorry I said that then proceeded to do it lmao – Jacob Krumholz Mar 20 '20 at 18:55
  • What does the actual error message say? –  Mar 20 '20 at 18:59
  • You're using `typedef struct` (where the typedef is redundant anyway in C++) to reinvent namespaces... badly. C++ supports namespaces directly. – Ben Voigt Mar 20 '20 at 18:59
  • I added a screenshot. Thanks for your effort in helping me. – Jacob Krumholz Mar 20 '20 at 19:09
  • If i'm doing it badly how should I go about doing it good? I can easily make it a new namespace idk y that didn't occur to me. – Jacob Krumholz Mar 20 '20 at 19:12

1 Answers1

0

Clearly you are using them, otherwise the linker would not be complaining about missing definitions.

In your Main.cpp file add the following lines.

sf::Image KeyStruct::Img;
sf::Texture KeyStruct::Tex;
sf::Sprite KeyStruct::Sprite;

But if you are absolutely sure you are not using them, then why not delete them from the struct and see what happens.

john
  • 85,011
  • 4
  • 57
  • 81
  • I am using them just not in the file that throws the error – Jacob Krumholz Mar 20 '20 at 18:56
  • Unresolved references are a linking error. They apply to the program as a whole not to any individual file. Have you tried my suggestion? It should solve your problem (despite the down vote). – john Mar 21 '20 at 09:08
  • Ya i did the issue was I was declaring them as static. They shouldn't have been static. Thanks for responding. – Jacob Krumholz Mar 21 '20 at 21:08
  • @JacobKrumholz That makes sense. Static class members are quite an advanced feature of C++, but I assumed you wanted them for some reason. – john Mar 22 '20 at 07:14