Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ skipping line of code?

Tags:

c++

gcc

g++

skip

Background

I am writing a multi-threaded, websocket server in C++.

Problem

When I try to integrate my HTTP parser, MKFAHTTPRequest Request( std::string( Buffer ) ); gets completely skipped during execution.

I've cleaned the project and added -Wall and -Werror (which should tell me that Request is an unused variable, but it doesn't).

void operator()(){
    while( true ){
        if( m_Socket->is_open() ){
            char Buffer[1024]; 

            boost::system::error_code Error; 

            std::cout << "MKFAConnection::operator()() - Reading..." << std::endl;
            m_Socket->read_some( boost::asio::buffer( Buffer, sizeof( Buffer ) ), Error ); 

            if( !Error ){
                // This line is getting skipped!?!?!?
                MKFAHttpRequest Request( std::string( Buffer ) );

                m_Socket->write_some( boost::asio::buffer( std::string( "Hello World" ) ) ); 

            } else break; 

        } else break; 

    }

}
like image 750
Nathan Wehr Avatar asked Feb 08 '13 14:02

Nathan Wehr


People also ask

How do you skip a line in C?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

Can C be written on one line?

Being a bit naive, standard C requires an empty newline at the end of your source code, so 1-line programs that actually do something are impossible.

What do you do when a line of code is too long?

Summary. If you have a very long line of code in Python and you'd like to break it up over over multiple lines, if you're inside parentheses, square brackets, or curly braces you can put line breaks wherever you'd like because Python allows for implicit line continuation.


1 Answers

MKFAHttpRequest Request( std::string( Buffer ) );

This line doesn't do what you think it does. You think it defines an object named Request of type MKFAHttpRequest and initializes the object with a temporary object of type std::string.

In fact, it declares a function named Request which accepts a single parameter of type std::string and returns an object of type MKFAHttpRequest.

This is related to (or perhaps an example of) the most vexing parse.

Perhaps one of these will make it better:

MKFAHttpRequest Request( (std::string( Buffer )) );
MKFAHttpRequest Request{ std::string( Buffer ) };
MKFAHttpRequest Request = std::string( Buffer );
MKFAHttpRequest Request = MKFAHttpRequest(std::string( Buffer ));

Ref: http://en.wikipedia.org/wiki/Most_vexing_parse

like image 185
Robᵩ Avatar answered Oct 17 '22 04:10

Robᵩ