From: GAP on
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.
class Test
{
private:

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

const Test &newObj = getObj() ; //Error occurs here, error: invalid
initialization of non-const reference of type

//Can it works like this?



}

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

};

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

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



Error : invalid initialization of non-const reference of type
Anyone faced this problem?

Thanks & Regards,
Gururaj T.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: SG on
On 5 Mrz., 11:56, GAP <gururaj.tal...(a)gmail.com> wrote:
>
> [...]
>
> const Test &newObj = getObj() ; //Error occurs here
>
> [...]
>
> Error : invalid initialization of non-const reference of type
> Anyone faced this problem?

This error message doesn't belong to this line of code.
Your code compiles fine. see http://codepad.org/Agt5T0Vh
("No errors or program output")

Cheers,
SG


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Guru on
On Mar 5, 7:24 pm, SG <s.gesem...(a)gmail.com> wrote:
> On 5 Mrz., 11:56, GAP <gururaj.tal...(a)gmail.com> wrote:
>
>
>
> > [...]
>
> > const Test &newObj = getObj() ; //Error occurs here
>
> > [...]
>
> > Error : invalid initialization of non-const reference of type
> > Anyone faced this problem?
>
> This error message doesn't belong to this line of code.
> Your code compiles fine. seehttp://codepad.org/Agt5T0Vh
> ("No errors or program output")

{ edits: quoted signature and banner removed. please keep readers in mind when
quoting. -mod }

Sorry for the inconvenience.
Please make sure the following changes

Originally posted code:
const Test &newObj = getObj() ; //Error occurs here, error: invalid
initialization of non-const reference of type

Changes done:
Test &newObj = getObj() ; //Error occurs here, error: invalid
initialization of non-const reference of type



Without const object the code works fine in windows vc++ compiler,
but the same code gives error in gcc 4.0

So I made object as const to fix the error, which is not correct
actually.Because as I need to update the object states for which I
need non cost object.

Can u please tell me why gcc gives error but not vc++, which is the
correct one?




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]