こちらは問題なく動きます
c++
1#include <iostream> 2#include <list> 3#include <algorithm> 4 5int main() 6{ 7 auto numbers = std::list<int>(); 8 numbers.push_back(1); 9 numbers.push_back(2); 10 numbers.push_back(3); 11 12 std::for_each(numbers.cbegin(), numbers.cend(), [](int x) 13 { 14 std::cout << x << std::endl; 15 }); 16}
こちらがエラーが出ます。
どのようにすればstd::listでクラスが使えますでしょうか?
エラー C2664 'void std::list<Human,std::allocator<_Ty>>::push_back(const _Ty &)': 引数 1 を 'Human *' から 'Human &&' へ変換できません。
c++
1#include <iostream> 2#include <list> 3#include <algorithm> 4 5class Human 6{ 7public: 8 std::string Name; 9 int Age; 10 11 Human(std::string name, int age) 12 { 13 this->Name = name; 14 this->Age = age; 15 } 16}; 17 18int main() 19{ 20 auto humans = std::list<Human>(); 21 humans.push_back(new Human("name001", 10)); 22 23 std::for_each(humans.cbegin(), humans.cend(), [](Human x) 24 { 25 std::cout << x.Name.c_str() << x.Age << std::endl; 26 }); 27}
補足情報(FW/ツールのバージョンなど)
- Visual Studio Community 2017
- Windows SDK Version: 10.0.16299.0
- Platform Toolset: Visual Studio 2017 (v141)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/11 11:16