c++ - 表达式必须有一个常量值
问题描述
我遇到了麻烦,无法解决,有人可以帮我吗,如果你能解释一下错误,请
template<unsigned long x>
struct dis
{
dis() { std::cout << x << std::endl; }
};
unsigned int bina(unsigned long x)
{
return x == 0 ? 0 : x % 10 + 2 * bina(x / 10);
}
int main()
{
unsigned long b;
std::cout << "Give a binary number:";
std::cin >> b;
dis<bina(b)>out; //here's the error
return 0;
}
错误是: expr 必须有一个 const 值
解决方案
b
不是一个常量表达式,所以bina(b)
也不是。
您可能会更改dis
为接受运行时值:
struct dis
{
dis(unsigned long x) { std::cout << x << std::endl; }
};
constexpr unsigned int bina(unsigned long x)
{
return x == 0 ? 0 : x % 10 + 2 * bina(x / 10);
}
int main()
{
unsigned long b;
std::cout << "Give a binary number:";
std::cin >> b;
dis /*out*/(bina(b));
return 0;
}
推荐阅读
- node-red - 模板节点 http out 返回“无响应对象”
- c++ - 智能指针:不存在合适的构造函数来从 derived_object* 转换为 std::shared_ptr
- java - 如何在循环中从 EditText 中提取文本并将其连接起来以在单个 TextView 中查看?
- javascript - 尽管范围是十六进制值,但 D3 scaleLinear() 返回 rgb 值
- strapi - 自动定时动作
- python - 联合子查询的 SqlAlchemy groupby
- haskell - 函数中的数据类型不匹配
- swift - 如何为结构实现哈希(进入哈希:输入哈希)
- python - 如何将 CoreML 模型转换为 TensorFlow 模型?
- java - 如何正确地从另一个实体中删除一个实体?