编译器编译"io_service_"变量显示为:不能出现在常量表达式中

Compiler compile a 'io_service_' variable show as : cannot appear in a constant-expression

本文关键字:常量 表达式 不能 io 编译 service 变量 编译器 显示      更新时间:2023-10-16

我创建了一个ServerService命名空间,用于包含类名server_datetime。Server_datetime类作为Boost示例中的教程,但我通过使用模板参数将io_service(Boost::asio::io_seervice(和endpoint(tcp::endpoint(tcp::v4((,SIZE_DATA((对象插入到模板中,改进了Server_datetime类别。我以下面的例子为例:

using boost::asio::ip::tcp;
namespace ServerService{
template<typename Service, typename Endpoint>
class server_datetime {
public:
    server_datetime(){
        acceptor_(service_, endpoint_);
        for(;;)
        {
            tcp::socket socket(Service);
            acceptor_.accept(socket);
            std::string message = make_daytime_string;
            boost::system::error_code ignored_error;
            boost::asio::write(socket, boost::asio::buffer(message),boost::asio::transfer_all(), ignored_error);
        }
    }
    std::string make_daytime_string(){
        std::time_t now = std::time(0);
        return std::ctime(&now);
    }
    virtual ~server_datetime();
private:
    tcp::acceptor acceptor_;
    Service service_;
    Endpoint endpoint_;
};
}

主函数调用server_datetime类,如下所示:

#include "server_datetime.hpp"
using namespace std;
using boost::asio::ip::tcp;
int main() {
    const boost::asio::io_service io_service_;
    const int SIZE_DATA = 13;
    ServerService::server_datetime<io_service_, tcp::endpoint(tcp::v4(),SIZE_DATA)  >  server;
    cout << "" << endl; // prints 
    return 0;
}

编译器编译主函数后,编译器显示错误为:

..srcconnectk.cpp: In function 'int main()':
..srcconnectk.cpp:10: error: 'io_service_' cannot appear in a constant-expression
..srcconnectk.cpp:10: error: 'boost::asio::ip::tcp::v4()' cannot appear in a constant-expression
..srcconnectk.cpp:10: error: a function call cannot appear in a constant-expression
..srcconnectk.cpp:10: error: template argument 1 is invalid
..srcconnectk.cpp:10: error: template argument 2 is invalid
..srcconnectk.cpp:10: error: invalid type in declaration before ';' token
std::string message = make_daytime_string;

你忘记了((,应该是:

 std::string message = make_daytime_string();

server_datetime模板需要类型名称(它在第一个源代码的顶部这样说(,但您提供的却是值。

也许您不应该在main中创建io_service_,而是让服务器来创建?

模板参数用于在编译时指定类型和常数值,而不是用于在运行时注入对象;这就是普通函数/构造函数参数的作用。因此,在这种情况下,如果您向服务器提供服务和端点,请将它们作为参数传递给构造函数。

代码中还有一些其他错误;以下是一些更正(尽管可能仍然存在问题,我可能已经自己介绍了一些(:

namespace ServerService{
// Put 'using' declarations inside the namespace,
// to avoid polluting the global namespace
using boost::asio::ip::tcp;
using boost::asio::io_service;
// Not a template - pass runtime objects as constructor arguments
class server_datetime {
public:
    server_datetime(io_service & service, tcp::endpoint const & endpoint) :
        // Initialise members in an initialiser list
        acceptor_(service, endpoint),
        service_(service)
    {}
    // Put the main loop in a separate function; it's rather odd
    // to have a constructor that doesn't return.
    void run(){
        for(;;)
        {
            // Argument must be the object service_, not the type Service
            tcp::socket socket(service_); 
            acceptor_.accept(socket);
            std::string message = make_daytime_string(); // missing parentheses
            boost::system::error_code ignored_error;
            boost::asio::write(socket, boost::asio::buffer(message),boost::asio::transfer_all(), ignored_error);
        }
    }
    std::string make_daytime_string(){
        std::time_t now = std::time(0);
        return std::ctime(&now);
    }
    // No need for a virtual destructor - this class is not polymorphic
private:
    boost::asio::io_service & service_; // must be a reference - io_service is not copyable
    tcp::acceptor acceptor_;
    // No need to store the endpoint - it's only used to initialise acceptor_.
};
}
int main() {
    using boost::asio::ip::tcp;
    // can't be const if you want to use it
    boost::asio::io_service io_service_;
    // renamed SIZE_DATA and given it the same type as the constructor argument
    const unsigned short port = 13; 
    ServerService::server_datetime server(io_service_, tcp::endpoint(tcp::v4(),port));
    server.run();
    // no need to explicitly return zero, unless you want to.
}