?
Solved

returning refrence to void poiter

Posted on 2001-06-12
2
Medium Priority
?
356 Views
Last Modified: 2011-09-20
I have following peace of C++ Code which compiles & runs perfectly fine on Sun & Microsoft but will not compile on HP 11


/********************************************/
#include <iostream.h>
int i;
class cls1{

public:
    void*& func1(int a, int b) {

         void *x;
     
         i=a+b;
         x=&i;

         return (void*) x;
    }
};

int main(){

    cls1 obj1;

    int * b;

    b = (int *)obj1.func1(5,6);

    cout<<*b<<endl;

    return 0;
}


/**********************************************/
Error Given on HPUX
Error (future) 438: "cls1.cpp", line 22 # The initializer for a non-constant reference must be an lvalue.
Try changing 'void *&' to 'void *const &' at ["cls1.cpp", line 16].                                
                     
   return (void*) x;
                            ^    

Error 582: "cls1.cpp", line 22 # Initialization of the result of "void *&cls1::func1(int,int)" requires
creating a temporary, yet the temporary's  lifetime ends with the return from the function.        
                   
   return (void*) x;  
                            ^

/*********************************************/
What could be the possible reason? Also what, if any, changes are required to do in this code so that
it runs equally good on all three platforms?
0
Comment
Question by:avi_india
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6183358
The compiler doesn't realize that what you're returning (essentially  a reference to i) will still be in scope after the return.

Try changing the relevant code to simply:

void*& func1(int a, int b) {
        i=a+b;
        return (void *)i;
   }
0
 
LVL 5

Accepted Solution

by:
marecs earned 45 total points
ID: 6184980
Or

void* func1(int a, int b) {                                                    
   return (void*) new int(a+b);                                                                  
}                                                                              
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question