C:var未在此范围内声明(C: var was not declared in this scope)

编程入门 行业动态 更新时间:2024-10-19 20:41:54
C:var未在此范围内声明(C: var was not declared in this scope)

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

第81行无效使用模板名称'warray'而没有调整列表

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

在<town之前预期','或'...'的第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

I am attempting to get my array wrapper class to compile, but I'm new to c++. I keep getting a series of relating to the last function:

Line 81 Invalid Use of template-name 'warray' without an arugment list

Line 81 ISO C++ forbids declaration of 'parameter' with not type

line 81 error expected ',' or '...' before < town

line 83 rhs was not declared in this scope

and finally, line 86 rhs was not declared in this scope

This function is so confusing, and I think I implemented it all correct.

IDK! Please help!

#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

最满意答案

您已将其标记为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将被自动调用。

You have marked this as C++.

I recommend you change from:

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

And perhaps try:

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

What you call "theSize" is now available as theData.size().

To append values of T, use push_back().

If you desire, you can allocate a start size using theData.reserve(size), but not necessary.

Remove

delete [] theData;

because you no longer 'new'd it in the ctor.

The vector's dtor will be called automagically when your warray instance is dtor'd.

更多推荐

本文发布于:2023-07-25 05:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1256778.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在此   范围内   声明   var   declared

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!