0

以下のコンパイラ エラーが発生する原因を正確に把握しようとしています。Bloodshed Dev-C++ 4.9.9.2 を使用しています。

読み取りエラー: 'stackType::stackType(const stackType&)' のプロトタイプが、クラス 'stackType' のいずれにも一致しません。また、コピー コンストラクターの 1 つの定義の非テンプレート 'stackType::stackType(const stackType&)' のテンプレート定義も一致しません。

stackType<Type>::stackType(const stackType<Type>& otherStack) //definition of copy constructor
{
     list = NULL;
     copyStack(otherStack);
}

別のエラーは stackType::stackType(int) ですが、コードはコンストラクターの定義用であり、このエラーは次のとおりです。

template <class Type>
stackType<Type>::stackType(int stackSize) //definition of constructor
{
     if (stackSize <= 0)
     {
          cout << "Size of the array holding the stack must be positive." << endl;
          cout << "Creating an array of size 100." << endl;
          maxStackSize = 100;
     }
     else
          maxStackSize = stackSize; //sets stack size to value specified by parameter stackSize
          stackTop = 0; //sets stackTop to 0
          list = new Type[maxStackSize]; //creates array to hold elements of the stack
}

C:\Users\Owner\Desktop\SD254\SD254 Unit7B.cpp からファイルに含まれている状態を取得しているという私の最終的なエラーは、これ以上言いません。上記は明らかにメインファイルが私のコンピューターに保存されているディレクトリですが、なぜそれが問題なのですか?? 私の完全なコードは以下です。

ヘッダファイル:

#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>


using namespace std;

template <class Type>
class stackADT
{
      public:
             virtual void initializeStack() = 0; //initialize stack
             virtual bool isEmptyStack() const = 0; //determines whether stack is empty or not
             virtual bool isFullStack() const = 0; //determines whether stack is full or not
             virtual void push(const Type& newItem) = 0; //adds new item to the stack
             virtual Type top() const = 0; //returns the top element to the stack
             virtual void pop() = 0; //removes the top element of the stack
};
template <class Type>
class stackType: public stackADT<Type>
{
      public:
             const stackType<Type>& operator=(const stackType<Type>&); //overloads assignment operator
             void initializeStack(); //initializes stack to empty state
             bool isEmptyStack() const; //determines stack is empty
             bool isFullStack() const; //determines stack is not full
             void push(const Type& newItem); //adds new item to stack
             Type top() const; //return the top element of the stack
             void pop(); //removes top element of the stack
             stackType(int stackSize = 100); //constructor and creates array with default size of 100
             ~stackType(); //destructor and removes all elements from the stack
             void copyStack(const stackType<Type>& otherStack); //copies other stack
      private:
              int maxStackSize; //stores maximum stack size
              int stackTop; //point to the top of the stack
              Type *list; //pointer to the array that holds the stack elements

};
template <class Type>
void stackType<Type>::initializeStack() //definition of function intializeStack
{
     stackTop = 0;
}
template <class Type>
bool stackType<Type>::isEmptyStack() const //definition of function isEmptyStack
{
     return(stackTop == 0);
}
template <class Type>
bool stackType<Type>::isFullStack() const //definition of function isFullStack
{
      return (stackTop == maxStackSize);
}
template <class Type>
void stackType<Type>::push(const Type& newItem) //definition of push function
{
     if (!isFullStack())
     {
          list[stackTop] = newItem; //adds new item to top of stack
          stackTop++; //increments stackTop
     }
     else
          cout << "Cannot add to a full stack." << endl;
}
template <class Type>
Type stackType<Type>::top() const //definition of top-returns the top element to the stack
{
     assert(stackTop != 0); //if stack is empty terminate program
     return list[stackTop - 1]; //return element of the stack indicated by stackTop -1
}
template <class Type>
void stackType<Type>::pop() //definition of function pop
{
     if (!isEmptyStack())
          stackTop--; //decrement stackTop
     else
          cout << "Cannot remove from an empty stack." << endl;
}
template <class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack) //definition to make copy of stack
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     list = new Type[maxStackSize]; //copies otherStack into this one
     for (int j = 0; j < stackTop; j++)
          list[j] = otherStack.list[j];
}
template <class Type>
stackType<Type>::stackType(int stackSize) //definition of constructor
{
     if (stackSize <= 0)
     {
          cout << "Size of the array holding the stack must be positive." << endl;
          cout << "Creating an array of size 100." << endl;
          maxStackSize = 100;
     }
     else
          maxStackSize = stackSize; //sets stack size to value specified by parameter stackSize
          stackTop = 0; //sets stackTop to 0
          list = new Type[maxStackSize]; //creates array to hold elements of the stack
 }
template <class Type>
stackType<Type>::~stackType() //definition of destructor
{
     delete [] list; //deallocates the memory occupied by the array
}
template <class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack) //definition of copy constructor
{
     list = NULL;
     copyStack(otherStack);
}
template <class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) //overload assignment operator
{
      if (this != &otherStack) 
           copyStack(otherStack);
      return *this;
}
#endif

実際のプログラム ファイルは次のとおりです。

#include <iostream>
#include "myStack.h"

using namespace std;
void testCopyConstructor(stackType<int> otherStack);
int main()
{
    stackType<int> stack(25);
    stackType<int> copyStack(25);
    stackType<int> dummyStack(50);

    stack.initializeStack();
    stack.push(15);
    stack.push(10);
    stack.push(20);
    copyStack = stack; //copies stack into copyStack

    cout << "The elements of copyStack: " ;
    while (!copyStack.isEmptyStack()) //prints copyStack
    {
          cout << copyStack.top() << " ";
          copyStack.pop();
    }
    cout << endl;
    copyStack = stack;
    testCopyConstructor(stack); //tests the copy constructor
    if (!stack.isEmptyStack())
         cout << "The original stack is not empty." << endl; 
         cout << "The top element of the original stack: " << copyStack.top() << endl;
    dummyStack = stack; //copies stack into dummyStack
    cout << "The elements of dummyStack: ";
    while (!dummyStack.isEmptyStack()) //prints dummyStack
    {
          cout << dummyStack.top() << " " ;
          dummyStack.pop();
    }
    cout << endl;
    return 0;
 }
void testCopyConstructor(stackType<int> otherStack)
{
     if (!otherStack.isEmptyStack())
         cout << "otherStack is not empty." << endl; 
         cout << "The top element of otherStack: " << otherStack.top() << endl;
}

エラーを修正して正しくコンパイルおよびビルドできるようにする方法を誰かが教えてくれますか?

4

1 に答える 1

2

クラス定義にコピーコンストラクターの宣言を追加する必要があります。突然それを定義することはできません。

クラス定義は次のようになります。

template<typename Type>
class stackType: public stackADT<Type>
{
public:
    stackType(const stackType&);
    // ... all the other stuff
};

次に、以下の関数定義が認識されます。

于 2012-09-02T18:39:50.433 に答える