473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

operator + on vector


<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

Jul 19 '05 #1
4 1701
Hi,

"Bob Smith" <bobsmith@[dont-even-think-about-it]jippii.fi> wrote in message
news:MU***********@reader1.news.jippii.net...

<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.





Jul 19 '05 #2

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

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.






Jul 19 '05 #3

"Bob Smith" <bobsmith@[dont-even-think-about-it]jippii.fi> wrote in message
news:YK*************@reader1.news.jippii.net...

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
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,

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.





Jul 19 '05 #4
Here is some info about when the compiler can't determine if it refers to a
type or not (and thus you have to supply the typename keyword).
http://www.comeaucomputing.com/techt...ates/#typename
Regards, Ron AF Greve

"Bob Smith" <bobsmith@[dont-even-think-about-it]jippii.fi> wrote in message
news:YK*************@reader1.news.jippii.net...

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

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.





Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
21
by: Makhno | last post by:
Hello, Why does my cast from Vector<class Float> to Vector<float> not work? It won't compile, template<class Float> class Vector { public: Vector(Float x1,Float y1,Float...
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
20
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
0
by: santana | last post by:
Hi I've created a class to be used with stl vector. I'm having a hard time decipher the error message outpout by g++... burlen@quaoar:~/hdf52vtk_dev/src$ g++ junk.cpp GridPoint.o...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.