我正在练习推和弹出。为此,我使用
emplace_back
函数将结构推入我的数组中。
这是我的代码:
#include <string>
#include <vector>
#include <iostream>
#include <string_view>
struct Person
{
std::string name{};
int age{};
};
int main()
{
std::vector<Person> nums{{"Daniel", 34},
{"Jose", 39},
{"Martin", 22}
};
nums.emplace_back("Malena", 30);
for (auto const& a : nums)
std::cout << a.name << " " << a.age << '\n';
return 0;
}
错误:
In file included from /Users/xxx/CLionProjects/test/main.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:504:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:175:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:57:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:643:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1881:31: error: no matching constructor for initialization of 'Person'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/memory:1773:18: note: in instantiation of function template specialization 'std::__1::allocator<Person>::construct<Person, char const (&)[7], int>' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
如有任何帮助,我们将不胜感激。