首次尝试在不使用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
我错过了一些非常明显的东西吗?感谢。
答案 0 :(得分:3)
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;
答案 1 :(得分:1)
你在vector定义和size_type之间缺少::运算符(以及它之后的迭代器)。
答案 2 :(得分:0)
typedef std::vector<int>::size_type
应为typedef std::vector<int>::size_type size;
等。