Cjueden
Cjueden

Reputation: 1200

Undefined referance to class

I know this has been asked a thousands times, but I'm stumped. I've been looking all over for that last 3 days without a result. I keep getting this error and I can't figure out why. I've added only the code that I have input / that matters. If I comment out my code the program compiles without a problem. What am I doing wrong???

CMakeFiles/brewtarget.dir/MainWindow.cpp.o: In function MainWindow::MainWindow(QWidget*)': MainWindow.cpp:(.text+0xb145): undefined reference to yeastCellCounter::yeastCellCounter()'

CODE

mainwindow.cpp

#include "yeastcellcounter.h"

// a whole lot of stuff between these...

yeastCountDialog = new yeastCellCounter();

mainwindow.h

class yeastCellCounter;

// A whole log of stuff between these...

yeastCellCounter *yeastCountDialog;

yeascellcounter.cpp

#include "yeastcellcounter.h"

yeastCellCounter::yeastCellCounter(){}

yeastcellcounter.h

#ifndef YEASTCELLCOUNTER_H
#define YEASTCELLCOUNTER_H

class yeastCellCounter
{
public:
    yeastCellCounter();
};

#endif // YEASTCELLCOUNTER_H

This are the INCLUDE_DIRECTORIES directive in cmakelist.txt

SET(ROOTDIR "${CMAKE_CURRENT_SOURCE_DIR}")
SET(SRCDIR "${ROOTDIR}/src")
SET(UIDIR "${ROOTDIR}/ui")
SET(DATADIR "${ROOTDIR}/data")
SET(TRANSLATIONSDIR "${ROOTDIR}/translations")
SET(WINDIR "${ROOTDIR}/win")

INCLUDE_DIRECTORIES(${SRCDIR})
INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}/src") # In case of out-of-source build.
INCLUDE_DIRECTORIES("${CMAKE_BINARY_DIR}/QtDesignerPlugins")

Upvotes: 0

Views: 128

Answers (2)

Philip G. Lee
Philip G. Lee

Reputation: 450

The problem is:

yeastCountDialog = new yeastCellCounter();

It should be:

yeastCountDialog = new yeastCellCounter;

(Notice the lack of parentheses). The default constructor is always called without parentheses. And also, you need to add "yeastcellcounter.cpp" to the list of cmake sources.

Upvotes: 1

RobbieE
RobbieE

Reputation: 4350

Whenever you see a error of the type undefined reference to ... it is a linker error. This means that the compiler has completed it's work and all the object files have been compiled without errors. It's now time for the linker to put all the pieces together into a single file.

In your specific example, it says that it cannot find the definition of the function yeastCellCounter::yeastCellCounter(). From the code you have pasted, this function, albeit empty, is clearly defined in the file yeascellcounter.cpp.

It looks like your cmakelists.txt file is incomplete. You haven't specified which source files need to be linked together to create your final executable. You need to use the add_executable statement for this.

Here's a simple example

Upvotes: 2

Related Questions