关于c ++:编译器编译’io_service_’变量显示为:不能出现在常量表达式中

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

我为包含类名server_datetime创建了一个serverservice命名空间。Server_DateTime类是Boost示例中的教程,但我通过使用模板参数将IO_Service(Boost::Asio::IO_Service)和Endpoint(TCP::Endpoint(TCP::v4(),Size_Data))对象插入模板来改进了Server_DateTime类。我以下面的例子为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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类:

1
2
3
4
5
6
7
8
9
10
#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;
}

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

1
2
3
4
5
6
7
..\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

1
std::string message = make_daytime_string;

您忘记了(),应该是:

1
 std::string message = make_daytime_string();

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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.
}


服务器的datetime模板需要类型名(它在第一个源代码的顶部这样说),但您需要提供值。

也许您不应该在主服务器中创建IO服务,但是让服务器这样做?