Undefined reference to class::function

hi,
i have a building error problem : "Undefined reference to class::function", i've searched the internet but haven't found a real solution to the problem. most people solve it by including the right header file but i've included all mine.

this is the code of main.cpp
1
2
3
4
5
6
7
8
9
10
#include "../header_files/main.h"
using namespace std;
	
int main() 
{
	Tree* FolderTree = new Tree();
	FolderTree->makeTree("/",false,1);
	FolderTree->printTreeList();
	return 0;
}


and the main.h
1
2
3
4
5
6
7
8
9
#ifndef MAIN_H
#define MAIN_H

#include "../header_files/TreeElement.h"
#include "../header_files/File.h"
#include "../header_files/Folder.h"
#include "../header_files/Tree.h"

#endif 


and this is the erro message

g++ -Wall -o "main" "main.cpp" (in directory: /home/alexander/Documents/C++_projects/DocumentTree/source_files)
/tmp/ccfAeMu9.o: In function `main':
main.cpp:(.text+0x1f): undefined reference to `Tree::Tree()'
main.cpp:(.text+0x47): undefined reference to `Tree::makeTree(char const*, bool, int)'
main.cpp:(.text+0x53): undefined reference to `Tree::printTreeList()'
collect2: ld returned 1 exit status
Compilation failed.
It means you never gave those functions a body.
but i did give those function a body

example:
1
2
3
4
5
6
7
8
9
void Tree::printTreeList()
{
	unsigned int i;
	for(i=0 ; i < treeVector.size() ; i++)
   {
      cout << treeVector[i]->getName() << endl;
   }
	
}
But that function isn't in main.cpp, and from the looks of your commandline you are only compiling main.cpp:

g++ -Wall -o "main" "main.cpp"

You will need to compile whatever cpp file has those functions. You'll also have to link the resulting object file with main's object file.

Doing all of this by hand (via commandline) is nuts. Do yourself a favor and get an IDE. Then all you have to do is add these files to a project and press the 'Build' key. No need for commandline-fu.
Last edited on
check,

now i know the problem, i'm able to build my program by hand in the terminal
(btw i was using an IDE (geany)) , now i will make a make file so i won't have to do it by hand every time
now i will make a make file so i won't have to do it by hand every time


Err, well, you shouldn't even have to make a makefile.

I don't know what geany is, but if it doesn't let you just build by pressing a button, it's either not an IDE, or it's a really crappy one.

In any respectable IDE, you just tell it which files are part of your project (typically done with an "Add File to Project" option in the menus). Then whenever you want to build you just select the "Build" option (often mapped to one of the F keys). It automatically compiles all modified files and links them. You don't have to mess around with the details.
Topic archived. No new replies allowed.