TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ALL
from: JOHNNY BRAZEAL
date: 1998-04-30 18:12:00
subject: g++ & templates

 
 
 
I believe this is my ignorance in my coding, humph /:-( 
or a fixme yet to be in gnu g++: Feb 26 1998 B19 Win32 on NT 
Thanks in advance :-) 
 
goal: use different files for template based classes, main, and headers. 
trys: compile/run fine when implimentatons are in same file/scope of 
instantiati 
obstacle: either stack dump or undefined reference(s) from make. 
question:  work around? fix? corrections to code? 
 
below is the coding for "goal". 
trys coded with  in same file/scope. 
 
*.h ========================= 
const int defaultStack = 50; 
 
template  
class Stack 
{  // vars 
   int top; 
   T   *data; 
public: 
   Stack(int size = defaultStack); 
   Stack(char*); 
   ~Stack(); 
   int getsize(); 
   void push(T); 
   T    pop(); 
}; 
 
*.cpp ======================= 
#include "stack.h" 
 
const int defaultSize = 128; 
 
template  
Stack::Stack(int s) 
{  // "vars" = 0; 
   data = new T(size); 
} 
 
template  
Stack::Stack(char* s) 
{  // "vars" = 0; 
   data = new T(size); 
} 
 
template  
Stack::~Stack() 
{  delete [] data; 
} 
 
template  
void Stack::push(T d) 
{   data[top++] = d; 
} 
 
template  
int Stack::getsize() 
{  return top; 
} 
 
template  
T Stack::pop() 
{  return data[--top]; 
   } 
 
main.cpp ==================== 
#include *.h   // all... 
int main( void ) 
{  int i; 
   Stack nInt(20); 
   nInt.push(5); 
   cout << "pushed 5" << endl; 
   for( int j=0;j<2;++j ) 
   {  i = nInt.pop(); 
      cout << "i is poped: " << i << endl; 
      } 
 
   Stack cpStack(20);  // stack of strings 
   cpStack.push("first word"); 
   cpStack.push("second word"); 
   cpStack.push("number three word"); 
 
   cout << endl << "here are " << cpStack.getsize() << " strings" << endl; 
   for( int j=0;j < cpStack.getsize()+2;++j ) 
      cout << endl << cpStack.pop(); 
   cout << endl << "stack size " << cpStack.getsize() << endl; 
   cout  << endl << "end main"; 
   return 0; 
} 
___ 
 * OFFLINE 1.58 
--- Maximus/2 2.02
---------------
* Origin: The HUB * Austin TX * Centex PCUG * 512-346-1852 (1:382/1201)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.