编译器编译"io_service_"变量显示为:不能出现在常量表达式中
Compiler compile a 'io_service_' variable show as : cannot appear in a constant-expression
我创建了一个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.
}
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 使用自动推导的 lambda 参数作为常量表达式
- 生成提升::hana::set 的常量表达式问题
- 为什么不能用常量表达式声明数组?
- 不是 lambda 函数中的常量表达式
- 函数调用在常量表达式中必须具有常量值
- 错误:constexpr 变量'struct2Var'必须由常量表达式初始化
- 关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题
- 生成 constexpr 字符串表,不能产生常量表达式
- 整体模板参数。错误:在常量表达式中使用'this'
- 如何在满足常量表达式的同时将整数传递给指针,传递给 std::array<double、integer>?
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:'new'不能出现在常量表达式中
- 我可以写出小于 -0.5 两个 ulps 的常量表达式双精度吗?
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 为什么我不能在非常量表达式上使用此模板阶乘函数?
- C++ 使用变量而不是常量表达式初始化数组
- 使用函数参数作为常量表达式的一部分 - gcc vs clang
- 片段着色器中的"错误:在 GLSL 1.30 及更高版本中禁止使用非常量表达式索引的采样器数组"