| | |
Undefined reference error
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 61
Reputation:
Solved Threads: 1
I am trying to implement a queue based on a Linked List that I had to write previously. It is templated and for some reason I am getting the following error using g++ when I compile:
It is saying undefined reference however I have declared and defined it so I cant figure out what the problem is.
This is my class declaration and definition:
Here is my main:
Thanks in advance for any help.
C++ Syntax (Toggle Plain Text)
g++ LinkedQueueMain.cpp -o LinkedQueueMain /tmp/ccqwStpK.o: In function `main': LinkedQueueMain.cpp:(.text+0x2ee): undefined reference to `LinkedQueue<char>::operator=(LinkedQueue<char> const&)' collect2: ld returned 1 exit status
It is saying undefined reference however I have declared and defined it so I cant figure out what the problem is.
This is my class declaration and definition:
// LinkedQueue.h
#ifndef LINKEDQUEUE_H
#define LINKEDQUEUE_H
#include <iostream>
#include "RuntimeException.h"
#include "LinkedList.h"
template<typename T> class LinkedQueue;
template<typename T>
std::ostream& operator<<(std::ostream& out, const LinkedQueue<T>& queue);
template<typename T>
class LinkedQueue {
private:
LinkedList<T> ll;
public:
// user-defined exceptions
class QueueEmptyException : public RuntimeException {
public:
QueueEmptyException() : RuntimeException("Access to an empty queue") {}
};
LinkedQueue() { } // constructor
~LinkedQueue() { } // destructor
LinkedQueue(const LinkedQueue& queue) { ll = queue.ll; } // copy constructor
LinkedQueue& operator=(const LinkedQueue& queue); // assignment operator
// I have cut out accessory function declarations
friend std::ostream& operator<< <>(std::ostream& out, const LinkedQueue<T>& queue); // overload <<
};
//-------------------------------------------------------------------------------------------------------------------------
template<typename T>
LinkedQueue<T>& LinkedQueue<T>::operator=(const LinkedQueue& queue) {
ll = queue.ll;
return *this;
}
// other functions defined here I just cut them out for space
#endifHere is my main:
#include "LinkedQueue.h"
#include <iostream>
#include <string>
#include <iterator>
using namespace std;
int main()
{
LinkedQueue<char> queue;
LinkedQueue<char> queue_copy;
//===== enqueue() =====
queue.enqueue('1');
queue.enqueue('2');
queue.enqueue('3');
queue.enqueue('4');
queue.enqueue('5');
queue.enqueue('6');
queue_copy = queue; //THIS IS WHERE I BELIEVE THE PROBLEM IS
cerr << "assigning queue to queue_copy, queue_copy = ";
cerr << queue_copy << endl;
cerr << "size of queue_copy = " << queue_copy.size() << endl;
cerr << "first of queue_copy = " << queue_copy.first() << endl << endl;
return 0;
}Thanks in advance for any help.
Are you sure this code works:
'll' is under private section, right? I'm not sure if you can access it.
Here's my code example if it helps:
C++ Syntax (Toggle Plain Text)
ll = queue.ll;
Here's my code example if it helps:
C++ Syntax (Toggle Plain Text)
//operators //= Complex& Complex::operator=(Complex const& aCplx){ if (this != &aCplx){ mNum.Im = aCplx.getIm(); mNum.Re = aCplx.getRe(); } return *this; }
•
•
Join Date: Apr 2008
Posts: 61
Reputation:
Solved Threads: 1
•
•
•
•
Are you sure this code works:
'll' is under private section, right? I'm not sure if you can access it.C++ Syntax (Toggle Plain Text)
ll = queue.ll;
Here's my code example if it helps:
C++ Syntax (Toggle Plain Text)
//operators //= Complex& Complex::operator=(Complex const& aCplx){ if (this != &aCplx){ mNum.Im = aCplx.getIm(); mNum.Re = aCplx.getRe(); } return *this; }
•
•
•
•
Well the definition is within the class and classes can access their own private data members so ya it should work. I tried putting it in the public section just to check and it did the same thing. As to your example thats basically the same thing that im trying to do. I don't see why mine doesnt work.
Function can access IT'S own members (this->memb1; this->memb2

But I'm really not sure about accessing other instance of same type...
Maybe your problem is in something else...
•
•
Join Date: Apr 2008
Posts: 61
Reputation:
Solved Threads: 1
•
•
•
•
I'm not sure about this.
Function can access IT'S own members (this->memb1; this->memb2
But I'm really not sure about accessing other instance of same type...
Maybe your problem is in something else...
BTW Thanks to everyone thats been trying to help. This is getting really frustrating.
![]() |
Similar Threads
- getting the undefined reference error (C++)
- [linker error] undefined reference - why am I getting this error? (C++)
- undefined reference error while linking..help (C++)
- undefined reference error -HELP! (C++)
- Dev C++ linker errors, undefined reference (C++)
- [Linker error] undefined reference to `CQTMovieFile::CQTMovieFile()' (C++)
- Dev-C++, GLUI linker error, undefined reference (C++)
- Linker Error>Undefined reference error to xyz (C++)
- undefined reference errors when using C++ Sockets Library (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ with GSL help
- Next Thread: VS 2005 - Working with templates and lists error.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





