C:var未在此范围内声明

时间:2015-05-12 00:54:25

标签: c++ scope

我试图让我的数组包装类进行编译,但我是c ++的新手。我不断得到一系列与最后一个函数相关的内容:

第81行 无效使用模板名称' warray'没有行政名单

第81行 ISO C ++禁止声明'参数'没有打字

第81行 错误预期','或者' ...'之前<镇

第83行 rhs未在此范围内声明

最后,第86行 rhs未在此范围内声明

这个功能太混乱了,我想我实现的一切都是正确的。

IDK!请帮忙!

#ifndef WARRAY

#define WARRAY
    #include <iostream>
    #include <stdexcept>

    template <typename T>

    class warray {
        private:
            unsigned int theSize;
            T* theData;
        public:
            //will default to a size of 10 - bump to 10 if below
            warray(unsigned int size = 10){
                if(size < 10){
                    size = 10;
                }

                theSize = size;
                theData = new T[theSize];
            }

            //copy
            warray(const warray &rhs):theSize(rhs.theSize){
                theData = new T[theSize];
                //use assignment*this = rhs;
                *this = rhs;
            }

            //assignment
            warray & operator=(const warray &rhs){
                //only resize array if lhs < than rhs//this also remedies
                if(theSize < rhs.theSize){
                    delete [] theData;
                    theData = new T[rhs.theSize];
                }
                theSize = rhs.theSize;
                for(unsigned int i = 0; i < theSize; ++i){
                    (*this);
                }
                return *this;
            }

            //destrctor
            ~warray(){
                delete [] theData;
            }

            //operator+ will concatenate two arrays should be const
            warray operator+(const warray &rhs) const{
                warray toRet(theSize + rhs.size);
                for(unsigned int i = 0; i < theSize; ++i){
                    toRet[i] = (*this)[i];
                }
                for(unsigned int i = 0; i < theSize; ++i){
                    toRet[i+theSize] = rhs[i];
                }
                return warray();
            }

            //operator[unsigned T index]
            //will index and allow access to requested element
            // - two versions, const and non-const
            T operator[](unsigned int index) const{
                if(index >= theSize){
                    throw std::out_of_range ("in operator [] ");
                }
                return theData[theSize];
            }

            //size
            unsigned int size() const{
                return theSize;
            }
    };

     std::ostream &operator<< (std::ostream &os, const warray&<T> rhs){
        os << "[ ";
        for(unsigned i = 0; i < rhs.size()-1; ++i){
            os << rhs[i] << " , ";
        }
        os << rhs[rhs.size() - 1] << " ]";
        return os;
    }

#endif

3 个答案:

答案 0 :(得分:2)

这一行:

       T *theData [theSize];

尝试声明大小为theSize的指针数组,但theSize不是常量,在编译时不知道。你也不想要一个指向T的指针数组,你想要一个指向T数组的指针。

将其更改为

       T *theData;

您的代码还有其他问题。例如您的<<运算符需要是一个模板,我不知道(*this)正在尝试实现的目标。

注意:我假设你是为了学习而做的,不能简单地使用矢量。

编辑:{无效使用模板名称'warray'没有参数列表“错误是由<<运算符在其前面没有template<typename T>引起的。它需要这个使它成为一个模板化的功能。

答案 1 :(得分:1)

无法以这种方式定义数据:

T *theData[theSize];

使用变量定义静态数组的大小是非标准的。有些编译器允许,有些则不允许。最佳做法是不要这样做,这样你就不会被绊倒。实际上,大小在那时没有定义的值,所以即使你的编译器做了那个技巧,也有大量的ka-boom潜力。

幸运的是,根据其余代码,您不需要。你自己定义数组的大小,所以:

T *theData;

你应该没问题。

答案 2 :(得分:1)

您已将此标记为C ++。

我建议你改变:

class warray {
    private:
        unsigned int theSize;
        T* theData;

也许可以尝试:

class warray {
    private:
        std::vector<T> theData;

您所谓的“theSize”现在可用作theData.size()。

要附加T的值,请使用push_back()。

如果需要,可以使用Data.reserve(大小)分配起始大小,但不是必需的。

删除

delete [] theData;

因为你不再在ctor中新手了。

当你的warray实例是dtor'd时,vector的dtor将被自动调用。