Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why the undefined references in this simple program?

Reply
Thread Tools

Why the undefined references in this simple program?

 
 
here@softcom.net
Guest
Posts: n/a
 
      02-26-2013
I'm trying to complile and link this simple example of using a template:

//file stack2.hpp (header)
template<class T> class Stack {
T* v;
int max_size;
int top;
public:
class Underflow {};
class Overflow {};

Stack(int s); //constructor
~Stack(); //destructor

void push(T);
T pop();
};

//file stack2.cpp
#include "stack2.hpp"

template<class T> void Stack<T>:ush(T c)
{
if (top==max_size) throw Overflow();
v[top] = c;
top = top + 1;
}
template<class T> T Stack<T>:op()
{
if (top==0) throw Underflow();
top = top - 1;
return v[top];
}

//file user2.cpp
#include <iostream>
#include "stack2.hpp"
using namespace std;

Stack<char> sc(200);

int main(int argc, char *argv[])
{
sc.push('c');
cout << sc.pop() << "\n";

return 0;
}

/////////////////////////

If I compile with: g++ stack2.cpp user2.cpp
here's what I get:

/tmp/cciILMx2.o: In function `main':
user2.cpp.text+0x1a): undefined reference to `Stack<char>:ush(char)'
user2.cpp.text+0x24): undefined reference to `Stack<char>:op()'
/tmp/cciILMx2.o: In function `__static_initialization_and_destruction_0(int, int)':
user2.cpp.text+0x94): undefined reference to `Stack<char>::Stack(int)'
user2.cpp.text+0x99): undefined reference to `Stack<char>::~Stack()'
collect2: ld returned 1 exit status
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      02-26-2013
On Tuesday, 26 February 2013 21:45:09 UTC+2, (E-Mail Removed) wrote:
> I'm trying to complile and link this simple example of using a template:
>
> //file stack2.hpp (header)
>
> template<class T> class Stack {
> T* v;
> int max_size;
> int top;
> public:
> class Underflow {};
> class Overflow {};
>
> Stack(int s); //constructor
> ~Stack(); //destructor
>
> void push(T);
> T pop();
> };
>
> //file stack2.cpp


Move its contents to stack2.h and delete the file.
This is how templates work.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-26-2013
On 2/26/2013 3:04 PM, Öö Tiib wrote:
> On Tuesday, 26 February 2013 21:45:09 UTC+2, (E-Mail Removed) wrote:
>> I'm trying to complile and link this simple example of using a template:
>>
>> //file stack2.hpp (header)
>>
>> template<class T> class Stack {
>> T* v;
>> int max_size;
>> int top;
>> public:
>> class Underflow {};
>> class Overflow {};
>>
>> Stack(int s); //constructor
>> ~Stack(); //destructor
>>
>> void push(T);
>> T pop();
>> };
>>
>> //file stack2.cpp

>
> Move its contents to stack2.h and delete the file.
> This is how templates work.


http://www.parashift.com/c++-faq/res...g-to-faqs.html

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
here@softcom.net
Guest
Posts: n/a
 
      02-26-2013
On Tuesday, February 26, 2013 12:04:17 PM UTC-8, Öö Tiib wrote:
> On Tuesday, 26 February 2013 21:45:09 UTC+2, (E-Mail Removed) wrote:
>
> > I'm trying to complile and link this simple example of using a template:

>
> >

>
> > //file stack2.hpp (header)

>
> >

>
> > template<class T> class Stack {

>
> > T* v;

>
> > int max_size;

>
> > int top;

>
> > public:

>
> > class Underflow {};

>
> > class Overflow {};

>
> >

>
> > Stack(int s); //constructor

>
> > ~Stack(); //destructor

>
> >

>
> > void push(T);

>
> > T pop();

>
> > };

>
> >

>
> > //file stack2.cpp

>
>
>
> Move its contents to stack2.h and delete the file.
>
> This is how templates work.


I tried that and got the following:

$ g++ user.c
/tmp/cc9OwIxI.o: In function `f(Stack&, int)':
user.c.text+0x1f): undefined reference to `Stack::Stack(int)'
user.c.text+0x39): undefined reference to `Stack::Stack(int)'
user.c.text+0x4c): undefined reference to `Stack:ush(char)'
user.c.text+0x5d): undefined reference to `Stack:ush(char)'
user.c.text+0x6e): undefined reference to `Stack:ush(char)'
user.c.text+0x7f): undefined reference to `Stack:ush(char)'
user.c.text+0x89): undefined reference to `Stack:op()'
user.c.text+0xb1): undefined reference to `Stack:op()'
user.c.text+0xd9): undefined reference to `Stack:op()'
user.c.text+0x101): undefined reference to `Stack:op()'
user.c.text+0x129): undefined reference to `Stack:op()'
user.c.text+0x151): undefined reference to `Stack::~Stack()'
user.c.text+0x179): undefined reference to `Stack::~Stack()'
/tmp/cc9OwIxI.o: In function `main':
user.c.text+0x1a5): undefined reference to `Stack::Stack(int)'
user.c.text+0x1b6): undefined reference to `Stack:ush(char)'
user.c.text+0x1d3): undefined reference to `Stack::~Stack()'
user.c.text+0x1ee): undefined reference to `Stack::~Stack()'
/tmp/cc9OwIxI.o: In function `__static_initialization_and_destruction_0(int, int)':
user.c.text+0x246): undefined reference to `Stack::Stack(int)'
user.c.text+0x24b): undefined reference to `Stack::~Stack()'
collect2: ld returned 1 exit status
 
Reply With Quote
 
here@softcom.net
Guest
Posts: n/a
 
      02-26-2013
On Tuesday, February 26, 2013 12:30:11 PM UTC-8, (E-Mail Removed) wrote:
> On Tuesday, February 26, 2013 12:04:17 PM UTC-8, Öö Tiib wrote:
>
> > On Tuesday, 26 February 2013 21:45:09 UTC+2, (E-Mail Removed) wrote:

>
> >

>
> > > I'm trying to complile and link this simple example of using a template:

>
> >

>
> > >

>
> >

>
> > > //file stack2.hpp (header)

>
> >

>
> > >

>
> >

>
> > > template<class T> class Stack {

>
> >

>
> > > T* v;

>
> >

>
> > > int max_size;

>
> >

>
> > > int top;

>
> >

>
> > > public:

>
> >

>
> > > class Underflow {};

>
> >

>
> > > class Overflow {};

>
> >

>
> > >

>
> >

>
> > > Stack(int s); //constructor

>
> >

>
> > > ~Stack(); //destructor

>
> >

>
> > >

>
> >

>
> > > void push(T);

>
> >

>
> > > T pop();

>
> >

>
> > > };

>
> >

>
> > >

>
> >

>
> > > //file stack2.cpp

>
> >

>
> >

>
> >

>
> > Move its contents to stack2.h and delete the file.

>
> >

>
> > This is how templates work.

>
>
>
> I tried that and got the following:
>
>
>
> $ g++ user.c
>
> /tmp/cc9OwIxI.o: In function `f(Stack&, int)':
>
> user.c.text+0x1f): undefined reference to `Stack::Stack(int)'
>
> user.c.text+0x39): undefined reference to `Stack::Stack(int)'
>
> user.c.text+0x4c): undefined reference to `Stack:ush(char)'
>
> user.c.text+0x5d): undefined reference to `Stack:ush(char)'
>
> user.c.text+0x6e): undefined reference to `Stack:ush(char)'
>
> user.c.text+0x7f): undefined reference to `Stack:ush(char)'
>
> user.c.text+0x89): undefined reference to `Stack:op()'
>
> user.c.text+0xb1): undefined reference to `Stack:op()'
>
> user.c.text+0xd9): undefined reference to `Stack:op()'
>
> user.c.text+0x101): undefined reference to `Stack:op()'
>
> user.c.text+0x129): undefined reference to `Stack:op()'
>
> user.c.text+0x151): undefined reference to `Stack::~Stack()'
>
> user.c.text+0x179): undefined reference to `Stack::~Stack()'
>
> /tmp/cc9OwIxI.o: In function `main':
>
> user.c.text+0x1a5): undefined reference to `Stack::Stack(int)'
>
> user.c.text+0x1b6): undefined reference to `Stack:ush(char)'
>
> user.c.text+0x1d3): undefined reference to `Stack::~Stack()'
>
> user.c.text+0x1ee): undefined reference to `Stack::~Stack()'
>
> /tmp/cc9OwIxI.o: In function `__static_initialization_and_destruction_0(int, int)':
>
> user.c.text+0x246): undefined reference to `Stack::Stack(int)'
>
> user.c.text+0x24b): undefined reference to `Stack::~Stack()'
>
> collect2: ld returned 1 exit status


SORRY THE ABOVE WAS THE WRONG FILE, HERE'S THE REAL OUTPUT:

$ g++ user2.cpp
/tmp/ccXm73vo.o: In function `__static_initialization_and_destruction_0(int, int)':
user2.cpp.text+0x94): undefined reference to `Stack<char>::Stack(int)'
user2.cpp.text+0x99): undefined reference to `Stack<char>::~Stack()'
collect2: ld returned 1 exit status
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      02-26-2013
On Tuesday, 26 February 2013 22:36:28 UTC+2, (E-Mail Removed) wrote:
> SORRY THE ABOVE WAS THE WRONG FILE, HERE'S THE REAL OUTPUT:
>
> $ g++ user2.cpp
> /tmp/ccXm73vo.o: In function `__static_initialization_and_destruction_0(int, int)':
> user2.cpp.text+0x94): undefined reference to `Stack<char>::Stack(int)'
> user2.cpp.text+0x99): undefined reference to `Stack<char>::~Stack()'
> collect2: ld returned 1 exit status


The definitions for constructor and destructor *are* *missing*.
Define them in your stack.hpp.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why the standard doesn't force following the "simple" references link ? X Ryl C++ 0 04-21-2006 04:17 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments