c++ - 在 C++ 中将 sqrt(var) 声明为编译时间常数
问题描述
我有一个 c++ 程序,我需要在 for 循环中传递一个数字的平方根。
#include<random>
#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <omp.h>
using namespace std;
int main()
{
vector<int>inputDataBits(49); // vector of randomly generated input data bits
#ifdef printDebug
std::cout << "the input data bits are" << endl;
std::cout << "-------------------------" << endl << endl;
int var =49;
const int r=(int)sqrt(var);
float input2d[r][r];
for (int i = 0; i < r; i++)
{
for (int j = 0; j < r; j++)
{
input2d[i][j] = inputDataBits[(j %r) + (i *r)];
std::cout << input2d[i][j] << "\t";
}
std::cout << endl << endl;
}
std::cout << endl << endl;
#endif
return 0;
}
我得到一个错误'表达式必须有一个常量值'。有没有办法在 C++ 中做到这一点?
解决方案
推荐阅读
- c - 变量被 printf 中的后续变量覆盖
- python - 在 sklearn 逻辑回归中如何选择初始偏差值?
- android - 有什么方法可以使用云功能和 fcm 或其他替代方法来改进快速发送推送通知?
- azure-maps - GPS位置改变后,如何找回新的gps坐标?
- c# - ASP.NET MVC 5 如何将错误消息传递给 ajax 请求的结果
- python - #'charmap' 编解码器无法解码位置 1148 中的字节 0x8d
- angular - 升级到 Angular 11 后如何修复未捕获的 ReferenceError
- ruby-on-rails - Ruby on rails with AJAX - 非常简单的计算器
- reactjs - 在 Mac OS 上找不到 Expo 命令
- django - 我面临一个与进行迁移相关的 Django 错误……我该如何解决?安装的 Django 版本是 3.1.5