Jump to content
  • Advertisement
Sign in to follow this  
shrtysk8r1

Another g++ compiler error

This topic is 4819 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Please bare with me, as i have been converting my application to linux and run into a few differences between MSVC and g++ compilers. The following code produces an error in g++ and not in MSVC:
void foo(Class& c)
{
  (...)
}

int bar(void)
{
   foo(Class(param1, param2));
}
The error is that the compiler is unable to convert from type c to c&. When i convert the code to the following - everything works as planned, however i would like to avoid the extra variable and line of code.
int bar(void)
{
    Class temp(param1, param2);
    foo(temp);
}
If anyone is able to provide some insight into my "problem" it would be greatly appreciated. [Edited by - shrtysk8r1 on June 6, 2005 9:33:58 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm assuming you meant "Class" not "c" as argument for the call to foo.

The later versions of GCC claim that its behavior is standards compliant -- you can't have a non-const reference to a temporary object. Microsoft, meanwhile, appears to believe that that's OK. Just re-formulate the code like the latter, and it'll work in both cases.

You get into similar problems when nuking STL containers:


std::container<SomeType> myContainer;


myContainer.swap( std::container<SomeType>() ); // doesn't work on GCC
std::container<SomeType>().swap( myContainer ); // works on both

Share this post


Link to post
Share on other sites
Yes, sorry for the typo -- I just recently found that the reference had to be const for that to work, so i guess there's no way of getting around the extra line, thanks for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!