0

I'm working on a project using Qt Creator.

Let's say I have a simple Tool class:

#ifndef TOOL_H
#define TOOL_H

#include <map>
#include <string>
#include "myobject.h"

class Tool
{
public:
    Tool();

private:
    const std::map<std::string, MyObject*> myMap;
};

#endif // TOOL_H

with a default constructor like this:

#include "tool.h"

using namespace std;

Tool::Tool()
{
    myMap = new map<string, MyObject*>();
    // populate myMap
}

Then I want to use the class in the constructor of another class:

#include "myclass.h"

using namespace std;

MyClass::MyClass()
{
    Tool t;
}

But I get the following compile error:

In function 'MyClass::MyClass()':
undefined reference to 'Tool::Tool()'

Naturally, myclass.h includes tool.h, so I don't understand why it can't find it. I tried using a pointer, declaring t as a member variable, but I keep getting this error.

I tried to make a minimal project outside mine using these classes and compiled it with g++ (Tool and then MyClass with a main function); it worked. So maybe it's a problem with how Qt Creator handles compile and linking? But I don't know which option to check.

Thank you for any ideas.

EDIT: The problem came from the Qt Creator environment. I actually created MyClass before Tool, and without cleaning the project compile failed without telling me what the error was; the true error was myMap = new map<string, MyObject*>(); in the Tool class, so Tool was never compiled (as Jarod42 mentioned), thus the compile error for MyClass.

Cleaning and rebuilding pointed out that true error and allowed me to fix my project.

Ety
  • 334
  • 5
  • 13
  • 2
    `myMap = new map();` is invalid BTW, You forget to compile tool.cpp – Jarod42 Jul 25 '16 at 10:00
  • You're right, that line was invalid and messed up with the compile process, which did not inform me of it. – Ety Jul 25 '16 at 11:52

1 Answers1

5

undefined reference to 'Tool::Tool()' is a linker error message.

It means your cpp files individually are successfully compiled, but the linker can't find Tool::Tool() when creating the binary.

It's most likely a project setup issue in your build system.

Dutow
  • 5,638
  • 1
  • 30
  • 40
  • 1
    It was indeed a project issue: it had to be cleaned first to be able to show me the error mentioned in the comment above (in the Tool class). Edited question. – Ety Jul 25 '16 at 12:01