注册 登录
编程论坛 C++教室

下面的代码红色报错,为什么?

fishviv 发布于 2010-12-25 18:31, 1510 次点击
#include<iostream>
#include"vector.h"
#include<iomanip>
#define N 4
using namespace std;
template<typename T>
struct Stack{
    Stack(){}
    ~Stack() {clear();}
    bool empty(void) const {return vector<T>::empty();}
    int  size (void) const {return vector<T>::size();}
    int  top(void)  {return vector<T>::back();}
    void pop(void)  {vector<T>::pop_back();}
    void push(int v){vector<T>::push_back(v);}
    void clear(void){vector<T>::clear();}
};
Stack*edge = new Stack[N];
//F:\Cpp\deepfind.cpp(17) : error C2955: 'Stack' : use of class template requires template argument list
        F:\Cpp\deepfind.cpp(16) : see declaration of 'Stack'


[ 本帖最后由 fishviv 于 2010-12-25 20:14 编辑 ]
13 回复
#2
fishviv2010-12-25 20:15
hhh
#3
qq10235692232010-12-25 22:24
不懂啊,顶一下。
#4
pangding2010-12-25 22:39
use of class template requires template argument list
看错误“用模版类要求提供模版参数”。

就是得写成 Stack<int> 之类的这个样子。
#5
fishviv2010-12-26 11:15
回复 4楼 pangding
这样改过,可是又有报错说,empty是属于vector...
#6
df198610172010-12-26 16:54
声明错误!stack<T> ****
#7
xin3109232010-12-27 01:24
#include<iostream>
#include<vector>
#include<iomanip>
#define N 4
using namespace std;
template<typename T>
struct Stack{
    Stack(){}
    ~Stack() {clear();}
    bool empty(void) const {return vector<T>::empty();}
    int  size (void) const {return vector<T>::size();}
    int  top(void)  {return vector<T>::back();}
    void pop(void)  {vector<T>::pop_back();}
    void push(int v){vector<T>::push_back(v);}
    void clear(void){vector<T>::clear();}
};
只能帮你到这一步   模版我还学过  不会
#8
xin3109232010-12-27 01:25
还没学过模版   悲剧
#9
pangding2010-12-27 20:38
回复 5楼 fishviv
你把完整的代码发上来看一下。
#10
megadeath2010-12-28 23:55
错误已经说明了,Stack 没有模板参数列表。

应该如下定义

Stack<int> * edge = new Stack<int>[N];

另外没看懂LZ的程序在干什么。
#11
missiyou2010-12-29 10:41
#include<iostream>
#include<vector>
#include<iomanip>
#define N 4
using namespace std;
template<typename T>
struct Stack{
    Stack(){}
    // ~Stack() {clear();}
    bool empty(void) const {return vector<T>::empty();}
    int  size (void) const {return vector<T>::size();}
    int  top(void)  {return vector<T>::back();}
    void pop(void)  {vector<T>::pop_back();}
    void push(int v){vector<T>::push_back(v);}
    // void clear(void){vector<T>::clear();}
};
Stack<int>*edge = new Stack<int>[N];

int main()
{
Stack<int> a;
a.push(11);
return 0;
}
这里写的本身就有问题!上面我改子之后是可以编译通过的, 但运行是有问题的!


改了点东西, 不知道是不是你要的那样!

#include<iostream>
#include<vector>
#include<iomanip>
#define N 4
using namespace std;
template<typename T>
    struct Stack
    {
    vector<T> v;
    public:
        Stack() {}
        ~Stack()
        {
            clear();
        }
        bool empty(void) const
        {
            return v.empty();
        }
        int  size (void) const
        {
            return v.size();
        }
        int  top(void)
        {
            return v.back();
        }
        void pop(void)
        {
            v.pop_back();
        }
        void push(T value)
        {
            v.push_back(value);
        }
        void clear(void)
        {
           v.clear();
        }
        
    };
   
int main(int argv, char*argc[])
{
    Stack<int> *edge = new Stack<int>[N];
    Stack<int> temp;
    temp.push(1);
    temp.push(2);
    cout << temp.top() << endl;
   
    return 0;
}

[ 本帖最后由 missiyou 于 2010-12-29 10:43 编辑 ]
#12
fishviv2010-12-31 21:25
回复 10楼 megadeath
我在遍历一个图,,,错误已经好了
#13
找工作中2011-01-02 13:24
lz的stack应该要private: std::vector<T>
还有就是lz要注意用好const,否则用你类的人会很惨的。
#14
DestinyLord2011-01-16 11:55
程序没给完吧??看不懂......
1