简体   繁体   English

使用 Visual Studio Boost asio TCP IP asio 类编译错误。 错误:C2228:“.close”的左边必须有类/结构/联合

[英]Boost asio TCP IP asio class compile error with Visual Studio. Error: C2228: left of '.close' must have class/struct/union

I'm trying to follow the steps provided in this page我正在尝试按照此页面中提供的步骤进行操作

http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/tutorial/tutdaytime1.html http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/tutorial/tutdaytime1.html

However, at some point the code does not compile, and gives me the specified error.但是,在某些时候代码无法编译,并给我指定的错误。 I can't understand what kind of conflict it is.我无法理解这是一种什么样的冲突。 The object tcpsock doesn't seem to be correctly created.对象 tcpsock 似乎未正确创建。 Please check my code:请检查我的代码:

io_service io_tcp;
tcp::resolver resolverObject(io_tcp);
tcp::resolver::query queryObject(argv[1], "daytime");
tcp::resolver::iterator endpoint_iterator = resolverObject.resolve(queryObject);
tcp::resolver::iterator end; //default constructor is end iterator

tcp::socket tcpsock(io_service);
boost::system::error_code socketError = boost::asio::error::host_not_found;

while (socketError && endpoint_iterator != end)
{
    //Apparently, the object isn't created correctly
    tcpsock.close(); //error happenes here
    tcpsock.connect(*endpoint_iterator++, socketError); //error happenes here too
}
if (socketError)
    throw boost::system::system_error(socketError);

Thank you for any efforts.感谢您的任何努力。

要初始化tcpsock与类型io_service ,而不是与变量io_tcp

I also found this compile error but from a different angle given the following:我也发现了这个编译错误,但从不同的角度给出了以下内容:

    boost::asio::read(socket, replyBuf, boost::asio::transfer_all(), error);

Compared to the correct:与正确的相比:

boost::asio::read(*socket, replyBuf, boost::asio::transfer_all(), error);

Notice the need the de-reference the socket.请注意需要取消引用套接字。 It seems boost's use of common base types prevents the usual intellisense errors.似乎 boost 使用常见的基本类型可以防止常见的智能感知错误。

In the future I recommend adding a null check before use for the args of a boost type at runtime.将来,我建议在运行时为 boost 类型的参数使用之前添加空检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:C2228:“”的左侧必须具有class / struct / union - Error: C2228: left of '' must have class/struct/union 错误22错误C2228:'.size'的左侧必须具有class / struct / union - Error 22 error C2228: left of '.size' must have class/struct/union 错误C2228:'。Alloc'的左边必须有class / struct / union - error C2228: left of '.Alloc' must have class/struct/union 错误C2228:“。x”的左侧必须具有class / struct / union - error C2228: left of '.x' must have class/struct/union 在异常情况下,C ++错误C2228('。''的左边必须有class / struct / union) - C++ Error C2228 (left of '.val' must have class/struct/union) in unusual circumstances 错误C2228:'。size'的左边必须有class / struct / union - error C2228: left of '.size' must have class/struct/union 获取错误代码C2228:“ ._ Ptr”的左侧必须具有class / struct / union - Getting error code C2228: left of '._Ptr' must have class/struct/union 错误C2228:'.DXGI_MODE'的左边必须有class / struct / union Direct X. - error C2228: left of '.DXGI_MODE' must have class/struct/union Direct X 错误C2228:'。words'的左边必须有class / struct / union - error C2228: left of '.words' must have class/struct/union 错误C2228:'.draw'的左边必须有class / struct / union - error C2228: left of '.draw' must have class/struct/union
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM