0

次のコードを見てください。

Stack.h

template <typename T>

class Stack
{
public:
    Stack(int number)
    {
        maxSize = number;
        top = -1;
        stackData = new T*[maxSize];
    }

    ~Stack()
    {
        delete [] stackData;
    }

    int count()
    {

    }

    bool isEmpty()
    {
        if(top==-1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool isFull()
    {
        if(top== (maxSize-1))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    *T pop()
    {
        if(!isEmpty())
        {
            return stackData[top--]; // Remove Item From Stack
        }
    }

    *T peek()
    {
        T *peekData =  &stackData[top];
        return peekData;
    }

    void push(T *pushValue)
    {
        if(!isFull())
        {
            stackData[++top] = pushValue;
        }
    }

private:
    int maxSize;
    T ** stackData;
    int top;
};

メイン.cpp

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

using namespace std;

int main()
{
    int i = 0;

    Stack<double> doubleStack(5);
    double doubleValue = 1.1;

    cout << "pushing elements into the stack" << endl;

    while(i<5)
    {
        doubleStack.push();
    }

    system("pause");
    return 0;
}

このコードを実行すると、次のエラーが発生します。

1>------ Build started: Project: CourseWork2, Configuration: Debug Win32 ------
1>  Main.cpp
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1>          c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(74) : see reference to class template instantiation 'Stack<T>' being compiled
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(54): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(60): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C2146: syntax error : missing ';' before identifier 'pop'
1>          c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(11) : see reference to class template instantiation 'Stack<T>' being compiled
1>          with
1>          [
1>              T=double
1>          ]
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(48): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(49): warning C4183: 'pop': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C2146: syntax error : missing ';' before identifier 'peek'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(56): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\stack.h(57): warning C4183: 'peek': missing return type; assumed to be a member function returning 'int'
1>c:\users\yohan\documents\visual studio 2010\projects\coursework2\coursework2\main.cpp(18): error C2660: 'Stack<T>::push' : function does not take 0 arguments
1>          with
1>          [
1>              T=double
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Intellelisense は、isFull()、count()、および isEmpty() 以外のメソッドを識別していません。このため、残りをコーディングできません。

どうしてこれなの?助けてください!

4

2 に答える 2

3

アスタリスクを間違った場所に置きます。そのはず:

T *pop() {
    //implementation
}

T *peek() {
    //implementation
}
于 2013-02-08T05:55:45.617 に答える
3

*関数構文の間違った場所に置きます:

アップデート:

 *T pop()
 *T peek()

に:

 T* pop()
 T* peek()
于 2013-02-08T05:55:52.607 に答える