gpt4 book ai didi

c++ - 编译器编译一个 'io_service_' 变量显示为 : cannot appear in a constant-expression

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:53 25 4
gpt4 key购买 nike

我创建了一个包含类名 server_datetime 的 ServerService 命名空间。 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;
}

编译器编译main函数后,编译器报错如下:

..\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();

关于c++ - 编译器编译一个 'io_service_' 变量显示为 : cannot appear in a constant-expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5698715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com