Undefined reference to template members - c++

I'm new to C++, and preparing a homework by using NetBeans IDE on Ubuntu 10.04. I use g++ as a C++ compiler.
The error message:
build/Debug/GNU-Linux-x86/Maze.o: In function `Maze':
Maze/Maze.cpp:14: undefined reference to `Stack<Coordinate>::Stack()'
Maze/Maze.cpp:14: undefined reference to `Stack<Coordinate>::Stack()'
Maze/Maze.cpp:69: undefined reference to `Stack<Coordinate>::push(Coordinate)'
Maze/Maze.cpp:79: undefined reference to `Stack<Coordinate>::isEmpty()'
Maze/Maze.cpp:87: undefined reference to `Stack<Coordinate>::destroy()'
And my related code:
Maze.h
#include "Coordinate.h"
#include "Stack.h"
....
....
/**
* Contains the stack object
*
* #var Stack stack
* #access private
*/
Stack<Coordinate> *stack;
...
...
Maze.cpp
#include "Maze.h"
...
...
Maze::Maze()
{
// IT SHOWS THAT THE FOLLOWING LINE HAS AN ERROR///
stack = new Stack<Coordinate>;
///////////////////////////////////////////////////
for( int y=0; y<8; y++ )
{
for( int x=0; x<8; x++ )
{
maze[y][x] = '0';
}
}
}
...
...
And according to the error output, Each line that I used stack variable has an error: Undefined reference.
Stack.cpp
#include "Stack.h"
...
...
template <class T> Stack<T>::Stack()
{
// Create the stac!
create();
}
...
I have googled it, but could not solve the problem. I think there is something wrong with my includes order, or maybe I used the pointers in a wrong way.
I also tried to create a makefile by myself, but the result did not change. I prepared this makefile according to this link: http://www.cs.umd.edu/class/spring2002/cmsc214/Tutorial/makefile.html
Here is my makefile:
maze: Maze.o Stack.o Coordinate.o
g++ -Wall Maze.o Stack.o Coordinate.o -o maze
Maze.o: Maze.cpp Maze.h Stack.h Coordinate.h
g++ -Wall -c Maze.cpp
Stack.o: Stack.cpp Stack.h
g++ -Wall -c Stack.cpp
Coordinate.o: Coordinate.cpp Coordinate.h
g++ -Wall -c Coordinate.cpp
Maze.h: Stack.h Coordinate.h
How could I overcome this error? Any ideas?

Stack is a template. The complete definition has to go in its header file. That is, do not separate it into a .h and a .cpp file.

Related

Undefined reference to vector declared in a different source file used in an "if" statement

I've created a source file that contains a number of data structures (maps, vector, array). Its header file is #included in the main-file.
The main file looks like this:
#include "reachability.h" //Where monkey() and vector<int> int_req are declared
main()
{
monkey(int_req); // Calling monkey(int_req) here is OK! Bar is visible
...
ifstream fp("foo.txt");
if(fp.is_open())
{
std::string line;
while( getline(fp,line) )
{
monkey(int_req); //'int_req' is an undefined reference!
}
}
}
And reachability.h
#ifndef REACHABILITY_H
#define REACHABILITY_H
extern std::vector<int> int_req;
void monkey(std::vector<int> feces);
#endif
And reachability.cc
std::vector<int> int_req;
void monkey(std::vector<int> thrown_obj)
{
... //Iteration and dereferencing of "thrown_obj"
}
I've accessed data structures that are declared in reachability.cc in a for-loop in the scope of main and that was fine. Something wonky is happening in this if-statement though.
Compiler Error:
lab1.o: In function `main':
/home/ubuntu/workspace/ECE597/Lab1/lab1.cc:105: undefined reference to `int_req'
collect2: error: ld returned 1 exit status
Edited: reachability.cc is included in compiliation:
elusivetau:~/XXXX/XXXX/XXXX $ g++ lab1.cc parser.cc gate.cc reachability.cc -o run
/tmp/ccJK4O9q.o: In function `main':
lab1.cc:(.text+0x489): undefined reference to `int_req'
collect2: error: ld returned 1 exit status
Edited: makefile for this program:
all: lab1.o parser.o gate.o reachability.o
g++ -g lab1.o parser.o gate.o reachability.o -o run
lab1.o: lab1.cc
g++ -g -c lab1.cc
parser.o: parser.cc
g++ -g -c parser.cc
gate.o: gate.cc
g++ -g -c gate.cc
reachability.o: reachability.cc
g++ -g -c reachability.cc
clean:
rm *o run
Whatever it is, you're not giving us the correct information.
I added includes and removed non-code to make this compile. And voila, it also links:
test.cpp:
#include "reachability.h" //Where monkey() and vector<int> int_req are declared
#include <string>
#include <iostream>
#include <fstream>
main()
{
monkey(int_req); // Calling monkey(int_req) here is OK! Bar is visible
std::ifstream fp("foo.txt");
if(fp.is_open())
{
std::string line;
while( getline(fp,line) )
{
monkey(int_req); //'int_req' is an undefined reference!
}
}
}
reachability.h:
#ifndef REACHABILITY_H
#define REACHABILITY_H
#include <vector>
extern std::vector<int> int_req;
void monkey(std::vector<int> feces);
#endif
reachability.cpp:
#include "reachability.h"
std::vector<int> int_req;
void monkey(std::vector<int> thrown_obj)
{
}
This compiles and links just fine. You are leading us on a wild goose chase by not bothering to create a mvce

Undefined reference to function (linker error)

Hi i read the other questions and answers about undefined reference.But still i'm not able to find out what are the problems with my code. I have a simple linked list code wherein i add the integers to the tail and after that i display them. Here is my code
"head.h"
#ifndef __HEAD_H_INCLUDE
#define __HEAD_H_INCLUDE
class Node {
int info;
Node *next;
};
class imple {
public:
imple();
void addToTail(int );
void display(void);
private:
Node *head,*tail;
};
#endif
"implementaion.cpp"
#include<iostream>
#include "head.h"
imple::imple(){
head=tail=0;
}
void imple::addToTail(int key){
if(tail==0)
{tail=head=new Node();
info=key;next=0;}
else
{
tail->next=new Node();
info=key;next=0;
tail=tail->next;
}
}
void imple::display(){
Node *temp;
for(temp=head;temp->next !=0;temp=temp->next)
{
std::cout<<temp->info << " ";
}
}
"main.cpp"
#include<iostream>
#include "head.h"
int main(){
Node node;
imple ab;
for(int i=0;i<5;i++)
ab.addToTail(i);
ab.display();
}
Everytime i compile i get this error
"/tmp/cc20Z1ZH.o: In function main':
lmain.cpp:(.text+0x10): undefined reference toimple::imple()'
lmain.cpp:(.text+0x2a): undefined reference to imple::addToTail(int)'
lmain.cpp:(.text+0x45): undefined reference toimple::display()'
collect2: ld returned 1 exit status"
Your answers and suggestions will be helpful
In short, you may use
g++ main.cpp implementation.cpp -o out
You need to include implementation.cppin your building process and make the function definitions accessible to the linker. That is, compile it with
g++ -c implementation.cpp -o implementation.o
and
g++ -c main.cpp -o main.o
and link them together with
g++ main.o implementation.o -o out
Try using
g++ main.cpp implementaion.cpp
Probably this will help
You can create a run file with:
g++ -o main implementation.cpp main.cpp
and run it with :
./main

C++ classes in separate file [duplicate]

This question already has answers here:
creating classes link error
(2 answers)
Closed 8 years ago.
I'm trying to learn how to make classes in C++ where I use a header file, a .cpp file that contains the class function definitions, and a main .cpp file. Here is what I have (taken from an example)
in class.h
class MyClass
{
public:
void foo();
int bar;
};
in class.cpp
#include "class.h"
using namespace std;
void MyClass::foo()
{
cout<< "test";
}
in main.cpp
#include "class.h"
using namespace std;
int main()
{
MyClass a;
a.foo();
return 0;
}
Compiling the main.cpp results in this error:
[Linker error] C:\:(.text+0x16): undefined reference to `MyClass::foo()'
collect2: ld returned 1 exit status
Do I need to compile the class.cpp or class.h? Am I missing a way of linking class.h with class.cpp? If so how do I link them?
You need to compile the implementation files into object files and link them together. The following is an example for when you are using g++:
g++ -c class.cpp -o class.o
g++ -c main.cpp -o main.o
g++ class.o main.o -o main
./main
In reality, you would add more options like -std=c++11 -O3 -Wall -Wextra -Werror etc.
You can try this on Linux shell using g++
Compile Create object files of main.cpp and class.cpp called main.o and class.o
g++ -c class.cpp
g++ -c main.cpp
Linking the object codes main.o and class.o to create executable file called program
g++ -o program main.o class.o
then run the program executable file
./program
You are likely to be compiling only main.cpp and not class.cpp.
What command are you using to generate the output ?
This should work fine :
g++ class.cpp main.cpp -o class
Its working fine
I tried the code in my Compiler
MyClass.h
#include <iostream>
class MyClass
{
public:
void foo();
int bar;
};
MyClass.cpp
#include "MyClass.h"
using namespace std;
void MyClass::foo()
{
cout<< "test";
}
Main.cpp
#include <iostream>
#include "MyClass.h"
int main(int argc, const char * argv[])
{
MyClass a;
a.foo();
return 0;
}
Ive tried the code in Xcode.
Its working just fine.
Use compiler option -I<dir of .h file> while compiling .cpp file. Compile both the .cpp files

g++ removes useless function? Undefined reference

I have hpp file with declaration:
namespace X {
class Y {
public:
[other functions]
inline float basicFunction();
int someFunction();
[other functions]
};
}
And in cpp file:
namespace X {
[implementations etc.]
inline float Y::basicFunction() {
return someValue * someMath / moreMath;
}
int Y::someFunction() {
return basicFunction() * 100;
}
[other functions]
}
I'm using it at other cpp file, but I think this isn't problem. Compiling with:
g++ -c someclass.cpp -o someclass.o -std=c++11
g++ -c main.cpp -o main.o -std=c++11
g++ main.o someclass.o -o main -std=c++11 -O0
Throw error:
main.o: In function `main':
main.cpp:(.text+0x4d9): undefined reference to `X::Y::someFunction()'
Why? How I can compile it correct?
I know that someFunction() is useless, but this is called many times and I just like that way.
All code above isn't real, so may have bugs, but on my program it's (I think) correct
I tried many combinations (both functions with same return type, both inline, none inline etc.) and no effect.
Solved. Function cannot be inline.
Still don't know why it worked after some attempts after deleted inline but nevermind.
Explanation why function in this code can't be inline is simple. Compiler, when see "inline", don't create pointer to function, but paste code in the place of reference.
Just my mistake...

C++ Undefined Reference (Even with Include)

I cannot get this simple piece of code to compile without including the TestClass.cpp file explicitly in my main.cpp file. What am I doing wrong? Thanks in advance!
Here is the code:
TestClass.h
#ifndef TESTCLASS_H_
#define TESTCLASS_H_
class TestClass
{
public:
static int foo();
};
#endif
TestClass.cpp
#include "TestClass.h"
int TestClass::foo() { return 42; }
main.cpp
#include <iostream>
#include "TestClass.h"
using namespace std;
int main()
{
cout << TestClass::foo() << endl;
return 0;
}
Here is the error:
g++ main.cpp -o main.app
/tmp/ccCjOhpy.o: In function `main':
main.cpp:(.text+0x18e): undefined reference to `TestClass::foo()'
collect2: ld returned 1 exit status
Include TestClass.cpp into the commandline, so the linker can find the function definition:
g++ main.cpp TestClass.cpp -o main.app
Alternatively, compile each to their own object file, then tell the compiler to link them together (it will forward them to the linker)
g++ -c main.cpp -o main.o
g++ -c TestClass.cpp -o TestClass.o
g++ main.o TestClass.o -o main.app
You're not compiling and linking against TestClass.cpp (where the implementation of foo() is). The compiler is thus complaining that your trying to use an undefined function.