开发者

error: expected constructor, destructor, or type conversion before ‘<’ token

I'm fairly new to C++ but already gained some experience in java.

In following short c++-exercise I tried to build a stack by using a class template. Unfortunately it fails to compile and I can't figure out why.

The error message is:

Stack.cpp:6: error: expected constructor, destructor, or type conversion before ‘<’ token

Stack.cpp:14: error: expected initializer before ‘<’ token

Stack.cpp:25: error: expected initializer before ‘<’ token

make[2]: * [build/Debug/GNU-Linux-x86/Stack.o] Error 1

Here's the Stack.h:

template <class T>
class Stack {
public:
    Stack(int = 10);

    ~Stack() {
        delete [] stackPtr;
    }

    int isEmpty()const {
        return top == -1;
    }

    int isFull() const {
        return top == size - 1;
    }

    int push(const T&);
    int pop(T&);

private:
    int size; // length i.e. number of elements on Stack.
    int top; //index of top element
    T* stackPtr;
};

Stack.cpp:

template <class T>
Stack<T>::Stack(int s) {
    size = s > 0 && s < 1000 ? s : 15;
    top = -1; // initialize stack
    stackPtr = new T[size];
}

template <class T>
int Stack<T>::push(const T& item) {
    if (!isFull()) {
        stackPtr[++top] = item;
        return 1; // push successful
    }
    return 0; // push unsuccessful
}

template <class T>
int Stack<T>::pop(T& popValue) {
    if (!isEmpty()) {
        popValue = stackPtr[top--];
        return 1; // pop successful
    }
    return 0; // pop unsuccessful
}

The main.cpp looks like that:

#include <iostream>
#include "Stack.h"
using namespace std;

int main(int argc, char** argv) {
    Stack<int> intStack;
    int i = 1.1;
    cout << "Pushing:" &开发者_如何转开发lt;< endl;
    while (intStack.push(i)) {
        cout << i << ' ';
        i += 1;
    }
    cout << endl << "Stack Full" << endl
         << endl << "Popping elements from is" << endl;
    while (intStack.pop(i))
        cout << i << ' ';
    cout << endl << "Stack Empty" << endl;
}

What's going wrong here?


I copy-pasted your code as-is in Visual Studio 2010 and compiled and linked without any problems, so long the content from both header and cpp are in one place (i.e., pasted both in main.cpp). Likewise, GCC doesn't complain either.

After splitting it up in a header and a cpp, it still compiled just fine, but now there were linker errors, the reason of them being explained in the links I gave in my comment (this and this, this C++ FAQ links might also be interesting). Syntactically, there is nothing wrong with your code as far as I and my compiler can see.

The only thing I could imagine, seeing that you first linked to pastebin where you head the complete files pasted (which I edited in the question then), was that you never included Stack.h in Stack.cpp (I think this might be the reason, as your Stack.h paste even had the include guards). Still, the fundamental problem remains.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜