Undefined reference to function (linker error) - c++

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

Related

Why does this C++ program have problems linking with undefined reference error?

For some reason, I'm having a lot of trouble lately with creating classes with multiple files because an undefined reference error keeps showing up.
Here's the code:
Card.h
class Card {
char name;
public:
char getName();
void setName(char);
Card();
Card(char);
};
Card.cpp
#include "Card.h"
Card::Card()
{
name = '-';
}
Card::Card(char _name)
{
name = _name;
}
void Card::setName(char _name)
{
name = _name;
}
char Card::getName()
{
return name;
}
main.cpp
#include <iostream>
#include "Card.h"
int main()
{
std::cout << "Welcome to deck of cards!" << std::endl;
Card card;
card.setName('A');
std::cout << card.getName() << std::endl;
return 0;
}
I get the program running by compiling and building:
g++ -c *.cpp
g++ -o Card.o main.o
This is the error:
/usr/bin/ld: main.o: in function `main':
main.cpp:(.text+0x4b): undefined reference to `Card::Card()'
/usr/bin/ld: main.cpp:(.text+0x5c): undefined reference to `Card::setName(char)'
/usr/bin/ld: main.cpp:(.text+0x68): undefined reference to `Card::getName()'
collect2: error: ld returned 1 exit status
I know it has something to do with the linker not recognizing the methods. But the methods exist, right?
Instead of g++ -o Card.o main.o you want g++ -o Card Card.o main.o
The doc says:
-o <file>
So, the -o option takes an output file. This is preventing you from linking Card.o in.

codeblocks c++ error: undefined reference to 'subclass::subclass()' [duplicate]

I can't seem to get the errors to go away. The errors are below. I have looked on Google Search and still I can't figure it out. It is not like I am new to C++, but I have not fooled around with it in a while.
The weird thing is it worked with g++ on Windows...
Errors using:
g++ main.cpp
Output:
/tmp/ccJL2ZHE.o: In function main': \ main.cpp:(.text+0x11): undefined reference to Help::Help()'
main.cpp:(.text+0x1d): undefined reference to Help::sayName()' \ main.cpp:(.text+0x2e): undefined reference to Help::~Help()'
main.cpp:(.text+0x46): undefined reference to `Help::~Help()'
collect2: ld returned 1 exit status
File main.cpp
#include <iostream>
#include "Help.h"
using namespace std;
int main () {
Help h;
h.sayName();
// ***
// ***
// ***
return 0;
}
File Help.h
#ifndef HELP_H
#define HELP_H
class Help {
public:
Help();
~Help();
void sayName();
protected:
private:
};
#endif // HELP_H
File Help.cpp
#include <iostream>
#include "Help.h"
using namespace std;
Help::Help() { // Constructor
}
Help::~Help() { // Destructor
}
void Help::sayName() {
cout << " ***************" << endl;
cout << " ************************************" << endl;
cout << " ************" << endl;
cout << " *********************" << endl;
}
Use
g++ main.cpp Help.cpp
You have to tell the compiler all the files that you want it to compile, not just the first one.
You should add help.o to your g++ line:
g++ -c help.cpp -o help.o
g++ help.o main.cpp
By splitting it to two lines you can save compilation time (in case of larger projects), because you can compile help.cpp only when it was changed. make and Makefile used well will save you a lot of headache:
#Makefile
all: main
main: help main.cpp
g++ -o main help.o main.cpp
help: help.cpp
g++ -c -o help.o help.cpp
I had the same problem with my Linux Lubuntu distribution and it was creating the problem for my constructor and destructor. It was not recognizing them.
Actually, this goes off if you just compile all of the three files together. So, once you saved all your files, just do this:
g++ main.cpp Help.h Help.cpp
./a.out
./a.out is the executable file for the Linux. Sorry, but I don't know about the Windows. And your program would run smoothly.

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 template members

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.

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.