【问题标题】:C: var was not declared in this scopeC: var 未在此范围内声明
【发布时间】:2015-05-12 20:22:02
【问题描述】:

我正在尝试编译我的数组包装类,但我是 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

【问题讨论】:

  • 是的,“T *theData [theSize];”是错的。您已将此标记为 C++ ...我建议您将两个数据属性替换为单个向量。
  • 正如其他人回答的那样,您的问题是您如何声明theData。还有一些其他注意事项:您的 &lt;&lt; 运算符应声明为 friend 并在类范围内移动;这条线还有什么作用:for(unsigned int i = 0; i &lt; theSize; ++i) { (*this); } ..??? .. 最后,不确定这是否是一项练习,但使用std::vector 或其他序列容器之一(然后可以使用其他STL 序列功能,如std::fill 和/或@ 987654328@)??
  • 请不要编辑您的问题,以使之前给出的所有答案无效。这会让想要回答您的新问题的未来访问者感到非常困惑。
  • @TheDark 很抱歉,我已针对问题进行了调整,不会更改
  • @TheDark 可以再看看吗?

标签: c++ scope


【解决方案1】:

这一行:

       T *theData [theSize];

尝试声明大小为theSize 的指针数组,但theSize 不是常量,在编译时未知。您也不需要指向T 的指针数组,而是需要指向T 数组的指针。

改成

       T *theData;

您的代码还有其他问题。例如您的 &lt;&lt; 运算符需要是一个模板,我不知道 (*this) 想要实现什么。

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

编辑:“在没有参数列表的情况下无效使用模板名称'warray'”错误是由&lt;&lt; 运算符前面没有template&lt;typename T&gt; 引起的。它需要这个才能使其成为模板函数。

【讨论】:

  • 添加,&lt;&lt; 运算符可以声明为friend 并在类范围内移动
  • @txtechhelp 它可以,但没有意义,因为它只需要公共接口。
【解决方案2】:

您已将此标记为 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'd 时,向量的 dtor 将被自动调用。

【讨论】:

    【解决方案3】:

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

    T *theData[theSize];
    

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

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

    T *theData;
    

    你应该没事吧。

    【讨论】:

      猜你喜欢
      • 2010-12-17
      • 1970-01-01
      • 2021-07-17
      • 2015-08-21
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多