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

[Warning] reference to local variable returned

Hello,

The following function gives me a warning:

template <class V, class AI, class I, class S>
V& AssocArray<V, AI, I, S>::operator [] (const AI& index)
{

iterator it = internal_array.find(index);

V mapIndex = (*it).second;
return mapIndex;


}

[Warning] reference to local variable `mapIndex' returned

I could not figure out the problem with mapIndex here.
Jul 5 '07 #1
2 10942
Hello,

The following function gives me a warning:

template <class V, class AI, class I, class S>
V& AssocArray<V, AI, I, S>::operator [] (const AI& index)
{

iterator it = internal_array.find(index);

V mapIndex = (*it).second;
return mapIndex;


}

[Warning] reference to local variable `mapIndex' returned

I could not figure out the problem with mapIndex here.
I'm not positive, but I think it might be because you are returning the address (reference) of a variable declared in the function. Once the function ends (after the return statement) that variable will go out of scope and can't be accessed. And so you're getting a warning because you'll essentially have the address of a non-existent variable.

Just get rid of the last 2 lines and replace them with this:

return (*it).second;

or this should work as well:

return it->second;
Jul 5 '07 #2
Ok, thanks. I don't understand what happened but once I restarted the compiler it did not give me any errors.
Jul 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
5
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
10
by: ma740988 | last post by:
I'm perusing source that quite frankly looks like a disaster in the making. This is a case though where return by reference gets muddled with my understand based on readings. struct bar {...
5
by: Gary Wessle | last post by:
Hi I am getting a warning when I run this program, I am interested to know why the compiler is warning. thank you **************** warning **************** $ make clean; make rm -rf *.o...
12
by: ypjofficial | last post by:
Hello All, I need to return a vector from a function.The vector is of int and it can contain as many as 1000 elements.Earlier I was doing //function definition vector<intretIntVector() {...
14
by: PengYu.UT | last post by:
Hi, I have the following which has a bug (see the commented line). The one with the bug has the output of 10 -1073752704 The one without the bug has the output of 10
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
11
by: ravi | last post by:
Can anybody out there tell me whether it i possible to return a reference from a function in C++ I am using Turbo C++
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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: 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: 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
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
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,...

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.