Question

I trying to fix my vector not able to push_back problem but i get this error:

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

here is my code

Class

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);
    ;
};

Line maybe cause error:

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

and this also not working:

std::vector<Point> temp;

Please help

here is more error message hope this help:

/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: In instantiation of member function 'std::__1::vector >::__construct_at_end' requested here

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Source/Week3_T.cpp:466:19: In instantiation of member function 'std::__1::vector >::vector' requested here

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Headers/Week3_T.h:15:5: Candidate constructor not viable: requires 5 arguments, but 0 were provided

/Users/sum/Documents/3407ICT_Starter_Kit_v8/ProjectOSX/Headers/Week3_T.h:24:5: Candidate constructor not viable: requires single argument 'point', but no arguments were provided

Was it helpful?

Solution

In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly. vector<Point> temp(10) initializes the vector with 10 elements, which are filled with the default-constructed value of your type. But when you have defined this constructor:

Point(int x, int y, Uint8 r, Uint8 g, Uint8 b) : x(x), y(y), r(r), g(g), b(b)
the compiler will not implicitly define a default constructor, so no constructor is available now. You can fix the compiler error by defining another constructor:

Point(){}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top