0

我正在尝试编译此代码

#ifndef TCPIP_H
#define TCPIP_H

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

class TCPIP
{
        private:

            boost::asio::io_service io_service;
            tcp::resolver resolver;
            tcp::resolver::query query;
            tcp::resolver::iterator iterator;
            tcp::socket s;

        public:

        TCPIP():resolver(io_service),
        query("127.0.0.1", "2345"),
        iterator(resolver.resolve(query)),
        s(io_service),
        boost::asio::connect(s, iterator)
        {}
        ~TCPIP(){}
        void IO()
        {
            std::cout << "Enter message: ";
            char request[max_length];
            std::cin.getline(request, max_length);
            size_t request_length = std::strlen(request);
            boost::asio::write(s, boost::asio::buffer(request, request_length));

            char reply[max_length];
            size_t reply_length = boost::asio::read(s,
            boost::asio::buffer(reply, request_length));
            std::cout << "Reply is: ";
            std::cout.write(reply, reply_length);
            std::cout << "\n";
        }
};
#endif

我得到了这些错误

c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\asio\detail\config.hpp|205|warning: #warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately.|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\asio\detail\config.hpp|206|warning: #warning For example, add -D_WIN32_WINNT=0x0501 to the compiler command line.|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\boost\asio\detail\config.hpp|207|warning: #warning Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).|
C:\Documents and Settings\My Documents\project5\erlang_connect\TCPIP.h||In constructor 'TCPIP::TCPIP()':|
C:\Documents and Settings\My Documents\project5\erlang_connect\TCPIP.h|26|error: expected class-name before '(' token|
C:\Documents and Settings\My Documents\project5\erlang_connect\TCPIP.h|26|error: expected '{' before '(' token|
C:\Documents and Settings\My Documents\project5\erlang_connect\erlang_connect.cpp||In function 'int main()':|
C:\Documents and Settings\My Documents\project5\erlang_connect\erlang_connect.cpp|7|error: expected primary-expression before ';' token|
C:\Documents and Settings\My Documents\project5\erlang_connect\erlang_connect.cpp|5|warning: unused variable 'INTERFACE'|
||=== Build finished: 3 errors, 4 warnings ===|

基本上这个问题与初始化列表和声明的问题有关boost::asio::connect(s, iterator)。这是用于制作它的原始 Boost 源代码(boost 阻塞 tcp-client)。我尝试使用指针,但 boost 库不喜欢这个主意!

//
// blocking_tcp_echo_client.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 3)
    {
      std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
      return 1;
    }

    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
    tcp::resolver::iterator iterator = resolver.resolve(query);

    tcp::socket s(io_service);
    boost::asio::connect(s, iterator);

    using namespace std; // For strlen.
    std::cout << "Enter message: ";
    char request[max_length];
    std::cin.getline(request, max_length);
    size_t request_length = strlen(request);
    boost::asio::write(s, boost::asio::buffer(request, request_length));

    char reply[max_length];
    size_t reply_length = boost::asio::read(s,
        boost::asio::buffer(reply, request_length));
    std::cout << "Reply is: ";
    std::cout.write(reply, reply_length);
    std::cout << "\n";
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}
4

1 回答 1

2

在初始化列表中,表达式需要调用父类的任一成员的构造函数。要解决这个问题,只需移动boost::asio::connect()到构造函数主体。

TCPIP()
  : resolver(io_service),              // calls resolver constructor
    query("127.0.0.1", "2345"),        // calls query constructor
    iterator(resolver.resolve(query)), // calls iterator constructor
    s(io_service)                      // calls socket constructor
{
  boost::asio::connect(s, iterator);
}

此外,请考虑解析器、查询和迭代器是否需要在TCPIP对象的整个生命周期内都可以访问。TCPIP让它们成为' 的构造函数中的自动变量可能是值得的,结果如下:

TCPIP()
  : s(io_service)
{
  tcp::resolver resolver(io_service);
  tcp::resolver::iterator iterator =
    resolver.resolve(tcp::resolver::query(("127.0.0.1", "2345"));
  boost::asio::connect(s, iterator);
}
于 2013-02-23T16:20:05.927 回答