简体   繁体   中英

error C2440: '=': cannot convert from 'std::list<std::string,std::allocator<_Ty>> *'to 'std::string*'

I'm currently learning how to use lists in C++ (and C++ in general) by entering string values into a list, in a do-while loop, and then have them printed from the list in a for loop, but have run into an error:

error C2440: '=': cannot convert from 'std::list<std::string,std::allocator<_Ty>> *'
                                   to 'std::string*'

The error refers to this particular line:

output = &container[k];

I don't know how to fix this, or what I'm doing wrong. Nor do I know whether it's a problem with my understanding of lists, or whether I'm using pointers incorrectly.

I would appreciate it if any answers could be worded as simply as possible, thanks.

The rest of the code:

#include <iostream>
#include <string>
#include <list>
using namespace std;

void main()
{

int i = 0;
list<string> container[10];
string input, *output;  

do{
    cout << "enter a value for container location " << i << endl;
    cin >> input;
    container[i].push_back(input);
    i++;
}while (i < 10);

for (int j = 0, k = 0; j < 10; j++)
{
    output = &container[k];
    cout << "Value of container location " << j << " = " << *output << endl;
    k++;
}
}

根据您的代码,您的container类型显然是vector<string>而不是[10]或简单地是string (同时保持数组规范)。

Pignaut, You are declaring an array (of size 10) of list of String, hence, each element of the array is a list of string. That's why this error. But, I think you want to declare a list of string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM