472,784 Members | 886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

How to create new variable in function to return?

101 100+
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 1567
gpraghuram
1,275 Expert 1GB
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 100+
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 Expert 1GB
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
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(); ...
8
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() { }...
7
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...
3
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
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...
4
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...
4
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...
4
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...
14
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...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.