Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Templates

Reply
Thread Tools

Templates

 
 
Puvendran
Guest
Posts: n/a
 
      11-06-2003
Hi,

I am writing a simple program to use templates. Basically the program
should be able create a stack of any type with ultimately 2 methods
one to add (push) and one to remove (pop) from the stack.

I am getting a compilation error thus - it's the stack<int>
s(10)declaration which seems to be the problem


Undefined first referenced
symbol in file
stack<int>::stack(int) /var/tmp/ccdiGgf1.o
ld: fatal: Symbol referencing errors. No output written to p
collect2: ld returned 1 exit status

Programs involved :


The main prog is

#include "p_stack.h"
//#include <iostream.h>

void
main()
{
stack<int> s(10);

int i;

for (int j=0;j < 12; j++)
{
//s.push(j);

}

for (int j=0;j < 10; j++)
{
//cout << s.pop() << endl;
}

}

The "p_stack.h" is

#include <vector>

template<class T>
class stack
{
public :

//int max_size;
//int curr_size;
vector<T> v;

stack(int);

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

p_stack.cc is

#include "p_stack.h"
#include <string>

template<class T>
stack<T>::stack(int i)
{
v.resize(i);
}


I thank you in advance for any assistance.

Puvendran
 
Reply With Quote
 
 
 
 
DarkSpy
Guest
Posts: n/a
 
      11-06-2003
5 Nov 2003 22:54:18 -0800
(Puvendran)

>Hi,
>
>I am writing a simple program to use templates. Basically the program
>should be able create a stack of any type with ultimately 2 methods
>one to add (push) and one to remove (pop) from the stack.
>
>I am getting a compilation error thus - it's the stack<int>
>s(10)declaration which seems to be the problem
>
>
>Undefined first referenced
> symbol in file
>stack<int>::stack(int) /var/tmp/ccdiGgf1.o
>ld: fatal: Symbol referencing errors. No output written to p
>collect2: ld returned 1 exit status
>
>Programs involved :
>
>
>The main prog is
>
>#include "p_stack.h"
>//#include <iostream.h>
>
>void
>main()
>{
> stack<int> s(10);
>
> int i;
>
> for (int j=0;j < 12; j++)
> {
> //s.push(j);
>
> }
>
> for (int j=0;j < 10; j++)
> {
> //cout << s.pop() << endl;
> }
>
>}

C++ standard:
int main() or main() will return a int value implicit.


>The "p_stack.h" is
>
>#include <vector>
>
>template<class T>
>class stack
>{
> public :
>
> //int max_size;
> //int curr_size;
> vector<T> v;


that is std::vector<T>
or define
using namespace std;
or define
using std::vector

to tell compiler the vector is defined with namespace std.


> stack(int);
>
> //void push(T);
> //T pop();
>};
>
>p_stack.cc is
>
>#include "p_stack.h"
>#include <string>
>
>template<class T>
>stack<T>::stack(int i)
>{
> v.resize(i);
>}
>
>
>I thank you in advance for any assistance.
>
>Puvendran






[comp.lang.c++]
[comp.lang.c++.moderated]
DarkSpy, A C++ Mad Dog.
 
Reply With Quote
 
 
 
 
Oplec
Guest
Posts: n/a
 
      11-06-2003
Puvendran wrote:
> Hi,
>
> I am writing a simple program to use templates. Basically the program
> should be able create a stack of any type with ultimately 2 methods
> one to add (push) and one to remove (pop) from the stack.
>
> I am getting a compilation error thus - it's the stack<int>
> s(10)declaration which seems to be the problem
>
>
> Undefined first referenced
> symbol in file
> stack<int>::stack(int) /var/tmp/ccdiGgf1.o
> ld: fatal: Symbol referencing errors. No output written to p
> collect2: ld returned 1 exit status
>
> Programs involved :
>
>
> The main prog is
>
> #include "p_stack.h"
> //#include <iostream.h>
>
> void
> main()
> {
> stack<int> s(10);
>
> int i;
>
> for (int j=0;j < 12; j++)
> {
> //s.push(j);
>
> }
>
> for (int j=0;j < 10; j++)
> {
> //cout << s.pop() << endl;
> }
>
> }
>
> The "p_stack.h" is
>
> #include <vector>
>
> template<class T>
> class stack
> {
> public :
>
> //int max_size;
> //int curr_size;
> vector<T> v;
>
> stack(int);
>
> //void push(T);
> //T pop();
> };
>
> p_stack.cc is
>
> #include "p_stack.h"
> #include <string>
>
> template<class T>
> stack<T>::stack(int i)
> {
> v.resize(i);
> }
>
>
> I thank you in advance for any assistance.
>
> Puvendran


If your compiler does not support the C++ "export" feature for
templates, and most compilers do not, then you must include you class
template member definitions after your class template definition in
every .cc file that will use your class template. What that means is
that you need to copy the member definition from p_stack.cc to p_stack.h
and make sure that the member definition is placed after the template
definition.

You should have this for p_stack.h

#ifndef P_STACK_H
#define P_STACK_H

#include <vector>

template<class T>
class stack
{
public :

//int max_size;
//int curr_size;
vector<T> v;

stack(int);

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

template<class T>
stack<T>::stack(int i)
{
v.resize(i);
}

#endif // #ifndef P_STACK_H

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      11-06-2003
(Puvendran) wrote in message news:<. com>...
> Hi,
>
> I am writing a simple program to use templates. Basically the program
> should be able create a stack of any type with ultimately 2 methods
> one to add (push) and one to remove (pop) from the stack.
>
> I am getting a compilation error thus - it's the stack<int>
> s(10)declaration which seems to be the problem
>
>
> Undefined first referenced
> symbol in file
> stack<int>::stack(int) /var/tmp/ccdiGgf1.o
> ld: fatal: Symbol referencing errors. No output written to p
> collect2: ld returned 1 exit status


<code snipped>

See http://www.comeaucomputing.com/techt.../#whylinkerror

Either use the export keyword when defining the stack constructor if
your compiler supports export, or put #include "p_stack.cc" at the end
of p_stack.h. If you do that, make sure that p_stack.cc _does not_
include p_stack.h.

hth
GJD
 
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
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57