我试图让我的数组包装类进行编译,但我是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; } #endifI 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.
更多推荐
发布评论