GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Dec-2006, 13:41
ankakusu ankakusu is offline
New Member
 
Join Date: Dec 2006
Posts: 2
ankakusu is on a distinguished road

Template usage - global functions


How can I solve the errors?


CPP / C++ / C Code:
//AlgebraicExpression.h
#include <string>
using std::string;

#include "Stack.h"

string infix2postfix( const string);
int CheckTypeOfChar(const char);
int precedence(const char);
void toStack(const string );


Stack<char> operands;
Stack<int> operators;


CPP / C++ / C Code:
//ALgebraicExpression.cpp
#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include "AlgebraicExpression.h"

string infix2postfix( const string exp ){
   string postFix=" ";
   
   for(int index=0;index<exp.length();index++ ){
      int ch=CheckTypeOfChar(exp[index]);
      switch (ch)
      {
         case 1:
         (int) exp[index];
         operands.push(exp[index]);
         break;
         case 2:
            
            while (!operators.isEmpty()&&operators.getTopPtr()->item!='('&& precedence(ch) <= precedence(operators.getTopPtr()->item)){
               postFix += operators.getTopPtr()->item;
               operators.pop();
            }
         operators.push(exp[index]);
         //postFix+=exp[index];
         break;
         case 3:
         operators.push(exp[index]);   
         break;
         case 4:
            while ( operators.getTopPtr() -> item != '('){
                  postFix += operators.getTopPtr() -> item;
                  operators.pop(T);
                  //cout<< "operators.getTopPtr()->item" <<operators.getTopPtr()->item<<endl;
               }
            operators.pop();
            //cout<< "operators.getTopPtr()->item" <<operators.getTopPtr()->item<<endl;
         operators.push(exp[index]);
         break;
      }
   }
   return postFix;
}

int CheckTypeOfChar(const char ch){
   if(ch =='1' || ch =='2'||ch =='3'||ch =='4'||ch =='5'||ch =='6'||ch =='7'||ch =='8'||ch =='9')
      return 1;
   else if (ch == '*' || ch =='/' || ch =='+' || ch =='-' )
      return 2;
   else if (ch == '(')
      return 3;
   else if ( ch ==')')
      return 4;
}

int precedence (const char ch1){
   char operatorTypes[]={'*','/','+','-','(',')'};
      if(ch1==operatorTypes[0] || ch1==operatorTypes[1] )
         return 2;
      else if (ch1==operatorTypes[2] || ch1==operatorTypes[3] )
         return 1;
   
}

void toStack(const string exp )
{
   for(int index=0;index<exp.length();index++ )
   {
      int ch=CheckTypeOfChar(exp[index]);
      switch (ch)
      {
         case 1:
         (int) exp[index];
         operands.push(exp[index]);
         break;
         case 2:
         operators.push(exp[index]);
         break;
         case 3:
         operators.push(exp[index]);   
         break;
         case 4:
         operators.push(exp[index]);
         break;
      }
   }
};

CPP / C++ / C Code:

//Stack.h

template < typename T>
class Stack
{
friend class AlgebraicExpression;

public:

Stack();
Stack(const Stack&);
~Stack();

bool isEmpty () const;
void push(T newItem);
void pop ();
void pop(T& stackTop);
void getTop(T& stackTop);
StackNode getTopPtr();

private:
struct StackNode
{
   T item;   
   StackNode *next;
};

StackNode *topPtr;
int size;
};

CPP / C++ / C Code:

//Stack.cpp

#include <cstddef>

#include "Stack.h"

template < typename T>
Stack<T>::Stack() : topPtr(NULL)
{
}
template < typename T>
Stack<T>::Stack(const Stack& aStack)
{
   if (aStack.topPtr == NULL)
      topPtr = NULL;  // original list is empty

   else
   {  // copy first node
   
      topPtr = new StackNode;
      topPtr->item = aStack.topPtr->item;

      // copy rest of list
      StackNode *newPtr = topPtr;    // new list pointer

      for (StackNode *origPtr = aStack.topPtr->next;origPtr != NULL; origPtr = origPtr->next)
      {  newPtr->next = new StackNode;
         newPtr = newPtr->next;
         newPtr->item = origPtr->item;
      }  // end for
      newPtr->next = NULL;
   }  // end if
}  // end copy constructor

template <class T>
Stack<T>::~Stack()
{
   // pop until stack is empty
   while (!isEmpty())
      pop();
   // Assertion: topPtr == NUL
}  // end destructor

template <class T>
bool Stack<T>::isEmpty() const
{
   return topPtr == NULL;
}  // end isEmpty

template < typename T>
void Stack<T>::push(T newItem)
{
   // create a new node
   StackNode *newPtr = new StackNode;

   // set data portion  of new node
   newPtr->item = newItem;
   // insert the new node
   newPtr->next = topPtr;
   topPtr = newPtr;
}  // end push

template <class T>
void Stack<T>::pop() //throw(StackException)
{
   if (isEmpty());
      //throw StackException("StackException: stack empty on pop");

   else
   {  // stack is not empty; delete top
      StackNode *temp = topPtr;
      topPtr = topPtr->next;
      // return deleted node to system
      temp->next = NULL;  // safeguard
      delete temp;
   }  // end if
}  // end pop

template < typename T>
void Stack< T >::pop(T& stackTop) // throw(StackException)
{
   if (isEmpty());
//      throw StackException("StackException: stack empty on pop");

   else
   {  // stack is not empty; retrieve and delete top
      stackTop = topPtr->item;
      StackNode *temp = topPtr;
      topPtr = topPtr->next;

      // return deleted node to system
      temp->next = NULL;  // safeguard
      delete temp;
   }  // end if
}  // end pop

template < typename T>
void Stack<T>::getTop(T& stackTop) //const //throw(StackException)
{
   if (isEmpty());
//      throw StackException("StackException: stack empty on getTop");

   else
      // stack is not empty; retrieve top
      stackTop = topPtr->item;
}  // end getTop
// End of implementation file.
template <typename T>
StackNode Stack<T>::getTopPtr (){
   return topPtr;
}

CPP / C++ / C Code:
//driver.cpp
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include "AlgebraicExpression.h"

int main(){
	cout<<infix2postfix("3+4+5");
	return 0;
};

Error:
Code:
Stack.h:17: hata: 'StackNode' does not name a type Stack.cpp:107: hata: expected constructor, destructor, or type conversion before 'Stack' Stack.h:17: hata: 'StackNode' does not name a type AlgebraicExpression.cpp: In function 'std::string infix2postfix(std::string)': AlgebraicExpression.cpp:23: hata: 'class Stack<int>' has no member named 'getTopPtr' AlgebraicExpression.cpp:23: hata: 'class Stack<int>' has no member named 'getTopPtr' AlgebraicExpression.cpp:24: hata: 'class Stack<int>' has no member named 'getTopPtr' AlgebraicExpression.cpp:34: hata: 'class Stack<int>' has no member named 'getTopPtr' AlgebraicExpression.cpp:35: hata: 'class Stack<int>' has no member named 'getgetTopPtr' AlgebraicExpression.cpp:36: hata: 'T' was not declared in this scope Stack.h:17: hata: 'StackNode' does not name a type
Last edited by LuciWiz : 18-Dec-2006 at 14:07. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 18-Dec-2006, 14:53
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: template usage - global functions


Quote:
Stack.h:17: hata: 'StackNode' does not name a type
This points to the line
CPP / C++ / C Code:
StackNode getTopPtr();
The compiler doesn't know what a StackNode is yet. Put the definition of struct StackNode before any code that uses it.

Quote:
Stack.cpp:107: hata: expected constructor, destructor, or type conversion before 'Stack'
pointing to the line
CPP / C++ / C Code:
StackNode Stack<T>::getTopPtr (){
The compiler can't find StackNode again, but for a different reason: in Stack.h, struct StackNode is declared inside class Stack. You'd have to refer to it as Stack<T>::StackNode.

The next four errors should be solved once you fix the error on line 107 and the compiler acknowledges the existence of the function getTopPtr.

CPP / C++ / C Code:
operators.pop(T);
On this line, the compiler has no idea what T is. I don't either.
  #3  
Old 18-Dec-2006, 15:12
ankakusu ankakusu is offline
New Member
 
Join Date: Dec 2006
Posts: 2
ankakusu is on a distinguished road

Re: template usage - global functions


thank you ubergerek. your helps worked a lot but now it gives different errors now:

CPP / C++ / C Code:
template < typename T>
class Stack
{
public:
struct StackNode
{
	T item;	
	StackNode *next;
};


Stack();
Stack(const Stack&);
~Stack();

bool isEmpty () const;
void push(T newItem);
void pop ();
void pop(T& stackTop);
void getTop(T& stackTop);
StackNode getTopPtr();

private:

StackNode *topPtr;
int size;
};



error:
Code:
AlgebraicExpression.cpp: In function 'std::string infix2postfix(std::string)': AlgebraicExpression.cpp:23: hata: base operand of '->' has non-pointer type 'Stack<int>::StackNode' AlgebraicExpression.cpp:23: hata: base operand of '->' has non-pointer type 'Stack<int>::StackNode' AlgebraicExpression.cpp:24: hata: base operand of '->' has non-pointer type 'Stack<int>::StackNode' AlgebraicExpression.cpp:34: hata: base operand of '->' has non-pointer type 'Stack<int>::StackNode' AlgebraicExpression.cpp:35: hata: base operand of '->' has non-pointer type 'Stack<int>::StackNode'

by the way I want to ask some other question: If I want to reach the topPtr in Stack.h ( which is private member ) not by function getTopPtr() but declaring AlgebraicExpression class as a friend function. But AlgebraicExpression is global. where can I define the friendship of these classes?

thanks a lot
Last edited by LuciWiz : 19-Dec-2006 at 16:07. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #4  
Old 18-Dec-2006, 15:18
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: template usage - global functions


The problem is getTopPtr is declared as returning a StackNode; change it to a StackNode*. In the class Stack you can put (instead of friend class -- there is no class AlgebraicExpression, just a file named that) "friend ..." and then the signature (prototype) of the function infix2postfix. I think that should answer your question.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fatal error C1083: Cannot open include file mia C++ Forum 5 14-May-2007 15:35
error template with C linkage MichaelS-R C++ Forum 6 17-May-2006 11:09
Testing a template class earachefl C++ Forum 7 13-May-2006 17:21
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 06:17
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 08:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:58.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.