0

Errors:
delimiters.cpp(41): error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator + (_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'char' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(434) : see declaration of 'std::operator +'

stacklinked.cpp(20): error C2061: syntax error : identifier 'StackNode'
stacklinked.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
stacklinked.cpp(28): error C2063: 'StackNode' : not a function
stacklinked.cpp(28): fatal error C1903: unable to recover from previous error(s); stopping compilation

Responsive Code:

#ifndef STACKLINKED_CPP
#define STACKLINKED_CPP

#include <iostream>

#include "StackLinked.h"

//--------------------------------------------------------------------

template <typename DataType>
StackLinked<DataType>::StackNode(const DataType& newDataItem,
                  StackLinked<DataType>::StackNode* nextPtr)

//::StackNode
// Creates a stack node containing item newDataItem and next pointer
// nextPtr.

    : dataItem(newDataItem), next(nextPtr)
{
}

//--------------------------------------------------------------------

template <typename DataType>
StackLinked<DataType>::StackLinked(int maxNumber) 
    : top(0) 

// Creates an empty stack. The parameter maxNumber is provided for 
// compatability with the array implementation and is ignored.

{
}

//--------------------------------------------------------------------

template <typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)

// Copy constructor for linked stack

: top( 0 )
{
    (void) operator=(other);    // Use operator=, ignore return value

    /*
    // Alternatively, could duplicate essentially all the code from
    // operator= and insert below.
    if( ! other.isEmpty() ) {
    // Copy first node
    top = new StackNode(other.top->dataItem, 0);
    StackNode *otherTemp = other.top->next; 
    StackNode *thisTemp=0, *thisPrevious=top;

    // Copy rest of nodes
    while( otherTemp != 0 )
    {
        thisTemp = new StackNode(otherTemp->dataItem, 0);
        thisPrevious->next = thisTemp;
        thisPrevious = thisTemp;
        otherTemp = otherTemp->next;
    }

    }
    */
}

//--------------------------------------------------------------------

template <typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)

// Overloaded assignment operator for the StackLinked class.
// Because this function returns a StackLinked object reference,
// it allows chained assignment (e.g., stack1 = stack2 = stack3).

{
    // Self-assignment protection
    if( this != &other ) return *this;

    clear();                // Clear existing nodes
    if( ! other.isEmpty() ) 
{
    // Copy first node
    top = new StackNode(other.top->dataItem, 0);
    StackNode *otherTemp = other.top->next; 
    StackNode *thisTemp=0, *thisPrevious=top;

    // Copy rest of nodes
    while( otherTemp != 0 )
    {
    thisTemp = new StackNode(otherTemp->dataItem, 0);
    thisPrevious->next = thisTemp;
    thisPrevious = thisTemp;
    otherTemp = otherTemp->next;
    }
    }

    return *this;
}

//--------------------------------------------------------------------

template <typename DataType>
StackLinked<DataType>::~StackLinked() 

// Destructor. Frees the memory used by a stack.

{
    clear();
}

//--------------------------------------------------------------------

template <typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw (logic_error) 

// Inserts newDataItem onto the top of a stack.

{
    if (isFull()) {
    // Not likely with linked implementation
    throw logic_error("push() while stack full");   
}

    top = new StackNode(newDataItem, top);
}

//--------------------------------------------------------------------

template <typename DataType>
DataType StackLinked<DataType>::pop() throw (logic_error) 

// Removes the topmost item from a stack and returns it.

{
    if (isEmpty()) {
    throw logic_error("pop() while stack empty");
}

    StackNode* temp = top;
    top = top->next;

    DataType value = temp->dataItem;
    delete temp;

    return value;
}

//--------------------------------------------------------------------

template <typename DataType>
void StackLinked<DataType>::clear() 

// Removes all the data items from a stack.

{
    for (StackNode* temp = top; top != 0; temp = top) 
{
    top = top->next;

    delete temp;
}
// Invariant: At this point in the code, top == 0.
// Top does not heed to explicitly set to 0. It was
// either 0 before the loop, or emerged from the loop as 0.
}

//--------------------------------------------------------------------

template <typename DataType>
bool StackLinked<DataType>::isEmpty() const 

// Returns true if a stack is empty. Otherwise, returns false.

{
    return top == 0;
}

//--------------------------------------------------------------------

template <typename DataType>
bool StackLinked<DataType>::isFull() const 

// Returns true if a stack is full. Otherwise, returns false. 

{
    return false;

    /*
    // Alternatively, can use implementation below.
    // This is a somewhat awkward way to test if the list is full.
    // If a node can be successfully allocated than the list is not
    // full.  If the allocation fails it is implied that there is no
    // more free memory therefore the list is full.
    // We are not aware of any other standard/portable way of 
    // performing the test. And this can fail due to external issues
    // such as the system exhausting swap or another thread stealing
    // the remaining memory between when isFull returns its result and
    // the caller does something that assumes that isFull() returned
    // a valid answer.
    //
    // Alternatives include just the line "return false", which is
    // probably good enough in this context, or platform-dependent
    // checks for available memory.
    StackNode* temp;
    DataType junk;

    try
    {
        temp = new StackNode( junk, 0 );
    }
    catch ( bad_alloc &e )
    {
        return true;
    }

    delete temp;
    return false;
    */
}

//--------------------------------------------------------------------

template <typename DataType>
void StackLinked<DataType>::showStructure() const 

// Linked list implementation. Outputs the data elements in a stack.
// If the stack is empty, outputs "Empty stack". This operation is
// intended for testing and debugging purposes only.

{
    if( isEmpty() )
    {
    cout << "Empty stack" << endl;
    }
    else
    {
        cout << "Top\t";
    for (StackNode* temp = top; temp != 0; temp = temp->next) {
        if( temp == top ) 
        {
        cout << '[' << temp->dataItem << "]\t";
    }
        else 
        {
    cout << temp->dataItem << "\t";
    }
}
        cout << "Bottom" << endl;
    }
}

#endif      //#ifndef STACKLINKED_CPP

Here's the header file, StackLinked.h

//--------------------------------------------------------------------
//
//  Laboratory 6                                          StackArray.h
// 
//  Class declaration for the array implementation of the Stack ADT
//
//--------------------------------------------------------------------

#ifndef STACKARRAY_H
#define STACKARRAY_H

#include <stdexcept>
#include <iostream>

using namespace std;

#include "Stack.h"

template <typename DataType>
class StackLinked : public Stack<DataType> {

  public:

    StackLinked(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
    StackLinked(const StackLinked& other);
    StackLinked& operator=(const StackLinked& other);
    ~StackLinked();

    void push(const DataType& newDataItem) throw (logic_error);
    DataType pop() throw (logic_error);

    void clear();

    bool isEmpty() const;
    bool isFull() const;

   void showStructure() const;

  private:

    class StackNode {
      public:
    StackNode(const DataType& nodeData, StackNode* nextPtr);

    DataType dataItem;
    StackNode* next;
    };

    StackNode* top;
};

#endif      //#ifndef STACKARRAY_H  

Here's another header file, Stack.h

//--------------------------------------------------------------------
//
//  Laboratory 6                                               Stack.h
// 
//  Class declaration of the abstract class interface to be used as
//  the basis for implementations of the Stack ADT.
//
//--------------------------------------------------------------------

#ifndef STACK_H
#define STACK_H

#include <stdexcept>
#include <iostream>

using namespace std;

template <typename DataType>
class Stack {
  public:
    static const int MAX_STACK_SIZE = 8;

    virtual ~Stack();

    virtual void push(const DataType& newDataItem) throw (logic_error) = 0;
    virtual DataType pop() throw (logic_error) = 0;

    virtual void clear() = 0;

    virtual bool isEmpty() const = 0;
    virtual bool isFull() const = 0;

    virtual void showStructure() const = 0;
};

template <typename DataType>
Stack<DataType>::~Stack() 
// Not worth having a separate class implementation file for the destuctor
{}

#endif      // #ifndef STACK_H

So, I've included all corresponding headers since this program utilizes inheritance. Please continue to help me debug this program. All help is well-appreciated.

  • possible duplicate of [Where and why do I have to put the "template" and "typename" keywords?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Constructor Apr 16 '14 at 12:08
  • 1
    Please provide the header and mark the lines wth error in your source. –  Apr 16 '14 at 12:11
  • Add `using namespace std;` below the `#include` – A B Apr 16 '14 at 12:12
  • Can you show us the relevant extract of your `StackLinked.h` file ? – hivert Apr 16 '14 at 12:15
  • @AnkitB: I still would say **remove** `using namespace std;` from header. – Jarod42 Apr 16 '14 at 13:39
  • According to include guards, your implementation of the template method seems to be in a cpp file... see [why-can-templates-only-be-implemented-in-the-header-file](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Jarod42 Apr 16 '14 at 13:42
  • @hivert: I tried your method and it generated more errors. I should've post the earlier errors, which I will do now. It seems by eyes overlooked some that occurred before the ones I posted. – CPPHobbyist14 Apr 18 '14 at 00:27
  • @Jarod42: so removing "using namespace std;" from the StackLinked.h file, would fix it? – CPPHobbyist14 Apr 18 '14 at 00:27
  • @CPPHobbyist14: `using namespace` is a bad habit. I don't think it is the problem BTW. – Jarod42 Apr 18 '14 at 06:46

1 Answers1

1

You probably want to replace

StackLinked<DataType>::StackNode(...)

by

StackLinked<DataType>::StackNode::StackNode(...)

that is the constructor of class StackNode called as usual StackNode::StackNode inside the class StackLinked<DataType>.

hivert
  • 10,579
  • 3
  • 31
  • 56