Does anyone know what header file is missing?

时间:2016-08-30 04:35:03

标签: c++ visual-studio-2015

This code works on Code Blocks but not on Visual Studio:

// A simple program that prints string test1:
#include <iostream>
#include <vector>

using namespace std;

int main()

{
    vector<string> test1 = { "pooping","reading" };


    for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
    {
        cout << *iter << endl;
    }
    system("pause");
}

The error that Visual Studio Outputs:

Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>' (or there is no acceptable conversion)   

3 个答案:

答案 0 :(得分:1)

std::basic_string<string>标题中正式定义。请参阅cppreference

答案 1 :(得分:0)

#include<string>

类模板basic_string存储和操作类似char的对象序列。该类既不依赖于字符类型,也不依赖于该类型的操作性质。操作的定义通过Traits模板参数提供 - std :: char_traits或兼容traits类的特化。有关详细信息,请参阅 std::basic_string

答案 2 :(得分:-1)

添加标题后,程序不会编译,它会抛出错误

"error: in C++98 'test1' must be initialized by constructor, not by '{...}'"

检查以下程序,然后按另一种方法初始化vector。

正如其他人建议你需要添加

      #include<string>

//打印字符串test1的简单程序:

  #include <iostream>
  #include <vector>
 #include<string>

 using namespace std;

 int main()

 {
 static const string arr[] = {"pooping","reading"};
vector<string> test1 (arr, arr + sizeof(arr) / sizeof(arr[0]) );

for (vector<string>::iterator iter = test1.begin(); iter != test1.end(); iter++)
{
    cout << *iter << endl;
}
system("pause");
 }