list.push_back()不工作:引发错误C++
#include <iostream>
#include <list>
using namespace std;
string song = "CDE";
int songlength = song.length();
int counter = 0;
int main() {
cout << songlength << endl;
for (int i = 0; i < songlength + 1; i++, counter++) {
list<string> songlist;
songlist.push_back(song[counter]);
if (counter <= songlength) {
}
}
}
我是初学者,对这种编程语言了解不多。
错误:
error: no matching function for call to 'std::__cxx11::list<std::__cxx11::basic_string<char> >::push_back(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)'
#include <iostream>
#include <list>
using namespace std;
string song = "CDE";
int songlength = song.length();
int counter = 0;
int main() {
cout << songlength << endl;
for (int i = 0; i < songlength + 1; i++, counter++) {
list<string> songlist;
songlist.push_back(song[counter]);
if (counter <= songlength) {
}
}
}
I am a beginner and I do not know much about this programming language.
Error:
error: no matching function for call to 'std::__cxx11::list<std::__cxx11::basic_string<char> >::push_back(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类
std :: String
没有具有类型char
的第一个参数的构造函数。您要么需要声明列表,要么
像这样,
但在最后一个情况下,您需要调用方法
push_back
喜欢使用构造函数
,也需要在for循环之前将列表的声明放置。否则,在循环中,列表将在循环的每次迭代中重新创建。
例如
The class
std::string
does not have a constructor with the first parameter of the typechar
.Either you need to declare the list like
or like
But in the last case you need to call the method
push_back
likeusing the constructor
Also you need to place the declaration of the list before the for loop. Otherwise within the loop the list is created anew in each iteration of the loop.
For example