473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3700
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
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.