gpt4 book ai didi

c++ - 'type' 的初始化没有匹配的构造函数

转载 作者:太空狗 更新时间:2023-10-29 21:00:04 24 4
gpt4 key购买 nike

我试图修复我的 vector not able to push_back 问题,但我得到了这个错误:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/memory:1456:36: No matching constructor for initialization of 'Point'

这是我的代码

class Point
{
public:
int x;
int y;
Uint8 r;
Uint8 g;
Uint8 b;
Point(int x, int y, Uint8 r, Uint8 g, Uint8 b) : x(x), y(y), r(r), g(g), b(b) {}
Point& operator=(Point const &np){
x=np.x;
y=np.y;
r=np.r;
g=np.g;
b=np.b;
return *this;
}
Point(const Point& point);
;
};

行可能导致错误:

std::vector<Point> temp(10);

这也不起作用:

std::vector<Point> temp;

请帮忙

这里有更多的错误信息希望对你有帮助:

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Source/Week3_T.cpp:8:10: In file included from /Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Source/Week3_T.cpp:8:

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Headers/Week3_T.h:3:10: In file included from /Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Headers/Week3_T.h:3:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/vector:265:10: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/vector:265:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/__bit_reference:15:10: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__bit_reference:15:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/algorithm:627:10: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:627:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/vector:925:25: In instantiation of function template specialization 'std::__1::allocator_traits

::construct' requested here

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/vector:1028:9: 在此处请求的成员函数 'std::__1::vector >::__construct_at_end' 的实例化中

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Source/Week3_T.cpp:466:19: 在此处请求的成员函数 'std::__1::vector >::vector' 的实例化中

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Headers/Week3_T.h:15:5: 候选构造函数不可行:需要 5 个参数,但 0 个是 提供

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Headers/Week3_T.h:24:5: 候选构造函数不可行:需要单个参数“point”, 但没有提供参数

最佳答案

在标准库中,某些容器在未显式给出值时使用默认构造函数“填充”值。 vector<Point> temp(10)用 10 个元素初始化 vector ,这些元素填充了您的类型的默认构造值。但是当你定义了这个构造函数时:

Point(int x, int y, Uint8 r, Uint8 g, Uint8 b) : x(x), y(y), r(r), g(g), b(b)
编译器不会隐式定义默认构造函数,因此现在没有可用的构造函数。您可以通过定义另一个构造函数来修复编译器错误:

Point(){}

关于c++ - 'type' 的初始化没有匹配的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127479/

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