c++ - C: var was not declared in this scope -
i attempting array wrapper class compile, i'm new c++. keep getting series of relating last function:
line 81 invalid use of template-name 'warray' without arugment list
line 81 iso c++ forbids declaration of 'parameter' not type
line 81 error expected ',' or '...' before < town
line 83 rhs not declared in scope
and finally, line 86 rhs not declared in scope
this function confusing, , think implemented 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 size of 10 - bump 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 < rhs//this remedies if(thesize < rhs.thesize){ delete [] thedata; thedata = new t[rhs.thesize]; } thesize = rhs.thesize; for(unsigned int = 0; < thesize; ++i){ (*this); } return *this; } //destrctor ~warray(){ delete [] thedata; } //operator+ concatenate 2 arrays should const warray operator+(const warray &rhs) const{ warray toret(thesize + rhs.size); for(unsigned int = 0; < thesize; ++i){ toret[i] = (*this)[i]; } for(unsigned int = 0; < thesize; ++i){ toret[i+thesize] = rhs[i]; } return warray(); } //operator[unsigned t index] //will index , allow access requested element // - 2 versions, const , 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 = 0; < rhs.size()-1; ++i){ os << rhs[i] << " , "; } os << rhs[rhs.size() - 1] << " ]"; return os; } #endif
you have marked c++.
i recommend change from:
class warray { private: unsigned int thesize; t* thedata;
and perhaps try:
class warray { private: std::vector<t> thedata;
what call "thesize" available thedata.size().
to append values of t, use push_back().
if desire, can allocate start size using thedata.reserve(size), not necessary.
remove
delete [] thedata;
because no longer 'new'd in ctor.
the vector's dtor called automagically when warray instance dtor'd.
Comments
Post a Comment