473,892 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create new variable in function to return?

101 New Member
Ok, this is my code for my function:

Expand|Select|Wrap|Line Numbers
  1. // Puke - new pointers only point to mTemp
  2. Object &Coordinate::stripObject(std::string name) {
  3.     for ( int i=0; i < mItems.size(); i++ ) {
  4.     if ( mItems.at(i)->getName() == name ) {
  5.         mTemp = *mItems.at(i);
  6.         mItems.erase(mItems.begin() + i);
  7.         return mTemp;
  8.     }
  9.     }
  10.     Object item("Nothing");
  11.     mTemp = item;
  12.     return mTemp;
  13. }
This, of course, is not what I want to do. I want to create a brand new variable and return it. Every time I do this, however, g++ gives an error (probably because the created variable falls out of scope)

BTW - Any variable with an 'm' as the fist character is a member variable.

Any help would be greatly appreciated :)
Mar 6 '08 #1
3 1612
gpraghuram
1,275 Recognized Expert Top Contributor
Ok, this is my code for my function:

Expand|Select|Wrap|Line Numbers
  1. // Puke - new pointers only point to mTemp
  2. Object &Coordinate::stripObject(std::string name) {
  3.     for ( int i=0; i < mItems.size(); i++ ) {
  4.     if ( mItems.at(i)->getName() == name ) {
  5.         mTemp = *mItems.at(i);
  6.         mItems.erase(mItems.begin() + i);
  7.         return mTemp;
  8.     }
  9.     }
  10.     Object item("Nothing");
  11.     mTemp = item;
  12.     return mTemp;
  13. }
This, of course, is not what I want to do. I want to create a brand new variable and return it. Every time I do this, however, g++ gives an error (probably because the created variable falls out of scope)

BTW - Any variable with an 'm' as the fist character is a member variable.

Any help would be greatly appreciated :)

Instead of returning the local object pass a variable as an argument(by reference) and then initialize it
Expand|Select|Wrap|Line Numbers
  1. void Coordinate::stripObject(std::string name,Object &retObj) {
  2. Object item("Nothing");
  3. retObj = item;
  4. }
  5.  
Raghuram
Mar 6 '08 #2
zensunni
101 New Member
Thanks for input, but I found a way..

Expand|Select|Wrap|Line Numbers
  1. Object &Character::stripObject(std::string name) {
  2.     for ( int i=0; i < mItems.size(); i++ ) {
  3.     if ( mItems.at(i)->getName() == name ) {
  4.         Object *item = mItems.at(i);
  5.         mItems.erase(mItems.begin() + i);
  6.         return *item;
  7.     }
  8.     }
  9.     std::cout << "Couldn't find " << name << "\n\n";
  10.     Object *item = new Object("Nothing");
  11.  
  12.     return *item;
  13. }
thanks again, though.
Mar 6 '08 #3
gpraghuram
1,275 Recognized Expert Top Contributor
Thanks for input, but I found a way..

Expand|Select|Wrap|Line Numbers
  1. Object &Character::stripObject(std::string name) {
  2.     for ( int i=0; i < mItems.size(); i++ ) {
  3.     if ( mItems.at(i)->getName() == name ) {
  4.         Object *item = mItems.at(i);
  5.         mItems.erase(mItems.begin() + i);
  6.         return *item;
  7.     }
  8.     }
  9.     std::cout << "Couldn't find " << name << "\n\n";
  10.     Object *item = new Object("Nothing");
  11.  
  12.     return *item;
  13. }
thanks again, though.
Using pointers is another option but the idea i told you will avoid the overhead of deleting the pointer in the calling part.

Raghuram
Mar 6 '08 #4

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

Similar topics

4
24191
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); function loadUrlVariables() { varString = location.search;
8
3201
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { } Client.prototype.fullname = "John Smith"; var s = "Client"; eval("var o = new " + s + "();"); alert(o.fullname);
7
8882
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I want my users to be able to select a report, click on a command button on a form, which will then automatically create the report as a pdf file and save it to the user's machine. I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
3
2081
by: moondaddy | last post by:
How can I create a public variable similar to an Enum, but rather than returning integer values it would return String values? -- moondaddy@nospam.com
2
3068
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is what I'm hoping to achieve. I've never before had to use Javascript closures, but now I do, so I'm making an effort to understand them. I've been giving this essay a re-read: http://jibbering.com/faq/faq_notes/closures.html
4
12460
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
4
6932
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
4
8291
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my problems and this one is really getting to me... I have a page that allows you to Browse Authors. There are three drop down boxes that auto-populate via AJAX. I have a file which it calls and returns the dynamically built XML file in the boxes...
14
5208
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to create the child element. At the point where the script is supposed to call itself with the new properties to create the parent, it gives me the error and looks like it's looping the function continuously. It seems that "id : attrib" (line 192) is...
0
9981
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
11241
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10836
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10468
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...
0
9644
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
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
6065
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4683
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
3
3288
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.