Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

invalid initialization of non-const reference

8 views
Skip to first unread message

opengl

unread,
Mar 2, 2010, 8:03:57 AM3/2/10
to
Hi All,

Here is one simple C++ program, if I run this in Visual studio, it
will never give error...But in Mac it gives error.

//-------------------------------------------------- program
---------------------------------
#include <iostream>


class Test
{
private:

public:
void TestIt(Test inObj)
{
Test &newObj = getObj() ; // Gives error here : invalid
initialization of non-const reference of type 'Test&' from a temporary
of type 'Test'

class Test
{
private:

public:
void TestIt(Test inObj)
{
Test &newObj = getObj() ;
}

Test getObj() const
{
Test obj ;
return obj;
}

};

int main (int argc, char * const argv[])
{

Test obj ;
obj.TestIt(obj);
return 0;
}

}

Test getObj() const
{
Test obj ;
return obj;
}

};

int main (int argc, char * const argv[])
{

Test obj ;
obj.TestIt(obj);
return 0;
}
//-------------------------------------------------- End
---------------------------------

Error : invalid initialization of non-const reference of type

Anyone faced this problem?

Jim Langston

unread,
Sep 25, 2010, 3:23:38 AM9/25/10
to
Comment inline.

"opengl" <gururaj...@gmail.com> wrote in message
news:fb088c5f-efd9-47b1...@c37g2000prb.googlegroups.com...


> Hi All,
>
> Here is one simple C++ program, if I run this in Visual studio, it
> will never give error...But in Mac it gives error.
>
> //-------------------------------------------------- program
> ---------------------------------
> #include <iostream>
>
>
> class Test
> {
> private:
>
> public:
> void TestIt(Test inObj)

Try changing this to:
void TestIt( Test& inObj )
see if that fixes it.

0 new messages