创建我的第一个向量 [Visual Studio 2013] 时出错

Error with the creating of my first vector [Visual studio 2013]

本文关键字:2013 出错 Studio Visual 我的 第一个 向量 创建      更新时间:2023-10-16

我正在学习C++编程语言,我的第一个向量遇到了一些问题。如果我按照书中的例子(编程原理和使用C++的实践),这就是一个例子:

vector<int> v = { 5, 7, 9, 4, 6, 8 }; 

我的编译器在这里说: 错误 1 错误 C2440: "初始化" : 无法从"初始值设定项列表"转换为"矢量" c:\users\pierob\documents\visual studio 2013\projects\learnprogramming\learnprogramming\main.cpp 7 1 learnprogramming

你能帮我吗?我有Visual Studio 2013 express(我使用Visual C++编译器2013年11月CTP)。

错误消息指出Vector,而不是vector,因此您实际正在编译的代码看起来很可疑。

使用 Visual Studio 2013 Update 3,以下编译包含 0 个错误:

#include <vector>
int main()
{
    std::vector<int> v = { 5, 7, 9, 4, 6, 8 };
}

输出:

1>  main.cpp
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

请复制上面的代码并编译它以确保它没有错误。

在 std_lib_facilities.h 头文件的第 99 行(对我来说)是一个显然令人作呕的宏黑客,用于获取一个范围检查的向量,上面写着:

#define vector Vector

我将其更改为:

#define vector vector

和您的代码:

vector<int> v = { 5, 7, 9, 4, 6, 8 };

似乎按预期工作。