473,324 Members | 2,248 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,324 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 3697
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.