Nick B.
Nick B.

Reputation: 25

template "error: expected expression" in object declaration

I'm receiving an error code when trying to declare a new stack object that uses a template class. I can't seem to find anything regarding this and most of the questions asked that I've found seem to refer to be for creating functions. Any help on how to resolve this would be greatly appreciated.

#include <stdio.h>
#include <iostream>
#include "stack4.h"

using namespace main_savitch_6B;
using namespace std;
int main(int argc, char **argv)
{
    template <class Item>  //error: expected expression
    stack a;

    a.push(4);



    return 0;
}

my header file:

#ifndef MAIN_SAVITCH_STACK4_H
#define MAIN_SAVITCH_STACK4_H
#include <cstdlib>   // Provides NULL and size_t
#include "node2.h"   // Node template class from Figure 6.5 on page 308

namespace main_savitch_6B  //7B
{
    template <class Item>
    class stack
    {
    public:
        // TYPEDEFS 
        typedef std::size_t size_type;
        typedef Item value_type;
        // CONSTRUCTORS and DESTRUCTOR
        stack( ) { top_ptr = NULL; }
        stack(const stack& source);
        ~stack( ) { list_clear(top_ptr); }
        // MODIFICATION MEMBER FUNCTIONS
        void push(const Item& entry);
        void pop( );
        void operator =(const stack& source);
        Item& top( );
        void swap(stack& y);
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const
        { return main_savitch_6B::list_length(top_ptr); }
        bool empty( ) const { return (top_ptr == NULL); }
        const Item& top( ) const;

    private:
        main_savitch_6B::node<Item> *top_ptr;  // Points to top of stack
    };
}

#include "stack4.template" // Include the implementation 
#endif

Upvotes: 0

Views: 898

Answers (1)

iBug
iBug

Reputation: 37317

stack<int> a;

if you want to define an instance of a template class

Upvotes: 1

Related Questions