C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

vectors in a class Trying to acces elements stored in a vec Rate Topic: -----

#1 yogi  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 25-December 05


Dream Kudos: 0

Share |

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
Was This Post Helpful? 0
  • +
  • -


#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • Icon

Reputation: 197
  • View blog
  • Posts: 13,429
  • Joined: 12-July 02


Dream Kudos: 25

Re: vectors in a class

Posted 03 January 2006 - 07:30 PM

What error is it giving you? and where is the definition for coefftype?
Was This Post Helpful? 0
  • +
  • -

#3 yogi  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 25-December 05


Dream Kudos: 0

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.
Was This Post Helpful? 0
  • +
  • -

#4 Axter  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-April 05


Dream Kudos: 0

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.
Was This Post Helpful? 0
  • +
  • -

#5 yogi  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 25-December 05


Dream Kudos: 0

Re: vectors in a class

Posted 04 January 2006 - 01:50 PM

Thanks ok thats great.

I will use this method.

But when designing a deconstructor for this class i cant use

delete summands;

So wat could i do???

Yogi
Was This Post Helpful? 0
  • +
  • -

#6 Axter  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-April 05


Dream Kudos: 0

Re: vectors in a class

Posted 05 January 2006 - 10:07 PM

You don't do anything.

You only need to call delete on an object that was created via new.

If you use a concrete type you don't have to use new, and you don't have to use delete.
It will delete itself automatically.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users