473,796 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Obj ect> 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(s tData2);
vMain.push_back (vec);
vec.clear();
vec.push_back(s tData3);
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(s tData4);
vMain[0].push_back(vec) ;

/////
The error I receive is:

error C2664: 'std::vector<_T y>::push_back' : cannot convert parameter 1
from 'std::vector<_T y>' to 'const CTestDlg::Objec t &'
with
[
_Ty=CTestDlg::O bject
]
and
[
_Ty=CTestDlg::O bject
]
Reason: cannot convert from 'std::vector<_T y>' to 'const
CTestAPIDlg::Ob ject'
with
[
_Ty=CTestDlg::O bject
]
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 2968
Lorn wrote:
Hopefully someone can help me out with this problem I'm having with 2d
vectors.

vec.push_back(s tData2);
vMain.push_back (vec);
vec.clear();
vec.push_back(s tData3);
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(s tData4);
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<Obj ect> 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<Obj ect> 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(s tData2);
vMain.push_back (vec);
vec.clear();
vec.push_back(s tData3);
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(s tData4);
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).pus h_back(vec);

or

vMain.insert(vM ain.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(stDa ta4);

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(vm ain.begin(), vec);

or after the first position:
vmain.insert(vm ain.begin()+1, vec);
Feb 3 '06 #6
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
"Lorn" <ef*******@yaho o.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<Obj ect> 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("R aggedMatrix::op erator()(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(s tData2);
vMain.push_back (vec);
vec.clear();
vec.push_back(s tData3);
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
3209
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
4
7132
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 is_equal.cpp:11: error: `vector' was not declared in this scope is_equal.cpp:11: error: parse error before `>' token is_equal.cpp: In function `bool equal_vec(...)': is_equal.cpp:12: error: `a' undeclared (first use this function) is_equal.cpp:12: error: (Each...
7
10629
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 namespace std; : : vector<string>* getTyphoon()
11
6316
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 method push_back() -> c:\program files\microsoft visual studio\vc98\include\xutility(39) : error C2679: binary '=' : no operator defined which takes a right-hand
10
4848
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
4003
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); } // reflect this vector off surface with normal vector
18
2903
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" /////////////////////////////// source code /////////////////////////////// #include<iostream> #include <string> #include<vector> #include<sstream>
0
6137
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 /usr/include/c++/3.3.3/bits/stl_algo.h: In function `_RandomAccessIter std::find(_RandomAccessIter, _RandomAccessIter, const _Tp&, std::random_access_iterator_tag) ': /usr/include/c++/3.3.3/bits/stl_algo.h:298: instantiated from `_InputIter...
7
17059
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> #include <string> struct abc_struct {
15
5872
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 its coontents into a vector of strings, storinig each line as separate
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.