🔒 Locked
Template Question
Advertisement
Original Post
I have 2 template classes, one for a stack, one for a queue - these are subclasses of Container (also a template). An instance of either of these is called through either MakeQueue or MakeStack returning a Container*.
EDIT: the code is in the second last post.
[edited by - The Bag on February 19, 2003 6:23:27 PM]
You seem to have a variety of errors...I'm surprised that it's even linking.
You templated the function, but if that is your exact declaration...then it's not correct. Here's what it should be:
I may be misunderstanding what you're trying to do, but that seems to be what I see.
[edited by - CpMan on February 19, 2003 12:17:15 PM]
[edited by - CpMan on February 19, 2003 12:20:11 PM]
[edited by - CpMan on February 19, 2003 12:22:58 PM]
STUPID < >'s
[edited by - CpMan on February 19, 2003 12:24:46 PM]
[edited by - CpMan on February 19, 2003 12:28:13 PM]
You templated the function, but if that is your exact declaration...then it's not correct. Here's what it should be:
class Container {
public:
template < typename T >
static Container* MakeStack() {
return (Container*)(new Stack< T >());
}
};
template < typename T >
class Stack : public Container {
public:
T a;
};
Used like:
int _tmain(int argc, _TCHAR* argv[])
{
Container* c;
c = Container::MakeStack< int >();
return 0;
}
I may be misunderstanding what you're trying to do, but that seems to be what I see.
[edited by - CpMan on February 19, 2003 12:17:15 PM]
[edited by - CpMan on February 19, 2003 12:20:11 PM]
[edited by - CpMan on February 19, 2003 12:22:58 PM]
STUPID < >'s
[edited by - CpMan on February 19, 2003 12:24:46 PM]
[edited by - CpMan on February 19, 2003 12:28:13 PM]
Thanks that has fixed what I was trying to do.
One problem, when I compile main I get this:
[edited by - The Bag on February 19, 2003 12:34:11 PM]
One problem, when I compile main I get this:
Compiling...
main.cpp
E:\Documents and Settings\M Hanlon\Desktop\test\main.cpp(11) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1794)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
[edited by - The Bag on February 19, 2003 12:34:11 PM]
Visual C++6 by my guess...
1) Make sure you have the latest SP installed. This may fix it.
2) There are certain conditions on which the compile will fail when certain template definitions/declarations are made. libGDN has run into a few of these, and although I don''t think that that code will do it...it''s possible. Do you have anymore template usage elsewhere?
Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
1) Make sure you have the latest SP installed. This may fix it.
2) There are certain conditions on which the compile will fail when certain template definitions/declarations are made. libGDN has run into a few of these, and although I don''t think that that code will do it...it''s possible. Do you have anymore template usage elsewhere?
Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
okay I just got someone to test it on VC7 - which gives the following error:
the project is split into 3 files,
main.cpp:
Container.h:
and container.cpp:
Any ideas where I''m going wrong - this is my first attempt at templates so be gentle on me ;-).
thx
test error LNK2019: unresolved external symbol "public: static class* __cdecl CContainer::MakeStack (void)" (?MakeStack@?$@H@CContainer@@SAPAV1@XZ) referenced in function _main
the project is split into 3 files,
main.cpp:
#include "Container.h"
int main()
{
Container* c;
c = Container::MakeStack<int>();
return 0;
}
Container.h:
#define MAX 100
class CContainer
{
public:
CContainer();
virtual ~CContainer();
template<class Type> static CContainer* MakeStack template<class Type> static CContainer* MakeQueue();
virtual void put(int add) = 0;
virtual int get() = 0;
};
and container.cpp:
#include "Container.h"
template <class Type>class CStack : public Container{
Type stack[MAX];
int tos;
public:
CStack(){
tos = 0;
}
void put(int add){
if(tos == MAX){
cout << "OVERFLOW" << endl;
}else{
stack[tos] = add;
tos++;
}
}
int get(){
if(tos == 0){
cout << "UNDERFLOW" << endl;
return 0;
}else{
tos--;
return stack[tos];
}
}
};
template <class Type> class CQueue : public Container{
Type queue[MAX];
int toq;
public:
CQueue(){
toq = 0;
}
void put(int add){
if(toq == 100){
cout << "OVERFLOW" << endl;
// OVERFLOW
}else{
queue[toq] = add;
toq++;
}
}
int get(){
if(toq == 0){
cout << "UNDERFLOW" << endl;
// UNDERFLOW
return 0;
}else{
// move everything down one
int temp = queue[0];
toq--;
for(int i = 0;i<toq; i++){
queue[i] = queue[i+1];
}
return temp;
}
}
};
template <class Type> CContainer* CContainer::MakeStack(){
return (CContainer*)(new CStack<Type>());
}
template <class Type> CContainer* CContainer::MakeQueue(){
return (CContainer*)(new CQueue<Type>());
}
Any ideas where I''m going wrong - this is my first attempt at templates so be gentle on me ;-).
thx
Right I've now changed it too (latest code):
Container.h:
Container.cpp:
main:
but this line of main:
c = CContainer::MakeStack > int < ();
is still giving the same problem
[edited by - The Bag on February 19, 2003 5:18:43 PM]
[edited by - The Bag on February 19, 2003 5:39:08 PM]
Container.h:
#define MAX 100
template <class Type> class CStack;
class CContainer
{
public:
template<class Type> static CContainer* MakeStack();
template<class Type> void put(Type add);
template<class Type> Type get();
};
template <class Type>class CStack : public CContainer{
Type stack[MAX];
int tos;
public:
CStack();
void put(Type add);
Type get();
};
Container.cpp:
#include <iostream>
#include "Container.h"
using namespace std;
template <class Type> CStack<Type>::CStack(){
tos = 0;
}
template <class Type> void CStack<Type>::put(Type add){
if(tos == MAX){
cout << "OVERFLOW" << endl;
//stack overflow
}else{
stack[tos] = add;
tos++;
}
}
template <class Type> Type CStack<Type>::get(){
if(tos == 0){
cout << "UNDERFLOW" << endl;
//stack underflow
return 0;
}else{
tos--;
return stack[tos];
}
}
template <class Type> CContainer* CContainer::MakeStack(){
return new CStack<Type>();
}
main:
#include "Container.h"
using namespace std;
int main()
{
CContainer* c;
c = CContainer::MakeStack<char>();
return 0;
}
but this line of main:
c = CContainer::MakeStack > int < ();
is still giving the same problem
[edited by - The Bag on February 19, 2003 5:18:43 PM]
[edited by - The Bag on February 19, 2003 5:39:08 PM]
If you're using templates, you can't hide the code, sorry.
Just think about it. Templated code has to be visible at compile time, because it's compiled based on the template parameters you give - the types. You can't compile the template code separate from where it's used because it doesn't have any types yet! Therefore, all template definitions must be in the header. Any C++ book on template programming will show you this.
Frankly, I don't think your code is going to work at all the way you have it structured. Is this a class assignment? If it is, I can't help you any further, but if not, post your code as it currently stands and I'll critique it more.
[edited by - fizban75 on February 19, 2003 7:49:23 PM]
Just think about it. Templated code has to be visible at compile time, because it's compiled based on the template parameters you give - the types. You can't compile the template code separate from where it's used because it doesn't have any types yet! Therefore, all template definitions must be in the header. Any C++ book on template programming will show you this.
Frankly, I don't think your code is going to work at all the way you have it structured. Is this a class assignment? If it is, I can't help you any further, but if not, post your code as it currently stands and I'll critique it more.
[edited by - fizban75 on February 19, 2003 7:49:23 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement