C: var 未在此范围内声明

C: var was not declared in this scope

我正在尝试编译我的数组包装器 class,但我是 c++ 的新手。我不断收到与最后一个功能相关的一系列信息:

第 81 行 在没有参数列表

的情况下无效使用模板名称 'warray'

第 81 行 ISO C++ 禁止声明 'parameter' 而不是类型

第 81 行 在 < town

之前预期出现错误“,”或“...”

第 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

这一行:

       T *theData [theSize];

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

数组的指针

改为

       T *theData;

您的代码还有其他问题。例如你的 << 运算符需要是一个模板,我不知道 (*this) 想要达到什么目的。

注意:我假设您这样做是出于学习目的,不能简单地使用向量。

编辑:"Invalid Use of template-name 'warray' without an argument list" 错误是由 << 运算符前面没有 template<typename T> 引起的。它需要这个来使它成为一个模板函数。

不能这样定义数据:

T *theData[theSize];

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

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

T *theData;

你应该还好吧。

您已将其标记为 C++。

我建议您更改为:

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

也许试试:

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

您所说的 "theSize" 现在可用 theData.size()。

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

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

删除

delete [] theData;

因为您不再在 ctor 中“新建”它。

当你的 warray 实例被 dtor 时,向量的 dtor 将被自动调用。