Page 1 of 1
vectors in a class Trying to acces elements stored in a vec
#1
vectors in a class
Posted 03 January 2006 - 07:13 PM
Hello people,
How you all doin? Could someone look at the code and tell me why im getting an error please. I have commented on the line that is giving the error.
class Polynomial{
public:
Polynomial(){summands = new vector<coefftype>();}
Polynomial operator+(const Polynomial &p);
private:
vector<int> *summands;
};
Polynomial Polynomial::operator+(const Polynomial &p){
if(this->summands->size() == p.summands->size()){
for(int index = 0; index < this->summands->size(); index++)
this->summands[index] = this->summands[index] + p.summands[index]; //ProblemLine
}
return *this;
}
Thanks Yogi
How you all doin? Could someone look at the code and tell me why im getting an error please. I have commented on the line that is giving the error.
class Polynomial{
public:
Polynomial(){summands = new vector<coefftype>();}
Polynomial operator+(const Polynomial &p);
private:
vector<int> *summands;
};
Polynomial Polynomial::operator+(const Polynomial &p){
if(this->summands->size() == p.summands->size()){
for(int index = 0; index < this->summands->size(); index++)
this->summands[index] = this->summands[index] + p.summands[index]; //ProblemLine
}
return *this;
}
Thanks Yogi
#3
Re: vectors in a class
Posted 03 January 2006 - 07:41 PM
sorry
typedef int coefftype;
This is the error.
g++ energy3d_16.cc
energy3d_16.cc: In member function ‘Polynomial Polynomial::operator+(const Polynomial&)’:
energy3d_16.cc:535: error: no match for ‘operator+’ in ‘*(((Polynomial*)this)->Polynomial::summands + (+(((unsigned int)i) * 12u))) + *(((std::vector<coefftype, std::allocator<coefftype> >*)p->Polynomial::summands) + (+(((unsigned int)i) * 12u)))’
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_bvector.h:267: note: candidates are: std::_Bit_iterator std::operator+(ptrdiff_t, const std::_Bit_iterator&)
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_bvector.h:353: note: std::_Bit_const_iterator std::operator+(ptrdiff_t, const std::_Bit_const_iterator&)
and this is line 535
this->summands[index] = this->summands[index] + p.summands[index];
Is that cool, and thanks for the quick response.
typedef int coefftype;
This is the error.
g++ energy3d_16.cc
energy3d_16.cc: In member function ‘Polynomial Polynomial::operator+(const Polynomial&)’:
energy3d_16.cc:535: error: no match for ‘operator+’ in ‘*(((Polynomial*)this)->Polynomial::summands + (+(((unsigned int)i) * 12u))) + *(((std::vector<coefftype, std::allocator<coefftype> >*)p->Polynomial::summands) + (+(((unsigned int)i) * 12u)))’
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_bvector.h:267: note: candidates are: std::_Bit_iterator std::operator+(ptrdiff_t, const std::_Bit_iterator&)
/usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/bits/stl_bvector.h:353: note: std::_Bit_const_iterator std::operator+(ptrdiff_t, const std::_Bit_const_iterator&)
and this is line 535
this->summands[index] = this->summands[index] + p.summands[index];
Is that cool, and thanks for the quick response.
#4
Re: vectors in a class
Posted 03 January 2006 - 11:53 PM
vector<int> *summands;
your vector should not be a pointer, and instead, it should be a concrete type.
vector<int> summands;
You don't need new with this type.
It's a hassel trying to use a pointer to a vector type, because you confuse the compiler when you try to access index operator[].
If summands is a pointer, the compiler is going to think you're trying to access an array of vector<int>, instead of one single vector<int>
You should avoid using pointers as much as possible, because what they represent is more ambiguous then a reference type or a concrete type.
A pointer can be a pointer to one object, or it can be a pointer to an array of objects.
Where as a reference is only ever going to be pointing to one object.
I don't see a requirement in your code for your type to be a pointer, so I recommend making it a concrete type. That will fix the code, and make it safer, and easier to maintain.
Page 1 of 1

Ask A New Question
Reply




MultiQuote




|