473,386 Members | 1,706 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,386 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 1594
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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,...

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.