473,320 Members | 1,794 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,320 software developers and data experts.

2d vector error

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

Feb 2 '06 #1
6 2951
Lorn wrote:
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

Feb 3 '06 #2
Lorn wrote:
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?
Feb 3 '06 #3
>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

Feb 3 '06 #4
Ahhhhh... ok, I get it. The correct notation here would be:

vMain[0].push_back(stData4);

Thanks guys :-)

Lorn

Feb 3 '06 #5
Lorn wrote:
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);
Feb 3 '06 #6
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Lorn" <ef*******@yahoo.com> wrote:
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::operator()(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.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 3 '06 #7

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

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
4
by: Vish | last post by:
I cannot compile a simple code as below using cygwin. I believe I am using latest version of g++(3.3.1) I get following error is_equal.cpp:9:19: ccc.cpp: No such file or directory...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
11
by: sw | last post by:
Hi, Is it possible to insert a class <vec> which has a vector<double> member, into the vector<vec> veclist for e.g? I've been getting compilation errors when trying to insert using the vector...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
14
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
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...
7
by: tehn.yit.chin | last post by:
I am trying to experiment <algorithm>'s find to search for an item in a vector of struct. My bit of test code is shown below. #include <iostream> #include <vector> #include <algorithm>...
15
by: arnuld | last post by:
This is the partial-program i wrote, as usual, i ran into problems halfway: /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a function to open a file for input and then read...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.