Solved

returning refrence to void poiter

Posted on 2001-06-12
2
337 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
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 15 total points
ID: 6184980
Or

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
FTP on FreeBSD server 2 92
dot directory in FreeBSD??? 4 60
Correct syntax to upload file in a script using sftp 2 64
UNIX SCP 5 48
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now