C++ 循环:“;”标记之前的预期主表达式
C++ Loop: expected primary-expression before ‘;’ token
我有一个如下所示的循环,我认为问题出在标记为"请参阅此处"的内部 for 循环上。这有什么问题?
// measure time to write different sizes of data
for (int i = 0; i < sizeof(sizes)/sizeof(int); i++) {
lengthMod = sizes[i]/sizeof(int) - 1;
start = wall_clock_time();
for (unsigned int j = 0; j < 10; j++) // << See here!!!
tmp = 1;
// force any write back cache to flush. read from other data source
for (unsigned int j = 0; j < REPS; j++) // << Or here!!!
tmp = j;
end = wall_clock_time();
timeTaken = ((float)(end - start))/1000000000;
fprintf(stderr, "%d, %1.2f n", sizes[i]/1024, ((float)(end - start))/1000000000);
}
你可以在 GitHub 上看到完整的源代码 ~第 32 行
g++ -O3 cache-write.cpp -o cache-write -lrt
cache-write.cpp: In function ‘int main()’:
cache-write.cpp:36:27: error: expected primary-expression before ‘;’ token
cache-write.cpp:36:27: error: expected ‘)’ before ‘;’ token
cache-write.cpp:36:29: error: name lookup of ‘j’ changed for ISO ‘for’ scoping [-fpermissive]
cache-write.cpp:36:29: note: (if you use ‘-fpermissive’ G++ will accept your code)
cache-write.cpp:36:32: error: expected ‘;’ before ‘)’ token
cache-write.cpp:44:11: error: ‘data1’ was not declared in this scope
make: *** [cache-write] Error 1
不要在声明 REPS 时使用 #define,而是使用
const unsigned int REPS = whatever;
一般来说,C++避免使用 #define,因为它很容易导致你遇到的那种问题。
在 REPS 定义的末尾有一个分号:
#define REPS 128 * MB;
相关文章:
- C++正则表达式无限循环
- 为什么在 while 循环中返回表达式不起作用
- 如何在 c++ 下使用嵌套循环和正则表达式降低时间复杂度?
- 使用 constexpr 或模板元编程简化较长的展开循环表达式
- 符号和未签名整数表达式之间的比较,以进行循环误差
- 循环赋值的多维数组:表达式必须是可修改的左值
- 射击时从列表中删除外星人 - 找出循环;"表达式:无法递增结束列表迭代器"
- 如何在条件表达式中使用 'cin' 正确退出 while 循环?(C++,CLion)
- 基于范围的正则表达式匹配的 for 循环
- C++ 循环:“;”标记之前的预期主表达式
- 逗号(,)在条件部分中两个表达式之间的for循环中的工作方式
- 计算一个表达式来计算循环
- C++ 循环和布尔表达式
- 为什么我的正则表达式会导致无限循环
- 执行基本的 for 循环并得到错误:")"之前的预期主表达式
- 是否可以将多个关系表达式放入 for 循环的测试条件中
- 为什么不能在 do while 循环的表达式部分内声明一个变量?
- 基于范围的 for 循环是否缓存容器表达式,或每次迭代重新计算它
- C++:布尔表达式导致无限 while 循环
- 从嵌套循环创建数学表达式