473,398 Members | 2,403 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,398 software developers and data experts.

References stored in maps

Hi all,

I have a question concerning an index that I'm creating on a vector of
objects such that I can refer to the objects by integer. The index
will change, the vector *will not* (obviously, otherwise the pointers
would screw up). I have:

vector<MyObjectmyVector;
map<size_t, MyObject&myIndex;

Now, the compiler chucks at the declaration of myIndex giving "error:
reference to reference is not allowed". This is presumably because of
the reference to type MyObject in the map declaration. So, my
question is: is this a valid use of references in C++? Should I just
be using a plain old pointer to point to the various MyObjects in
myVector?

Thanks,

Chris
Nov 16 '07 #1
5 1345
On Nov 16, 10:44 am, Equus <chrism0...@gmail.comwrote:
Hi all,

I have a question concerning an index that I'm creating on a vector of
objects such that I can refer to the objects by integer. The index
will change, the vector *will not* (obviously, otherwise the pointers
would screw up). I have:

vector<MyObjectmyVector;
map<size_t, MyObject&myIndex;

Now, the compiler chucks at the declaration of myIndex giving "error:
reference to reference is not allowed". This is presumably because of
the reference to type MyObject in the map declaration. So, my
question is: is this a valid use of references in C++? Should I just
be using a plain old pointer to point to the various MyObjects in
myVector?

Thanks,

Chris
Just to be sure, you know that you can't update the vector beyond it's
reserved size after you've indexed it right, otherwise it'll
reallocate and your index will become invalid.

I guess the compiler is complaining that the map implementation is
using a T& for it's template parameter. In this case you're sticking
an object& into a reference parameter and, no, it's not valid. I'm not
sure if that implementation is required or if you're just unfortunate
with your stl implementation.

Given that the compiler won't let you use references, whether it's
valid within the standard or not, I'd say you need to change tactics.
Switch to a bare pointer or a smart pointer (maybe a reference counted
pointer in both the vector and the map?).
Nov 16 '07 #2
On 2007-11-16 11:44, Equus wrote:
Hi all,

I have a question concerning an index that I'm creating on a vector of
objects such that I can refer to the objects by integer. The index
will change, the vector *will not* (obviously, otherwise the pointers
would screw up). I have:

vector<MyObjectmyVector;
map<size_t, MyObject&myIndex;

Now, the compiler chucks at the declaration of myIndex giving "error:
reference to reference is not allowed". This is presumably because of
the reference to type MyObject in the map declaration. So, my
question is: is this a valid use of references in C++? Should I just
be using a plain old pointer to point to the various MyObjects in
myVector?
You can not store references in a container so in situations like these
you have to either use some kind of pointer or rethink your design.

--
Erik Wikström
Nov 16 '07 #3
Equus!
>Hi all,

I have a question concerning an index that I'm creating on a vector of
objects such that I can refer to the objects by integer. The index
will change, the vector *will not* (obviously, otherwise the pointers
would screw up). I have:

vector<MyObjectmyVector;
map<size_t, MyObject&myIndex;

Now, the compiler chucks at the declaration of myIndex giving "error:
reference to reference is not allowed". This is presumably because of
the reference to type MyObject in the map declaration. So, my
question is: is this a valid use of references in C++? Should I just
be using a plain old pointer to point to the various MyObjects in
myVector?

Thanks,

Chris
I've successfully used maps with int, pointer pairs, e.g., <int, XX *>
Why not just use a pointer to your MyObject rather than a reference?
Nov 18 '07 #4
I've successfully used maps with int, pointer pairs, e.g., <int, XX *>
Why not just use a pointer to your MyObject rather than a reference?
Yup, cheers all. I've defaulted to using pointers. Works fine :-)

Chris
Nov 18 '07 #5
Equus wrote:
Now, the compiler chucks at the declaration of myIndex giving "error:
reference to reference is not allowed". This is presumably because of
the reference to type MyObject in the map declaration. So, my
question is: is this a valid use of references in C++? Should I just
be using a plain old pointer to point to the various MyObjects in
myVector?
The type passed to a container must be copy constructible and
assignable. References do NOT meet these requirements.
Nov 19 '07 #6

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

Similar topics

6
by: dwilliams | last post by:
Our organization has implemented an ASP.net application with an n-tiered architecture, made up of a business/data layer that calls T-SQL Stored procedures. We are attempting to better manage the...
2
by: Tim.D | last post by:
Hello people, Thanks to Serge, PM, Knut and a host of others I have now successfully completed my very first SQL Stored Procedure. Takes some 13mins or so to run, but that was expected as it...
3
by: Sean | last post by:
Have you ever wanted to add the great features inherent in Google Maps? Here is how you do it. ============== == STEP ONE == ============== Create a new MS Access form called frmGoogleMap....
5
by: cmay | last post by:
If you add a reference to an ASP.NET 2.0 Web Project, where does it save that reference? I don't mean the DLL, I mean the reference to the DLL. In 2003 I believe it would put 1 line per...
1
by: John Dalberg | last post by:
I have a web site application (no solution or project files). I did a search on the site's folder to find where any of the references are stored and nothing came up. Where are the app's references...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
2
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
20
by: David Thielen | last post by:
Hi; When we add a reference to a web app, where is that information stored? There is no project file, it's not in the web.config or solution file. ??? - thanks - dave ...
1
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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,...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.