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

Pushing back a new struct on a list/vector

Consider this:

#inlcude <list>

struct elem {
int col;
int val;
};

int main() {
std::list<eleml;

l.push_back(elem e={1,1}); // Error

elem e = {1,1};
l.push_back(e); // OK

return 0;
}

Is there any way to make the non-working line work without adding a
constructor to elem? What I want to do is to create an anonymous struct
and pass it as an argument to a function using initialization. It's not
particulary important, it's just been bugging me for some time now.

--
Erik Wikström

Nov 22 '06 #1
3 3751
* er****@student.chalmers.se:
Consider this:

#inlcude <list>

struct elem {
int col;
int val;
};

int main() {
std::list<eleml;

l.push_back(elem e={1,1}); // Error

elem e = {1,1};
l.push_back(e); // OK

return 0;
}

Is there any way to make the non-working line work without adding a
constructor to elem?
No. And why would you want to?

What I want to do is to create an anonymous struct
and pass it as an argument to a function using initialization.
You might consider using std::pair, or (not yet standard) std::tr1::tuple.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '06 #2

"er****@student.chalmers.se 写é“:
"
Consider this:

#inlcude <list>

struct elem {
int col;
int val;
};

int main() {
std::list<eleml;

l.push_back(elem e={1,1}); // Error

elem e = {1,1};
l.push_back(e); // OK

return 0;
}

Is there any way to make the non-working line work without adding a
constructor to elem?
I think adding a constructor is required. You could use std::pair, but
it provides a constructor also, not by you, but by the STL.
What I want to do is to create an anonymous struct
and pass it as an argument to a function using initialization. It's not
particulary important, it's just been bugging me for some time now.

--
Erik Wikström
Nov 22 '06 #3
On 22 Nov 2006 01:07:20 -0800 in comp.lang.c++,
"er****@student.chalmers.se" <er****@student.chalmers.sewrote,
>Is there any way to make the non-working line work without adding a
constructor to elem? What I want to do is to create an anonymous struct
and pass it as an argument to a function using initialization.
Sorry, no way to do that, but you can make it nearly anonymous by
limiting the scope of the very short-lived name.

{elem e = {1,1}; l.push_back(e);}

Nov 22 '06 #4

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

Similar topics

7
by: {AGUT2} {H}-IWIK | last post by:
Hi! I'm implementing a new method of data input for my program using the push_back(int) function. When I compile my code (Borland free compiler), I get: "Could not find a match for...
3
by: Duncan | last post by:
I need to populate a vector with the following struct details:\ struct group { string groupname; int gid; list<string> members; }; the text file which this is to read from is in the...
1
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
6
by: paul.anderson | last post by:
This code doesn't work - the first retrieval of t2 returns valid data, the subsequent do not. Please help!!! int main(int argc, char* argc){ struct Test{ int i; int j; intk; int l; }
5
by: Alexander Adam | last post by:
Hi! I've got this issue: I have a struct like this: struct node { unsigned int m_code; ?? m_data; unsigned int m_data_size; };
6
jwwicks
by: jwwicks | last post by:
Hello Experts, This is a student question and I know you could use a list or some other keyed container but they haven't been introduced to the class so they aren't free game so to speak. ...
0
by: zman77 | last post by:
Hi. In C dll, there is a struct that contains an array of struct pointers. I do not know how to represent that in C#. That's my problem. Here is the relevant C code: struct vector { uint...
6
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then...
2
by: beet | last post by:
Hi all, I tried to declare a c++ struct like following in a header file; I want to include this header file in other files to create and access this struct. ------ 1 #ifndef _SEARCHDATA_H_...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.