mflores88
asked on
Having problems with static template function in template class.
I'm getting the following errors:
v:\code\vcprograms\include \stackfram e.h(90) : error C2244: 'StackFrame<Any>::push' : unable to resolve function overload
v:\code\vcprograms\include \stackfram e.h(93) : error C2954: template definitions cannot nest
Here is my class. When I comment out the 2 static methods I don't have a problem. So it has something to do with those 2. What am I doing wrong? Thanks!
//************************ ********** ********** ********** ********** ********** ********** **********
// Definition of the StackFrame class.
//************************ ********** ********** ********** ********** ********** ********** **********
#include <iostream>
#include <list>
using namespace std;
template <typename Any>
typedef list<typename Any> List; //define list of Any type
typedef List::iterator Iter; //define itererator type
template <typename Any>
class StackFrame
{
public: //Accessible to the world
StackFrame(); //Default constructor
StackFrame(const StackFrame &pOrig); //Copy constructor, copy obj passed in
virtual ~StackFrame(); //Default destructor
virtual StackFrame &operator=(const StackFrame &pOrig);
friend std::ostream &operator<<(std::ostream &pout, const StackFrame &pSource);
bool push(Any &pData);
static bool push(Any &pData, StackFrame pStack);
Any pop();
static Any pop(StackFrame &pStack);
private:
List mlstStack;
};
//************************ ********** ********** ********** ********** ********** ********** **********
// Implementation of the StackFrame class.
//************************ ********** ********** ********** ********** ********** ********** **********
template <typename Any>
StackFrame<Any>::StackFram e()
{
cout << "\nConstructing StackFrame...\n" << endl;
}
template <typename Any>
StackFrame<Any>::StackFram e(const StackFrame &pOrig)
{
cout << "\nConstructing StackFrame...\n" << endl;
}
template <typename Any>
StackFrame<Any>::~StackFra me()
{
cout << "\nDestructing StackFrame...\n" << endl;
}
template <typename Any>
StackFrame<Any> &StackFrame<Any>::operator =(const StackFrame &pOrig)
{
if (this == &pOrig) //Self-assignment?
return *this; //Just return itself.
//need to add code to copy List
return *this;
}
template <typename Any>
std::ostream &operator<<(std::ostream &pout, const StackFrame<Any> &pSource)
{
//need to change this to loop through List and print each element
//pout << "name: " << pSource.mstrName << ", age: " << pSource.muintAge << ", sex: " << strSex;
return pout;
}
//Push data on to this StackFrame
template <typename Any>
bool StackFrame<Any>::push(Any &pData)
{
mlstStack.push_front(pData );
}
//Push data on to the indicated StackFrame
//static = no instantiation necessary
template <typename Any>
static bool StackFrame<Any>::push(Any &pData, StackFrame &pStack)
{
pStack.push_front(pData);
}
//Pop data off of this StackFrame
template <typename Any>
Any StackFrame<Any>::pop()
{
if(mlstStack.size() > 0)
{
return mlstStack.pop_front();
}
return null;
}
v:\code\vcprograms\include
v:\code\vcprograms\include
Here is my class. When I comment out the 2 static methods I don't have a problem. So it has something to do with those 2. What am I doing wrong? Thanks!
//************************
// Definition of the StackFrame class.
//************************
#include <iostream>
#include <list>
using namespace std;
template <typename Any>
typedef list<typename Any> List; //define list of Any type
typedef List::iterator Iter; //define itererator type
template <typename Any>
class StackFrame
{
public: //Accessible to the world
StackFrame(); //Default constructor
StackFrame(const StackFrame &pOrig); //Copy constructor, copy obj passed in
virtual ~StackFrame(); //Default destructor
virtual StackFrame &operator=(const StackFrame &pOrig);
friend std::ostream &operator<<(std::ostream &pout, const StackFrame &pSource);
bool push(Any &pData);
static bool push(Any &pData, StackFrame pStack);
Any pop();
static Any pop(StackFrame &pStack);
private:
List mlstStack;
};
//************************
// Implementation of the StackFrame class.
//************************
template <typename Any>
StackFrame<Any>::StackFram
{
cout << "\nConstructing StackFrame...\n" << endl;
}
template <typename Any>
StackFrame<Any>::StackFram
{
cout << "\nConstructing StackFrame...\n" << endl;
}
template <typename Any>
StackFrame<Any>::~StackFra
{
cout << "\nDestructing StackFrame...\n" << endl;
}
template <typename Any>
StackFrame<Any> &StackFrame<Any>::operator
{
if (this == &pOrig) //Self-assignment?
return *this; //Just return itself.
//need to add code to copy List
return *this;
}
template <typename Any>
std::ostream &operator<<(std::ostream &pout, const StackFrame<Any> &pSource)
{
//need to change this to loop through List and print each element
//pout << "name: " << pSource.mstrName << ", age: " << pSource.muintAge << ", sex: " << strSex;
return pout;
}
//Push data on to this StackFrame
template <typename Any>
bool StackFrame<Any>::push(Any &pData)
{
mlstStack.push_front(pData
}
//Push data on to the indicated StackFrame
//static = no instantiation necessary
template <typename Any>
static bool StackFrame<Any>::push(Any &pData, StackFrame &pStack)
{
pStack.push_front(pData);
}
//Pop data off of this StackFrame
template <typename Any>
Any StackFrame<Any>::pop()
{
if(mlstStack.size() > 0)
{
return mlstStack.pop_front();
}
return null;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah, your prior comment actually got me to pay more attention to that also.
Thanks!
Thanks!
typedef templates aren't legal
--------8<--------
template <typename Any>
typedef list<typename Any> List; //define list of Any type
--------8<--------
You can do the following, if you want to use an uppercase 'L' :-)
--------8<--------
template<typename T,typename Alloc = std::allocator<T> > class List : public std::list<T,Alloc> {};
--------8<--------
--------8<--------
template <typename Any>
typedef list<typename Any> List; //define list of Any type
--------8<--------
You can do the following, if you want to use an uppercase 'L' :-)
--------8<--------
template<typename T,typename Alloc = std::allocator<T> > class List : public std::list<T,Alloc> {};
--------8<--------
static bool push( Any &pData, StackFrame<Any>& pStack );
If it isn't going to be modifying pStack, then you don't need it... Actually, in that case, then I would do it like this:
static bool push( Any &pData, const StackFrame<Any>& pStack );