mingw 编译器错误

发布于 2024-12-10 17:59:51 字数 1231 浏览 0 评论 0原文

第一次尝试在不使用 Visual Studio 的情况下编译一些代码。安装mingw并设置环境路径变量。然后尝试编译此代码:

#include <iostream>
#include <vector>

int main() {
typedef std::vector<int> Container;
typedef std::vector<int>size_type size;
typedef std::vector<int>iterator iter;

Container container; 

for (size i = 0; i != 1000000; ++i) {
container.push_back(i);
}

for (iter i = container.begin(); i != container.end(); ++i) {
std::cout << *i << " " << std::endl;
}

system("PAUSE");
return 0;
}

通过打开 cmd shell,转到单个源文件的目录并键入:

g++ main.cpp

但我收到很多错误消息,如下所示:

main.cpp: In function 'int main()':
main.cpp:6:35: error: expected initializer before 'size'
main.cpp:7:34: error: expected initializer before 'iter'
main.cpp:11:6: error: 'size' was not declared in this scope
main.cpp:11:11: error: expected ';' before 'i'
main.cpp:11:18: error: 'i' was not declared in this scope
main.cpp:15:6: error: 'iter' was not declared in this scope
main.cpp:15:11: error: expected ';' before 'i'
main.cpp:15:34: error: 'i' was not declared in this scope
main.cpp:19:15: error: 'system' was not declared in this scope

我在这里遗漏了一些非常明显的东西吗?谢谢。

First attempt at compiling some code without using Visual Studio. Installed mingw and set the environment path variable. Then tried to compile this code:

#include <iostream>
#include <vector>

int main() {
typedef std::vector<int> Container;
typedef std::vector<int>size_type size;
typedef std::vector<int>iterator iter;

Container container; 

for (size i = 0; i != 1000000; ++i) {
container.push_back(i);
}

for (iter i = container.begin(); i != container.end(); ++i) {
std::cout << *i << " " << std::endl;
}

system("PAUSE");
return 0;
}

By opening up a cmd shell, going to directory of single source file and typing:

g++ main.cpp

But i get a lot of error messages as follows:

main.cpp: In function 'int main()':
main.cpp:6:35: error: expected initializer before 'size'
main.cpp:7:34: error: expected initializer before 'iter'
main.cpp:11:6: error: 'size' was not declared in this scope
main.cpp:11:11: error: expected ';' before 'i'
main.cpp:11:18: error: 'i' was not declared in this scope
main.cpp:15:6: error: 'iter' was not declared in this scope
main.cpp:15:11: error: expected ';' before 'i'
main.cpp:15:34: error: 'i' was not declared in this scope
main.cpp:19:15: error: 'system' was not declared in this scope

Am i missing something painfully obvious here? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幽蝶幻影 2024-12-17 17:59:51
typedef std::vector<int>size_type size;
typedef std::vector<int>iterator iter;

把它变成:

typedef std::vector<int>::size_type size;
typedef std::vector<int>::iterator iter;
typedef std::vector<int>size_type size;
typedef std::vector<int>iterator iter;

Turn it into:

typedef std::vector<int>::size_type size;
typedef std::vector<int>::iterator iter;
木槿暧夏七纪年 2024-12-17 17:59:51

您在向量定义和 size_type (及其后面的迭代器)之间缺少 :: 运算符。

You are missing :: operator between the vector definition and size_type (and iterator after it).

假面具 2024-12-17 17:59:51

typedef std::vector::size_type 应该是 typedef std::vector::size_type size; 等。

typedef std::vector<int>::size_type should be typedef std::vector<int>::size_type size; etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文