operator + on vector

B

Bob Smith

<snip>
class QxEvent{
....
};
typedef std::vector<QxEvent> QxEventVector;

QxEventVector QxEventPages::get_data(){
return ( m_event_visualizer_vector[0]->get_data() +
m_event_visualizer_vector[1]->get_data() );
}

get_data() returns a QxEventVector.

</snip>

The above member get_data() fails to compile, complaining that
QxEventVector has no operator+.

Could any please advice as how to implement the operator+ the easiest way.

Many thank's in advance.
/B
 
M

Moonlit

Hi,

Bob Smith said:
<snip>
class QxEvent{
...
};
typedef std::vector<QxEvent> QxEventVector;

QxEventVector QxEventPages::get_data(){
return ( m_event_visualizer_vector[0]->get_data() +
m_event_visualizer_vector[1]->get_data() );
}

get_data() returns a QxEventVector.

</snip>

The above member get_data() fails to compile, complaining that
QxEventVector has no operator+.

Could any please advice as how to implement the operator+ the easiest way.

Many thank's in advance.
/B

For instance the following code would do that:

--------------------------------------Snip--------------------
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;

template<typename T> class CMyVector : public vector<T>
{
public:

CMyVector<T> operator+( const CMyVector<T>& Obj )
{
CMyVector<T> Result =*this;
typename CMyVector<T>::const_iterator WalkerObj =
Obj.begin();

for( typename CMyVector<T>::iterator Walker =
Result.begin(); Walker != Result.end(); ++Walker,
++WalkerObj )
{
*Walker += *WalkerObj;
}

return Result;


};
};

int main()
{
CMyVector<int> Vec1, Vec2, Vec3;

Vec1.push_back( 1 );
Vec1.push_back( 2 );
Vec1.push_back( 3 );

Vec2.push_back( 6 );
Vec2.push_back( 9 );
Vec2.push_back( 13 );

Vec3 = Vec1 + Vec2;

copy( Vec3.begin(), Vec3.end(), ostream_iterator<int>(cout, "\n"));


return 0;
}
// TODO: more error checking!!

-------------------------------------------------------------------------

Make sure to check that the size of the vectors are the same:



Regards, Ron AF Greve.
 
B

Bob Smith

hey moonlit,
thank's that was quick
just one question,

For instance the following code would do that:

--------------------------------------Snip--------------------
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
using namespace std;

template<typename T> class CMyVector : public vector<T>
{
public:

CMyVector<T> operator+( const CMyVector<T>& Obj )
{
CMyVector<T> Result =*this;
typename CMyVector<T>::const_iterator WalkerObj =


why the use of typename in the above line?
I mean what does typename do there, what does it mean?
TIA
/B
 
M

Moonlit

Bob Smith said:
hey moonlit,
thank's that was quick
just one question,

=


why the use of typename in the above line?
I mean what does typename do there, what does it mean?
TIA
/B

It tells the compiler that name refers to a type. Actually in this case the
compiler could figure it out itself (when I first compiled it I forgot it
and the compiler told me I should add typename to that line :) ).


Regards, Ron AF Greve,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top