473,480 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Templates

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
Jul 19 '05 #1
3 3704
5 Nov 2003 22:54:18 -0800 pu*******************@btfinancialgroup.com
(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. :-)
Jul 19 '05 #2
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

Jul 19 '05 #3
pu*******************@btfinancialgroup.com (Puvendran) wrote in message news:<d5**************************@posting.google. 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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2544
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
5
2513
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
2139
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
12
1946
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
16
16187
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
1604
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
25
3284
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
2592
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
4434
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
1689
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.