wang_yue_xi
2019-10-09 20:19
采纳率: 100%
浏览 566

template报错不明白?

c++萌新 template报错

#include<bits/stdc++.h>
using namespace std;
template<size_t SIZE>
class HP{
    private:
        size_t Len;
        bool ifPlus;
        short hp[SIZE];
    public:
        void clear(){
            int i;
            ifPlus = true;
            Len = 0;
            for(i=0; i<Len; ++i) hp[i] = 0;
        }
        HP operator=(HP<size_t aSize> a){
            int i;
            clear();
            ifPlus = a.ifPlus;
            Len = a.Len;
            if(SIZE < a.Len){
                printf("Out of memory!");
                exit(0);
            }
            for(i=0; i<a.Len; ++i) hp[i] = a.hp[i];
            return temp;
        }

信息:

[Error] template argument 1 is invalid

[Error] template argument 1 is invalid (他说了两遍)

[Error] 'HP' is not a template

[Error] expected primary-expression before 'aSize'

[Error] expected '>' before 'aSize'

谢谢大佬

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2019-10-09 22:53
    已采纳
    #include <iostream>
    using namespace std;
    
    #include<bits/stdc++.h>
    using namespace std;
    template<size_t SIZE>
    class HP{
        private:
            size_t Len;
            bool ifPlus;
            short hp[SIZE];
        public:
            void clear(){
                int i;
                ifPlus = true;
                Len = 0;
                for(i=0; i<Len; ++i) hp[i] = 0;
            }
            HP operator=(HP a){
                int i;
                clear();
                ifPlus = a.ifPlus;
                Len = a.Len;
                if(SIZE < a.Len){
                    printf("Out of memory!");
                    exit(0);
                }
                for(i=0; i<a.Len; ++i) hp[i] = a.hp[i];
                HP temp;
                return temp;
            }
    
    };
    
    int main() {
        // your code goes here
        return 0;
    }
    
    打赏 评论

相关推荐 更多相似问题