0

I'm relatively new to c++, so bear with a little.

I have a class with the constructor:

Window(int width, int height, const std::string& title);

As defined in the header file.

I then have the code:

#include "window.h"

int main(int argc, char** argv) {
    new Window(800, 600, "Elysian Engine");
}

in Main.

When building, I am getting the error "undefined reference to 'Window(int, int, std::string const&)'" Which I do not understand, as I thought I am correctly importing it and everything. I understand this to be a linking error, but I'm not sure why.

Thanks!

--- EDIT ---

The code for window.cpp:

#include "window.h"
#include <SDL2/SDL.h>
#include <SDL/SDL.h>
#include <GL/glew.h>

Window::Window(int width, int height, const std::string& title) :
        width(width),
        height(height),
        title(title),
        isCloseRequested(false) {
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE,                8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,              8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,               8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,              8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,             32);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,              16);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,            1);

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,    SDL_GL_CONTEXT_PROFILE_CORE);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,   3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,   2);

    window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
    context = SDL_GL_CreateContext(window);

    SDL_GL_SetSwapInterval(1);

    GLenum res = glewInit();

    if (res != GLEW_OK) {
        fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
    }
}

Window::~Window() {
    SDL_GL_DeleteContext(context);
    SDL_DestroyWindow(window);
    SDL_Quit();
}
Isaac Woods
  • 1,114
  • 1
  • 17
  • 28
  • This has been marked as a duplicate, but I have looked at this, and did not see an answer. Could @πάντα ῥεῖ please emphasise where it answers my question? – Isaac Woods Dec 22 '14 at 17:14
  • _"as I thought I am correctly importing it and everything"_ Just the `#include "window.h"` statement isn't sufficient. Read about what's additionally necessary from the liked dupe please. – πάντα ῥεῖ Dec 22 '14 at 17:14
  • Nope, it's a class created by me. Do you wish to see the .cpp file for Window? – Isaac Woods Dec 22 '14 at 17:15
  • _" Do you wish to see the .cpp file for Window?"_ Might help to diagnose more, but most probably you've been missing to link `Window.cpp` with your program. – πάντα ῥεῖ Dec 22 '14 at 17:17
  • Ok, I am using Code::Blocks so I assumed it would link the files automatically? Surely the symbol is declared though in the header file, so why is the error on the line it is used? – Isaac Woods Dec 22 '14 at 17:18
  • Do you have something like `Window::Window(int width, int height, const std::string& title) { /* Definition */ }` in your `.cpp` file? – πάντα ῥεῖ Dec 22 '14 at 17:20
  • Yes, I believe I do. I have added the code from window.cpp just to clarify. – Isaac Woods Dec 22 '14 at 17:22
  • Looks correct to me, THX for the improvement. May be it's a Code::Blocks specific problem. Post the linker command line used if possible. May be add the [tag:codeblocks] tag. – πάντα ῥεῖ Dec 22 '14 at 17:25
  • @πάνταῥεῖ: wow, you did not mark it as dupe with your uberalles canonical... – László Papp Dec 22 '14 at 17:29
  • I can not find the linker command, it does not seem to make it obvious. I've added the codeblocks tag. Thanks for the help. – Isaac Woods Dec 22 '14 at 17:30
  • 1
    @lpapp Actually I retracted my dupe vote, after getting more clarification from OP's side ;-) ... – πάντα ῥεῖ Dec 22 '14 at 17:31
  • 1
    Oh, noooes, then I cannot call you MUR (Mr Undefined Reference)? :( – László Papp Dec 22 '14 at 17:31
  • @πάνταῥεῖ do they have a hat for that? – Mark Ransom Dec 22 '14 at 17:32
  • @MarkRansom Let's see. I wasn't chasing for one. – πάντα ῥεῖ Dec 22 '14 at 17:33
  • @IsaacWoods how would Code::Blocks know that windows.cpp is the implementation file for the declarations in windows.h? – Come Raczy Dec 22 '14 at 17:35
  • The only possible source of that error is that `window.cpp` is not being linked with `main`. I don't know anything about Code::Blocks so I can't help any further. – Mark Ransom Dec 22 '14 at 17:35
  • Ok, thanks @MarkRansom. And I think it's because the class Window is declared in window.h and then the namespace is used by doing Window::Window(...) in window.cpp, but by no means am I an expert. – Isaac Woods Dec 22 '14 at 17:37
  • @IsaacWoods your syntax is fine, that's not a namespace it's a class scope but the syntax is the same. Now if you have a namespace that you didn't show in the question, that would be a possible explanation. – Mark Ransom Dec 22 '14 at 17:49
  • Oh, ok; thanks for the correction. And no, I do not define a namespace anywhere in the project. – Isaac Woods Dec 22 '14 at 17:51

1 Answers1

1

Because your code does not appear to be causing the issue, I will address the IDE.

When I first started using Code::Blocks, I ran into the "undefined reference" problem quite a bit. There are multiple ways that I solved this.

  • Exit Code::Blocks and re-open it. This solved my issues more times than I would care to count. It's similar to finally realizing that an executable is acting strangely because you needed to run make clean before compiling; sometimes, you are compiling an old version of code without realizing it.

  • Delete window.h and window.cpp from the project and re-add them. This is similar to the solution above. Although this has worked for me before, I couldn't figure out exactly why it worked.

  • Include the full path of window.h. Instead of #include "window.h", try #include "/path/goes/here/window.h". It is possible that your current location (as specified in your #include statement) is incorrect.

  • Check the compiler that is selected for the project. When I used Code::Blocks, I typically used gcc. However, there was a case where I was receiving all sorts of errors (upon attempting compilation) that I either did not understand or could not resolve, only to learn that I had accidentally selected some gcc variant from the compiler selection list that I did not even have installed.

  • Create a new project. Move your source files out of the project folder that is created by Code::Blocks, and then remove that project folder. Launch Code::Blocks, create a new project, and add your source files to the new project.

  • When all else fails and you absolutely must use Code::Blocks, reinstall the full package. I had to do this my first time using Code::Blocks (which was also my first time using an IDE). I realized that I had a bare-bones installation of the IDE, which was limiting what I could do (including compiling). Naturally, I cannot access the Code::Blocks website right now, otherwise I would offer a link here as well. In this case, I think it is safe to say that the full-featured stable version will be the largest (in MB) you can find on Code::Blocks' Downloads page. After you have reinstalled, create a new project and add your files back to it.

I eventually moved to using writing code in an IDE (for the linting!), and taking care of a makefile on my own. It has been awhile since I've used Code::Blocks, but each of the above bullets represents solutions/work-arounds that have helped me for your specific problem (or, in general) while I was using Code::Blocks.

buratino
  • 1,408
  • 2
  • 17
  • 40
  • Ok, thanks. As we have basically decided this is a IDE problem, I have decided to move to Sublime Text 3 and deal with compilation on my own. Thanks for the help everyone! – Isaac Woods Dec 22 '14 at 18:13
  • Out of curiosity, would you mind testing the proposed solutions and letting me know if any work? – buratino Dec 22 '14 at 18:16
  • I have tried the first solution many-a-time and to no avail. The second and third solutions did nothing, resulting in the same error. Codeblocks offered no other compiler options, only the standard gcc is offered. And I have not tried the others. – Isaac Woods Dec 22 '14 at 18:19