0

我正在尝试在以下位置运行这个小程序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 ==========

我还收到以下错误消息:

在此处输入图像描述

编辑(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 ==========

除了同样的错误信息框。

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

谢谢。

4

4 回答 4

1

奇怪的 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];
}
}

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

您还需要:

#include <vector>

并替换vectorstd::vector.

于 2012-06-23T15:39:08.353 回答
0

我可能不知道答案,但这对我有用。

#include <iostream>
#include <vector>

int main(){
    std::vector<bool> features(4,false);
    for(std::vector<bool>::iterator i = features.begin(); i != features.end(); i++){
        std::cout<< (*i);
    }
}
于 2012-06-23T16:06:08.333 回答
0

尝试这样的事情:

#include "stdafx.h"

#include <vector>
#include <iostream>

using namespace std;

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

在 VS2010 下,您可以在项目属性 C/C++ 小节、预编译器小节下关闭预编译头文件。

于 2012-06-23T16:10:41.983 回答
0

我遇到了同样的问题。我用了

#include <vector> 

std::vector,而不是vector,但问题仍然存在。后来,我尝试添加以下行:

using namespace std;

所有的错误都消失了!

我想知道为什么?最后,我发现当我定义一个二维向量时,我使用了: ,如果没有指定命名空间std::vector<vector<double>> myVector;,应该是: std::vector<std::vector<double>> myvector;std

于 2014-03-15T17:52:58.477 回答