Copying a vector that is inside a struct

Was wondering if anyone could look at me code stub and see what im doing wrong. Im trying to use a generic structure that holds a vector and im trying to pass it to another class and do a copy on it but the g++ compiler is giving me an error. Im not sure if the copy im tring to make from the showIntData() method at the bottem is legal or not. Any help would be appreciated. Here is the cpp code.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef vector<int> IntData;

struct Wrapper
{
IntData const* IntVector;
};

class Sender
{
public:
void SendVector();
};

class Reciever
{
public:
void displayVector( IntData const &IntData );
void showIntData( Wrapper* Payload );
};

void Reciever::displayVector( IntData const &IntData )
{
IntData::const_iterator itr;

for( itr = IntData.begin(); itr != IntData.end(); itr++ )
{
cout << "payload data -> " << *itr << endl;
}
}

void Reciever::showIntData( Wrapper* Payload )
{
IntData::const_iterator itr;

// this works fine and displays the data in the vector
for( itr = Payload->IntVector->begin(); itr != Payload->IntVector->end(); itr++ )
{
cout << "looking for payload data in itr -> " << *itr << endl;
}

/* now i want to create a new copy of the vector and pass it to the displayVector() method */

/* This code below gives me an error at compile time
main.cpp:47: error: no match for operator = in VectorCopy =
Payload->Wrapper::IntVector

IntData VectorCopy;
VectorCopy = Payload->IntVector;
this->displayVector(VectorCopy);

*/
}
void Sender::SendVector()
{
cout << "SendVector() sending vector " << endl;

IntData SomeIntData;
SomeIntData.push_back( 1 );
SomeIntData.push_back( 2 );

Wrapper* Payload = new Wrapper;
Payload->IntVector = &SomeIntData;

Reciever* VectorReciever = new Reciever;
VectorReciever->showIntData( Payload );
delete VectorReciever;
}

int main()
{
Sender VectorSender;
VectorSender.SendVector();
}

Please learn to use code tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void Reciever::showIntData( Wrapper* Payload )
{
   IntData::const_iterator itr;

   // this works fine and displays the data in the vector
   for( itr = Payload->IntVector->begin(); itr != Payload->IntVector->end();   itr++ )
   {
   cout << "looking for payload data in itr -> " << *itr << endl;
   }

   /* now i want to create a new copy of the vector and pass it to the displayVector() method */

   /* This code below gives me an error at compile time
   main.cpp:47: error: no match for operator = in VectorCopy =
   Payload->Wrapper::IntVector
   // VectorCopy IS A VECTOR OBJECT, not a pointer.
   IntData VectorCopy;
   
   // YOU CANNOT ASSIGN this way.  IntVector is a pointer to an object.
   VectorCopy = Payload->IntVector;

   // TRY IT THIS WAY
   VectorCopy = *(Payload->IntVector);
   this->displayVector(VectorCopy);

   */
}


To be honest with you, I am unclear on why you are trying to make a copy. What is the point? You can pass the vector of the payload directly to the displayVector function.
this->displayVector(*(Payload->IntVector));
Last edited on
Thanks for the reply i will use the code tags from now on. I was just trying different things on the vector copy there so i see what you mean now. Thanks for the help.
1
2
3
4
5
6
7
8
void Reciever::displayVector( IntData const &IntData )
{
  IntData::const_iterator itr;
  for( itr = IntData.begin(); itr != IntData.end(); itr++ )
  {
    cout << "payload data -> " << *itr << endl;
  }
}

Noticed you are using the type as the name of your argument.
even if the compiler isn't giving you an error on this(which I imagine it would), it is probably something to be avoided to prevent confusion.
Opps thats correct im changing it now, thanks.
Topic archived. No new replies allowed.