2d vector error

L

Lorn

Hopefully someone can help me out with this problem I'm having with 2d
vectors. My vector initialization looks like this:

struct Object {
double d1;
double d2;
int i1;
};

std::vector<std::vector <Object> > vMain;
std::vector<Object> vec;

/////
When I add objects in the vectors it looks like this:

Object stData2 = { 2, 2, 2};
Object stData3 = { 3, 3, 3};
Object stData4 = { 4, 4, 4};

vec.push_back(stData2);
vMain.push_back(vec);
vec.clear();
vec.push_back(stData3);
vMain.push_back(vec);
vec.clear();

/////
Ok, so far so good. My problem comes when I want to add a third vector
object into my vMain vector at a specified position. The following
gives me an error:

vec.push_back(stData4);
vMain[0].push_back(vec);

/////
The error I receive is:

error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1
from 'std::vector<_Ty>' to 'const CTestDlg::Object &'
with
[
_Ty=CTestDlg::Object
]
and
[
_Ty=CTestDlg::Object
]
Reason: cannot convert from 'std::vector<_Ty>' to 'const
CTestAPIDlg::Object'
with
[
_Ty=CTestDlg::Object
]
No constructor could take the source type, or constructor
overload resolution was ambiguous.

/////

I have seen examples where the code above compiles and seemingly works.
What am I doing wrong? Any tips or suggestions would be very valuable
and much appreciated.

Thank you,
Lorn
 
M

Marco Wahl

Lorn said:
Hopefully someone can help me out with this problem I'm having with 2d
vectors.

vec.push_back(stData2);
vMain.push_back(vec);
vec.clear();
vec.push_back(stData3);
vMain.push_back(vec);
vec.clear();

/////
Ok, so far so good. My problem comes when I want to add a third vector
object into my vMain vector at a specified position. The following
gives me an error:

vec.push_back(stData4);
vMain[0].push_back(vec);

Hi Lorn,

I just reduced/modified your code slightly. I suppose it becomes
cristal-clear
what the problem was.

#include <vector>

struct Object {
double d1;
double d2;
int i1;
};

int main(int argc, char* argv[])
{
std::vector<std::vector <Object> > vMain;
std::vector<Object> vec;
Object stData2 = { 2, 2, 2}; // This works? I'm surprised!
Object stData3 = { 3, 3, 3}; // But that doesn't matter for now.
Object stData4 = { 4, 4, 4};

vMain.push_back(vec);
vMain.push_back(vec);
vMain[0].push_back(vec); // Why this [0] here?

return 0;
}


Best wishes
 
R

Ron Natalie

Lorn said:
Hopefully someone can help me out with this problem I'm having with 2d
vectors. My vector initialization looks like this:

struct Object {
double d1;
double d2;
int i1;
};

std::vector<std::vector <Object> > vMain;
std::vector<Object> vec;

/////
When I add objects in the vectors it looks like this:

Object stData2 = { 2, 2, 2};
Object stData3 = { 3, 3, 3};
Object stData4 = { 4, 4, 4};

vec.push_back(stData2);
vMain.push_back(vec);
vec.clear();
vec.push_back(stData3);
vMain.push_back(vec);
vec.clear();

/////
Ok, so far so good. My problem comes when I want to add a third vector
object into my vMain vector at a specified position. The following
gives me an error:

vec.push_back(stData4);
vMain[0].push_back(vec);
vMain[0] is type vector<Object>. Pushing back requires an argument of
type Object.

Why did you put vMain[0] here when you correctly used just vMain before?
 
L

Lorn

vMain[0].push_back(vec); // Why this [0] here?

The reason for the zero is to specify the position in the vMain vector
where I would like to put the "vec" vector containing Object stData4.
Before the line you quoted there was 1 vector object in vMain[0] and 1
vector object in vMain[1]. It is basically a table. What I was trying
to do was add a second vector object at position vMain[0]. I have tried
multiple ways of doing this, but all give me the same error. For
example:

vMain.at(0).push_back(vec);

or

vMain.insert(vMain.begin(), vec.begin(), vec.end()) // not sure about
this one, but I think it should be possible with correct formatting

Does anyone know if what I'm trying to do is a limitation of STL? Can a
vector<Object be added as I'm trying to do, or am I somehow having
problems with my compiler. Or, maybe just my code is wrong :).

Thanks
 
L

Lorn

Ahhhhh... ok, I get it. The correct notation here would be:

vMain[0].push_back(stData4);

Thanks guys :)

Lorn
 
R

Ron Natalie

Lorn said:
vMain[0].push_back(vec); // Why this [0] here?

The reason for the zero is to specify the position in the vMain vector
where I would like to put the "vec" vector containing Object stData4.
Before the line you quoted there was 1 vector object in vMain[0] and 1
vector object in vMain[1]. It is basically a table. What I was trying
to do was add a second vector object at position vMain[0]. I have tried
multiple ways of doing this, but all give me the same error. For
example:

push_back puts back at the end of the vector ALWSAYS. at(0)
or [0] returns an element of the vector.

If you want to insert, use insert:

vmain.insert(vmain.begin(), vec);

or after the first position:
vmain.insert(vmain.begin()+1, vec);
 
D

Daniel T.

"Lorn said:
Hopefully someone can help me out with this problem I'm having with 2d
vectors. My vector initialization looks like this:

struct Object {
double d1;
double d2;
int i1;
};

std::vector<std::vector <Object> > vMain;
std::vector<Object> vec;

IMHO, don't do the above, your just making it hard on yourself (as you
are learning.) Create a Matrix class, if you know that each row will
have the same number of columns, then use a single vec internally, else
use a map.

template < typename T >
class RaggedMatrix {
typedef map< pair< int, int >, T > RepType;
RepType _rep;
public:
T& operator()( int x, int y ) {
return _rep[ make_pair(x, y) ];
}

const T& operator()( int x, int y ) const {
typename RepType::const_iterator it = _rep.find( make_pair(x, y) );
if ( it == _rep.end() )
throw out_of_range("RaggedMatrix::eek:perator()(int, int)const");
return it->second;
}
};
/////
When I add objects in the vectors it looks like this:

Object stData2 = { 2, 2, 2};
Object stData3 = { 3, 3, 3};
Object stData4 = { 4, 4, 4};

vec.push_back(stData2);
vMain.push_back(vec);
vec.clear();
vec.push_back(stData3);
vMain.push_back(vec);
vec.clear();

With the above, you can easily add objects:

RaggedMatrix rMain;
rMain(0, 0) = stData2;
rMain(0, 1) = stData3;
rMain(1, 0) = stData4;

&c.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,178
Latest member
Crypto Tax Software
Top