Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The if statement right above m_state = 2 is giving me a compiler warning about r being used without being initialized. I don't see how this is possible. Maybe I've been staring at it too long.

C++
gfx_result commit() {
    gfx_result r = gfx_result::success;
    if(m_state==0) {
        r = begin();
        if(r!=gfx_result::success) {
            return r;
        }
    }
    if(m_state==1) {
        r = m_batch.commit(m_async);
        if(m_async) {
            sus_type_async::resume(m_destination);
        } else {
            sus_type::resume(m_destination);
        }
        
        if(r!=gfx_result::success) {
            return r;
        }
        m_state = 2;
    }
    return m_state==2?gfx_result::success:gfx_result::invalid_state;
}


What I have tried:

Everything is in the question. I've tried renaming the variable to "rr" and it still warns me about "r". I'm sure it's building the file.
Posted
Updated 24-Apr-22 19:36pm
Comments
KarstenK 25-Apr-22 1:57am    
giving better var name would always help. Maybe you are looking at the wrong code.
honey the codewitch 25-Apr-22 6:53am    
it's just a temporary holder for an error return value. Typically for things like this I use one letter names. It actually makes it clearer because it's easier to scan visually what is "throwaway" and what is part of the actual algorithm.

There are two reasons I know I'm not looking at the wrong code. For one VS Code provides line #s and click to jump to the location of compiler error messages. I've also manually verified.

1 solution

Quote:
I've tried renaming the variable to "rr" and it still warns me about "r".

THen either it's not looking at the same file as you, or it's a different "r"

Start at the beginning: look at the error message and it'll tell you the exact file, and the line number within the file, and probably a column number as well. In VS, you can double click the error message in the Errors Pane and it'll go directly to the line, otherwise open that file in an editor using the full path it provides and use CTRL+G to go directly to the line.

Which line is it? Where is "r" being used? Where is the declaration relative to that (VS right click will get you to the definition of a function or variable.)

If you rename it and the error message doesn't reflect the name change, you are looking at the wrong code for the problem!
 
Share this answer
 
Comments
honey the codewitch 25-Apr-22 6:54am    
That's what I thought too so I recloned the git in a separate project. Same deal. I also am manually checking the compiler error line # with the line # shown by VS Code and it matches.

I'm starting to think there's a bug in the GCC compiler? I need to rename my variable again to see if I wasn't hallucinating.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900