[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Having problems with static template function in template class.

Posted on 2003-12-09
4
Medium Priority
?
461 Views
Last Modified: 2008-02-01
I'm getting the following errors:

v:\code\vcprograms\include\stackframe.h(90) : error C2244: 'StackFrame<Any>::push' : unable to resolve function overload
v:\code\vcprograms\include\stackframe.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>::StackFrame()
{
   cout << "\nConstructing StackFrame...\n" << endl;
}

template <typename Any>
StackFrame<Any>::StackFrame(const StackFrame &pOrig)
{
   cout << "\nConstructing StackFrame...\n" << endl;
}

template <typename Any>
StackFrame<Any>::~StackFrame()
{
   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;
}
0
Comment
Question by:mflores88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 500 total points
ID: 9905759
@mflores88:

>    static bool push(Any &pData, StackFrame pStack);
>    static Any pop(StackFrame &pStack);

Try changing the above to lines to look like this:

   static bool push(Any &pData, StackFrame<Any> pStack);
   Any pop();
   static Any pop(StackFrame<Any> &pStack);

Hope That Helps,
Dex*
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9905984
Actually, if "push" is going to modify pStack, then it should be a reference, like this:
     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 );
0
 

Author Comment

by:mflores88
ID: 9906000
Yeah, your prior comment actually got me to pay more attention to that also.

Thanks!
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9906037
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<--------

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question