关于 class
  • 板块学术版
  • 楼主jqQt0220
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/11/18 09:25
  • 上次更新2023/11/18 12:51:19
查看原帖
关于 class
678175
jqQt0220楼主2023/11/18 09:25

我尝试自己实现一个栈,代码差不多长这样:

namespace jqQt0220
{
    template<typename T> class stack
    {
    private:
        T *val;
        T *beg=val;
        int top=0;
        inline void refresh()
        {
            val=beg+top;
        }
    public:
        inline void push(T _x)
        {
            top++;
            refresh();
            *val=_x;
        }
        inline void pop()
        {
            top--;
            refresh();
        }
        inline T top()
        {
            return *val;
        }
        inline int size()
        {
            return top;
        }
        inline bool empty()
        {
            return top>0;
        }
        inline void clear()
        {
            top=0;
            refresh();
        }
    };
}

但是 CE 了,错误警告差不多长这样:

head/jqQt0220.h:1:0: error: unterminated #ifndef
 #ifndef JQQT0220
 ^
In file included from temp.cpp:1:0:
head/jqQt0220.h:9:30: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11        
         int val[1000000],len=0;
                              ^
head/jqQt0220.h:45:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11       
         T *beg=val;
                ^
head/jqQt0220.h:46:17: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11       
         int top=0;
                 ^
head/jqQt0220.h:66:9: error: 'T jqQt0220::stack<T>::top()' conflicts with a previous declaration
         }
         ^
head/jqQt0220.h:46:17: note: previous declaration 'int jqQt0220::stack<T>::top'
         int top=0;
                 ^
head/jqQt0220.h: In instantiation of 'class jqQt0220::stack<int>':
temp.cpp:17:22:   required from here
head/jqQt0220.h:46:17: error: 'T jqQt0220::stack<T>::top() [with T = int]' conflicts with a previous declaration
head/jqQt0220.h:46:17: note: previous declaration 'int jqQt0220::stack<int>::top'
head/jqQt0220.h: In instantiation of 'void jqQt0220::stack<T>::push(T) [with T = int]':
temp.cpp:31:21:   required from here
head/jqQt0220.h:54:16: error: no post-increment operator for type
             top++;
                ^
head/jqQt0220.h: In instantiation of 'bool jqQt0220::stack<T>::empty() [with T = int]':
temp.cpp:35:24:   required from here
head/jqQt0220.h:73:23: error: invalid use of member (did you forget the '&' ?)
             return top>0;
                       ^
head/jqQt0220.h: In instantiation of 'void jqQt0220::stack<T>::pop() [with T = int]':
temp.cpp:37:19:   required from here
head/jqQt0220.h:60:16: error: no post-decrement operator for type
             top--;
                ^
head/jqQt0220.h: In instantiation of 'int jqQt0220::stack<T>::size() [with T = int]':
temp.cpp:47:26:   required from here
head/jqQt0220.h:69:20: error: cannot convert 'jqQt0220::stack<T>::top<int>' from type 'int (jqQt0220::stack<int>::)()' to type 'int'
             return top;

(其中 head/jqQt0220.h 是我存这个栈的头文件)

我怀疑可能是我 class 用的有点问题,不知道哪里有问题,大佬求解(验证码 ymyy

2023/11/18 09:25
加载中...