missing type specifier - int assumed??

This is a discussion on missing type specifier - int assumed?? within the C++ Programming forums, part of the General Programming Boards category; the error is in push declaration beside closing bracket... Code: #include <list> #include <iostream> using namespace std; template<class T> class ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    missing type specifier - int assumed??

    the error is in push declaration beside closing bracket...

    Code:
    #include <list>
    #include <iostream>
    using namespace std;
    
    template<class T>
    class Stack
    {
     public:
       Stack(void);      // default constructor
       void push(const T& item);  // add item to the Stack
      private:
       list<T> stackList;
    
    };
    template<class T>
    Stack<T>::Stack(void)
    {
    
    }
    
    template<class T>
    Stack<T>::push(const T& item)
    {
    	stackList.insert(size(),item);
    }
    
    int main()
    {
    	Stack<int> object;
    
    	for (int i = 0; i < 10; i++)
    	{
    		object.push(i);
    	}
    		
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    list::insert takes an iterator as the parameter indicating the place to insert, not an index.

    insert - C++ Reference

    you could use something like "stackList.insert(stackList.end(), item)" instead

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    30
    yes kya i agree. ok i insert at beginning of stack now with this code but how do i get my print function to work? i get a syntax error on the line that i pass ostream& to print function...
    Code:
    #include <list>
    #include <ostream>
    #include <iostream>
    using namespace std;
    
    template<class T>
    class Stack
    {
    public:
    Stack(void); // default constructor
    void push(const T& item); // add item to the Stack
    void printStack(ostream &out); // prints the values in stack to stream out
    private:
    list<T> stackList;
    
    };
    template<class T>
    Stack<T>::Stack(void)
    {
    
    }
    
    template<class T>
    void Stack<T>::push(const T& item)
    {
    stackList.insert(stackList.begin(),item);
    }
    
    template<class T>
    void Stack<T>::printStack(ostream &out)
    {
    list<int>::iterator iter;
    for (iter = stackList.begin(); iter < stackList.end(); iter++)
    out << *iter;
    }
    
    int main()
    {
    Stack<int> object;
    
    for (int i = 0; i < 10; i++)
    {
    object.push(i);
    }
    
    object.printStack(ostream&);
    
    
    return 0;
    
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It usually helps to post the error messages you're getting.

    Anyway, the problem is that printStack expects an actual object (eg: cout, cerr, etc). Another problem is that inside printStack you're comparing list iterators with <, but they aren't guaranteed to support that, so use != instead. Also, when using nested types within templates you should make it a habit to prepend the type with 'typename', eg:

    Code:
    typename list<int>::iterator iter;
    Besides all that, learn how to indent your code - it makes it much more readable.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    30
    thanks for all the help sebasiani ive got it working now.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by Sebastiani
    Also, when using nested types within templates you should make it a habit to prepend the type with 'typename'
    I disagree, but the code that you modified by way of example shows that there is another problem: stackList is of type std::list<T>, but iter is of type std::list<int>::iterator, which happens to work because the test is done with a Stack<int>. Rather, iter should be of type typename std::list<T>::iterator, and now the disambiguation with typename is appropriate.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    I disagree, but the code that you modified by way of example shows that there is another problem: stackList is of type std::list<T>, but iter is of type std::list<int>::iterator, which happens to work because the test is done with a Stack<int>. Rather, iter should be of type typename std::list<T>::iterator, and now the disambiguation with typename is appropriate.
    Ah, yes, I overlooked the list<int> declaration, and I literally cut and pasted my example from the OP's submission. Lazy and distracted - great combination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  5. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21