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

| 我创建了一个ServerService命名空间,用于包含类名称server_datetime。 Server_datetime类作为Boost示例的教程,但是我通过使用模板参数将io_service(boost :: asio :: io_service)和端点(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;
}
通过编译器编译主函数后,编译器显示错误为:
..\\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();
    
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.
}
    

要回复问题请先登录注册