gpt4 book ai didi

c++ - 在之前使用 emplace 而不是构造对象

转载 作者:行者123 更新时间:2023-11-30 01:52:15 25 4
gpt4 key购买 nike

#include <iostream>
#include <queue>
#include <iomanip>

using namespace std;

struct Time {
int h;
int m;
int s;
};

class CompareTime {
public:
bool operator() (Time& t1, Time& t2) {
if (t1.h < t2.h) return true;
if (t1.h == t2.h && t1.m < t2.m) return true;
if (t1.h == t2.h && t1.m == t2.m && t1.s < t2.s) return true;
return false;
}
};

int main() {
priority_queue<Time, vector<Time>, CompareTime> pq;
pq.emplace(3,2,40);
pq.emplace(3,2,26);
pq.emplace(5,16,13);
pq.emplace(5,14,20);

while(!pq.empty()) {
Time t2 = pq.top();
cout << setw(4) << t2.h << " " << setw(3) << t2.m << " " << setw(3) <<
t2.s << endl;
pq.pop();
}
return 0;

}

我之前尝试使用 emplace 而不是 construct object。但出现如下错误:

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h: In member function âvoid __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, _Args&& ...) [with _Args = int, int, int, _Tp = Time]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:95: instantiated from âvoid std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Alloc = std::allocator<Time>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_queue.h:527: instantiated from âvoid std::priority_queue<_Tp, _Sequence, _Compare>::emplace(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Sequence = std::vector<Time, std::allocator<Time> >, _Compare = CompareTime]â
time_queue_test.cpp:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:111: error: new initializer expression list treated as compound expression
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:111: error: no matching function for call to âTime::Time(int)â
time_queue_test.cpp:7: note: candidates are: Time::Time()
time_queue_test.cpp:7: note: Time::Time(const Time&)
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/vector:69,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/queue:62,
from time_queue_test.cpp:2:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc: In member function âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = int, int, int, _Tp = Time, _Alloc = std::allocator<Time>]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:100: instantiated from âvoid std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Alloc = std::allocator<Time>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_queue.h:527: instantiated from âvoid std::priority_queue<_Tp, _Sequence, _Compare>::emplace(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Sequence = std::vector<Time, std::allocator<Time> >, _Compare = CompareTime]â
time_queue_test.cpp:25: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:314: error: no matching function for call to âTime::Time(int, int, int)â
time_queue_test.cpp:7: note: candidates are: Time::Time()
time_queue_test.cpp:7: note: Time::Time(const Time&)

不知道我哪里做错了。

谢谢

最佳答案

接收3个int并初始化成员的构造函数,不是自动生成的,需要实现。

struct Time {
int h;
int m;
int s;

Time(int hv, int mv, int sv)
: h(hv), m(mv), s(sv)
{}
};

关于c++ - 在之前使用 emplace 而不是构造对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com