C++ vector 错误(Visual C++ 2008 Express Edition)

标签 c++ vector compiler-errors

我正在尝试在 Visual C++ 2008 Express Edition 中运行这个小程序:

#include <vector>
int main(){
vector<bool> features(4,false);
for(vector<bool>::iterator i = features.begin(); i != features.end(); i++){
cout<<features[i];
}
}

当我这样做时,我得到以下信息:

   1>------ Build started: Project: SFS, Configuration: Debug Win32 ------
1>Compiling...
1>SFS.cpp
1>.\SFS.cpp(1) : warning C4627: '#include <vector>': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
1>.\SFS.cpp(14) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Users\Ola\Documents\Visual Studio 2008\Projects\SFS\SFS\Debug\BuildLog.htm"
1>SFS - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我还收到以下错误消息:

enter image description here

编辑(1)

在我添加 #include "stdafx.h" 之后,我得到以下信息:

1>------ Build started: Project: SFS, Configuration: Debug Win32 ------
1>Compiling...
1>SFS.cpp
1>.\SFS.cpp(5) : error C2065: 'vector' : undeclared identifier
1>.\SFS.cpp(5) : error C2062: type 'bool' unexpected
1>.\SFS.cpp(6) : error C2065: 'vector' : undeclared identifier
1>.\SFS.cpp(6) : error C2062: type 'bool' unexpected
1>.\SFS.cpp(6) : error C2039: 'iterator' : is not a member of '`global namespace''
1>.\SFS.cpp(6) : error C2065: 'i' : undeclared identifier
1>.\SFS.cpp(6) : error C2065: 'features' : undeclared identifier
1>.\SFS.cpp(6) : error C2228: left of '.end' must have class/struct/union
1>        type is ''unknown-type''
1>.\SFS.cpp(6) : error C2065: 'i' : undeclared identifier
1>.\SFS.cpp(6) : error C2143: syntax error : missing ';' before ')'
1>.\SFS.cpp(6) : error C2143: syntax error : missing ';' before ')'
1>.\SFS.cpp(6) : error C2143: syntax error : missing ';' before '{'
1>.\SFS.cpp(7) : error C2065: 'cout' : undeclared identifier
1>.\SFS.cpp(7) : error C2065: 'features' : undeclared identifier
1>.\SFS.cpp(7) : error C2065: 'i' : undeclared identifier
1>Build log was saved at "file://c:\Users\Ola\Documents\Visual Studio 2008\Projects\SFS\SFS\Debug\BuildLog.htm"
1>SFS - 15 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

除了同样的错误提示框。

编辑(2)

在我添加 #include<iostream> 之后并使用 std::cout ,我得到以下信息:

1>------ Build started: Project: SFS, Configuration: Debug Win32 ------
1>Compiling...
1>SFS.cpp
1>.\SFS.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
1>.\SFS.cpp(2) : warning C4627: '#include <vector>': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
1>.\SFS.cpp(16) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Users\Ola\Documents\Visual Studio 2008\Projects\SFS\SFS\Debug\BuildLog.htm"
1>SFS - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

除了同样的错误提示框。

这是为什么呢?我该如何解决这个问题?

谢谢。

最佳答案

奇怪的 MSVS 东西。您包含的第一个文件是 "stdafx.h" 吗?应该是。

此外,进行干净构建(重建或清理/构建)。

#include "stdafx.h"
#include <vector>
int main(){
vector<bool> features(4,false);
for(vector<bool>::iterator i = features.begin(); i != features.end(); i++){
cout<<features[i];
}
}

或者您可以在项目属性中关闭使用预编译 header 构建的选项。

您还需要:

#include <vector>

并将vector替换为std::vector

关于C++ vector 错误(Visual C++ 2008 Express Edition),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11170750/

相关文章:

c++ - python 中等效的 C++ 嵌套打印

c++ - jvm如何处理多态及其优化

C++ - 结构指针数组

c++ - 如何让用户设置 2D Vector 的行大小和列大小?

Angular 7 - 导入 json 文件 - 编译错误

c++ - 循环遍历 C++ 中的所有 Lua 全局变量

c++ - 从 2D vector 中的每一行添加最大元素,使得行元素 $A_i$ 小于 $A_(i+1)$

C++:从对象指针 vector 中删除元素而不删除它指向的对象的正确方法是什么?

java - 为什么Java泛型不能用于静态方法?

c++ - OpenCV 帮助 - 错误 : no match for ‘operator[]’ (operand types are ‘cv::Mat’ and ‘int’ )