typedef std::vector<std::string> vstring;
typedef struct {
vstring inputs;
vstring branches;
vstring responses;
vstring repeats;
} data;
typedef struct {
vstring inputs2;
vstring branches2;
vstring responses2;
vstring repeats2;
} data2;
std::vector<data> mybase1;
std::vector<data2> mybase2;
std::vector<data2> mybase2(mybase1); //doesn't work, should copy?
//next attempt
int dbase_size = mybase1.size();
for(int index = 0; index < dbase_size; ++index) {
data2 temp = mybase1.at(index); //doesn't work
mybase2.push_back(temp); //doesn't work
}
//Reason: cannot convert from 'std::vector<_Ty>' to 'const //std::allocator<_Ty>'
copy vector into vector [Resolved]
This is just a code snippet.
I'm trying to copy one vector into another.
Reason, the first vector is defined in a class and goes out of scope and gets destroyed so I want to copy it into a global vector before it is destructed.
This is just two of my many attempts in the last few hours just to demonstrate that I have been trying to solve it myself. I think it's an issue with the first vector being in a class definition which makes it constant? Is that right?
I can post all the referenced code if this isn't enough. Basically the only difference is that the first "struct" (data1) and first vector (mybase1) are declared in a class while the second is global at the top of the *.cpp file.
m0ng00se
[Edited by - m0ng00se on March 5, 2008 9:45:36 PM]
1) std::vector::assign, one of the functions in <algorithm> or the constructor taking iterators will work.
2) Your bigger problem is that one vector is a vector<T>and the other a vector<U>. These are not related types. Why do you have two of essentially the same type? One of them is not required.
2) Your bigger problem is that one vector is a vector<T>and the other a vector<U>. These are not related types. Why do you have two of essentially the same type? One of them is not required.
Why do you have two different structs that are in effect, exactly the same (data and data2)?
This is the root of your problem...
If both vectors were using the same type of struct you could simply do:
This is the root of your problem...
If both vectors were using the same type of struct you could simply do:
std::vector<data> database2(database);// or std::vector<data> database2;database2 = database;// or even better:std::vector<data> database2;database2.swap( database );//this is a super-fast copy, as it just swaps the contents of the two vectors by exchanging root-pointers.
While structurally identical, data and data2 are not the same type type. Thus vector<data> and vector<data2> are not the same type and cannot be copied. Provide a sane overloaded set of operators for data <-> data2 and something akin to the second method should work (only using assignment rather than push_back).
...or just use one type.
...or just use one type.
whoa.... that is the problem alright...
but I thought they were identical? What am I missing?
Sorry but I thought they were the same. Can somebody please explain?
I just want to grab all the data in the first one before it goes out of scope and gets destructed, because I need to use it later elsewhere.
Does the fact that it's declared inside a class make it a different type? Sorry I'm a C programmer just learning C++ and I still get confused by classes.
Or is it something else entirely that I'm missing? I thought I'd created an identical vector to copy into. I don't need two different types. Do you mean data is a different type to data2 because they're two different user defined types? Should I have just used data in both?
Sorry guys, I'd really like to understand what I'm doing wrong so I don't do it again.
m0ng00se
but I thought they were identical? What am I missing?
Sorry but I thought they were the same. Can somebody please explain?
I just want to grab all the data in the first one before it goes out of scope and gets destructed, because I need to use it later elsewhere.
Does the fact that it's declared inside a class make it a different type? Sorry I'm a C programmer just learning C++ and I still get confused by classes.
Or is it something else entirely that I'm missing? I thought I'd created an identical vector to copy into. I don't need two different types. Do you mean data is a different type to data2 because they're two different user defined types? Should I have just used data in both?
Sorry guys, I'd really like to understand what I'm doing wrong so I don't do it again.
m0ng00se
Quote:Does the fact that it's declared inside a class make it a different type?
No, the place where the variable is declared has no effect on it's type.
Quote:I thought I'd created an identical vector to copy into.
No, one is a vector of 'data's and one is a vector of 'data2's.
They look the same to a human, but all C++'s type system sees is that one is called 'data' and the other is called 'data2', hence, they're different types.
Quote:Do you mean data is a different type to data2 because they're two different user defined types? Should I have just used data in both?
Yes.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement