Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:54:03+00:00 2026-06-17T22:54:03+00:00

I am trying to learn computer graphics using the book Interactive computer graphics –

  • 0

I am trying to learn computer graphics using the book Interactive computer graphics – A top-down approach, the code of the examples is in this link

There’s a header library called ´mat.h´ that gives some matrix utilities, but when I try to compile with Xcode the following error is thrown:

Non-const lvalue reference to type ‘Angel::mat2’ cannot bind to a temporary of type ‘Angel::mat2’

The piece of code that throws that error is:

mat2& operator /= ( const GLfloat s ) {
#ifdef DEBUG
    if ( std::fabs(s) < DivideByZeroTolerance ) {
        std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
                  << "Division by zero" << std::endl;
        return mat2();
    }
#endif // DEBUG

I just comment it out because it just gets compiled when debugging the app, but I would like to know what’s the problem and how to solve it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:54:04+00:00Added an answer on June 17, 2026 at 10:54 pm

    Strictly speaking, while the formal reason for the error is the attempt to bind a non-const reference to a temporary, the real problem here is the very attempt to return a reference, any reference to a temporary object. Whether the reference is const or not does not matter.

    The temporary object will be destroyed right after the return statement completes, resulting in a dangling reference bound to a now-destroyed object being returned. In other words, even if we attempt to “fix” this code by changing the return type to const reference, it still won’t work properly.

    Moreover, it appears that by design this function is supposed to return a non-const reference, meaning that changing the return type of this function is not an option. A non-const reference is what compound assignment operators typically return. The debug branch is just supposed to terminate early (and, of course, to return something, anything just to make the code compile) in case of “divide by zero” situation.

    One way to achieve that is to declare a standalone object of type mat2 (as a static member of class mat2, for example) and return a reference to it. I.e. declare

    class mat2 {
      ...
    #ifdef DEBUG
      static mat2 bad_result;
    #endif // DEBUG
    };
    

    define it

    #ifdef DEBUG
    mat2 mat2::bad_result;
    #endif // DEBUG
    

    and then do

    return bad_result;
    

    whenever an error is detected.

    Alternatively (and much more easily) you can declare it locally right before that return statement

    #ifdef DEBUG
        if ( std::fabs(s) < DivideByZeroTolerance ) {
            std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
                      << "Division by zero" << std::endl;
            static mat2 bad_result;
            return bad_result;
        }
    #endif // DEBUG
    

    Of course, there’s an obvious flaw in this design, since the outside code will be able to modify the returned object, which is undesirable. However, most likely the idea is that the program’s behavior is not guaranteed once an error message is printed to std::err, meaning that the modifiability of the returned value should not be a problem in this case.

    Taking that last point into account, we can even use a non-static local variable as a return value

    #ifdef DEBUG
        if ( std::fabs(s) < DivideByZeroTolerance ) {
            std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
                      << "Division by zero" << std::endl;
            mat2 bad_result;
            return bad_result;
        }
    #endif // DEBUG
    

    To return a reference to a local variable is as wrong as to return a reference to a temporary (for largely the same reasons). However, within the “no guarantees after an error” approach it will “work”, meaning that it will fix the error message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following my attempt to learn CG and OpenGL I found the book Interactive Computer
I'm trying to learn php but something goes wrong... When I use this code:
Trying to learn a bit about PDO and is going through this tutorial .
Trying to learn Django, I closed the shell and am getting this problem now
I'm trying to learn JavaScript, but the following code has been giving me a
I'm trying to find the physical location of a computer using a language like
I am trying to learn XML and I know this is a problem with
I am new to computer programming, and trying to learn some possibilities to track
I'm trying to learn OpenGL and using openglbook.com as my reference. I'm trying to
I am trying to learn how to do this .NET frameworks for my job

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.