尝试编译时出现错误"expected unqualified-id before ‘{’ token {"
Getting error "expected unqualified-id before ‘{’ token {" when trying to compile
尝试编译程序时出现以下错误:
错误:在"{"令牌 { 之前应为非限定 ID
这是代码:
#include <iostream>
#include <vector>
using namespace std;
{
vector<int> prime;
bool is_prime(int n)
{
for (int p = 0; p<prime.size(); ++p)
if (n%prime[p]==0) return false; // no remainder: prime[p] divided
return true; // no smaller prime could divide
}
int main()
{
do
{
prime.push_back(2); // consider the smallest prime
for (int i = 3; i<=100; ++i) // test all integers [3:100]
if (is_prime(i)) prime.push_back(i); // add new prime to vector
cout << "Primes: ";
for (int p = 0; p<prime.size(); ++p)
cout << prime[p] << 'n';
}
}
#include <iostream>
#include <vector>
using namespace std;
vector<int> prime;
bool is_prime(int n)
{
for (int p = 0; p<prime.size(); ++p)
if (n%prime[p]==0) return false; // no remainder: prime[p] divided
return true; // no smaller prime could divide
}
int main()
{
prime.push_back(2); // consider the smallest prime
for (int i = 3; i<=100; ++i) // test all integers [3:100]
if (is_prime(i)) prime.push_back(i); // add new prime to vector
cout << "Primes: ";
for (int p = 0; p<prime.size(); ++p)
cout << prime[p] << 'n';
}
这是无错误的代码版本。
你不需要在主函数中"do {",因为主函数中的所有行都是在程序运行时执行的!做而循环用于循环。在这里阅读更多关于它的信息:http://en.wikipedia.org/wiki/Do_while_loop,但是当需要执行行而不检查任何条件时(就像在这种情况下),你不需要做 while 循环。
使用命名空间 std 后也不需要 "{";
正如有人评论的那样,我也强烈建议您阅读一本书来清除这些基础知识!
相关文章:
- Arduino无法编译名为SP:"Expected ')' before '(' token"的变量
- 为什么我的功能在使用 goto 时会给我带来"expected primary-expression before '}' token"?
- 使用结构和数组时的"expected primary expression before '.' token"
- 如何解决"Expected expression before " = " token"?
- 错误:创建模板类时"expected unqualified-id before '{' token"
- 如何解决"expected constructor, destructor, or type conversion before ‘(’ token"错误?
- 这个编译器错误是什么意思 - "qualified-id in declaration before ‘=’ token" C++?
- 获取链表上的"expected ‘)’ before ‘&’ token"到 BST 构造函数签名
- 使用宏访问指向数据成员的指针会导致"error: expected unqualified-id before ‘*’ token"
- "error: expected primary-expression before '<=' token" 我做错了什么?
- 内存集调用位置的"error: expected constructor, destructor, or type conversion before '(' token"
- 设置if流,获取'error: expected initializer before ‘.’ token'
- 在2D伊辛模型上工作。C++新手。我在第 23 行收到错误"expected unqualified-id before '{' token"
- 我的代码看起来不错。为什么我要得到"error: expected primary-expression before ')' token"?
- 如何修复"Expected Primary-expression before ')' token"错误?
- C++错误"tabulate.cc:19: error: expected primary-expression before ',' token"
- 使用多个文件时"array bound is not an integer constant before ']' token"
- "expected initializer before '<' token"尝试模板成员专用化
- 为什么海湾合作委员会说"a function-definition is not allowed here before '{' token"?
- C++:"error: expected ',' or '...' before '(' token"