![](/img/trans.png)
[英]error C2440: 'initializing': cannot convert from 'initializer list' to 'std::vector<char *,std::allocator<_Ty>>'
[英]error C2440: '=': cannot convert from 'std::list<std::string,std::allocator<_Ty>> *'to 'std::string*'
我目前正在通過在do-while循環中將字符串值輸入到列表中,然后在for循環中將其從列表中打印出來,來學習如何在C ++(通常是C ++)中使用列表。錯誤:
error C2440: '=': cannot convert from 'std::list<std::string,std::allocator<_Ty>> *'
to 'std::string*'
該錯誤涉及以下特定行:
output = &container[k];
我不知道如何解決這個問題,或者我做錯了什么。 我也不知道我對列表的理解是否有問題,或者我使用的指針是否不正確。
如果可以用盡可能簡單的措詞表達我的感激,謝謝。
其余代碼:
#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,您要聲明一個字符串列表的數組(大小為10),因此,數組的每個元素都是一個字符串列表。 這就是為什么這個錯誤。 但是,我認為您想聲明一個字符串列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.