简体   繁体   English

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

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

I created a ServerService namespace for containing a class name server_datetime. 我创建了一个ServerService命名空间,用于包含类名称server_datetime。 Server_datetime class as tutorial at Boost examples, but I improved server_datetime class by using template parameter for inserting io_service ( boost::asio::io_service ) and endpoint ( tcp::endpoint(tcp::v4(),SIZE_DATA) ) objects to template. Server_datetime类作为Boost示例的教程,但是我通过使用模板参数将io_service(boost :: asio :: io_service)和终结点(tcp :: endpoint(tcp :: v4(),SIZE_DATA))对象插入模板来改进了server_datetime类。 I follow as example below : 我遵循以下示例:

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_;
};
}

Main function called server_datetime class by follow as: 主要功能称为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;
}

After main function compiled by compiler, Compiler show error as: 通过编译器编译主函数后,编译器显示错误为:

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

您忘记了(),应为:

 std::string message = make_daytime_string();

The server_datetime template expects type names (it says so right at the top of the first source code), but you supply values instead. server_datetime模板期望使用类型名称(它在第一个源代码的顶部这样说),但是您可以提供值。

Perhaps you shouldn't create the io_service_ in main, but let the server do that? 也许您不应该在main中创建io_service_,但是让服务器执行此操作?

Template parameters are for specifying types and constant values at compile-time, not for injecting objects at run-time; 模板参数用于在编译时指定类型和常量值,而不用于在运行时注入对象。 that is what ordinary function/constructor parameters are for. 这就是普通的函数/构造函数参数。 So in this case, if you provide the service and endpoint to the server, pass them as arguments to the constructor. 因此,在这种情况下,如果将服务和端点提供给服务器,则将它们作为参数传递给构造函数。

There are also a few other errors in the code; 代码中还存在其他一些错误; here are a few corrections (although there may still be problems, and I may have introduced some myself): 这里有一些更正(尽管可能仍然存在问题,并且我可能已经介绍了一些自己的问题):

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.
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM