Search code examples
c++inlinedefault-constructorambiguous

(C++) Constructor, default parameters, "call of overloaded... ambigous"


i'm new to this site, after doing some research I could not find a problem similar to mine(some questions looked like mine but their code was different)

So basically what i'm trying to do is to representing the framebuffer matrix with all different colors values. I'm coding a class named "Point", and I have one constructor, using default arguments, here it is :

Point.h

#ifndef POINT_H
#define POINT_H
#include <iostream>



class Point
{
    protected:
        int x;
        int y;

    public:
        Point(int=0,int=0);
        Point(const &Point);
        void showC() const;
        static void showC(Point);
        virtual ~Point();


};

#endif // POINT_H

Point.cpp

#include "Point.h"

using namespace std;

Point::Point(int a,int b)
{
    x=a;
    y=b;
}

Point::~Point()
{}

void Point::showC() const
{ cout << x << " " << y << endl; }


void Point::showC(Point P)
{ cout << P.x << " " << P.y << endl; }

But the problem is when I try to compile the program

main.cpp

#include <iostream>
#include "Point.h"



using namespace std;

int main()
{
 Point P1;
 Point P2(2);
 Point P3(4,-7);
 cout << "Call of function member showC\n";
 P1.showC();
 P2.showC();
 P3.showC();
cout << "Call of static function showC\n";
Point::showC(P1);
Point::showC(P2);
Point::showC(P3);

    return 0;
}

There is an error when I create Point P2 :

  • "Call of overloaded 'Point(int)' is ambigous"

On all the others question i read, either it was not the same problem or they had a default constructor in addition to a constructor with default argument which cause ambiguity of which constructor to use if you create an object without argument.

On a book i'm reading to improve skills on c++, there is this sample that is working somehow, and that's why I don't really understand

Here is the sample :

main.cpp

class point
{
private :
int x;
int y;

Point (int abs=0, int ord=0) //inline constructor
 {x=abs; y=ord;}

bool coincide(point);

};

    bool point::coincide(point pt)
{ return ( (pt.x==x) && (pt.y==y) );
} 

int main()
{
point a, b(1), c(1,0);
cout << "a and b : " << a.coincide(b) << " ou " b.coincide(a) << "\n"
cout << "b et c : " << b.coincide(c) << " ou " << c.coincide(b) << "\n"
}

However he grouped everything in the main.cpp files, and his constructor is inline.

Can anyone explain to me why is the sample working, and why my program is not ? I guess there is a mechanism that i don't understand...

Thanks in advance

RE-EDIT : I copied all the code


Solution

  • I think you are mixing both python and c++ way of creating class

    python do use : class Point:

    for declaring in class , c++ uses {} like class Point {}; Below works by changing the class declaration.

    Just added a cout in your constructor

    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Point
    {
    private:
        int x;
        int y;
    
    
    public:
        Point(int=0,int=0);
    };
    
    Point::Point(int a, int b)
    {
        x = a;
        y = b;
        cout<<x<<y<<endl;
    }
    
    int main()
    {
        Point P1;
        Point P2(2);
        Point P3(4,-7);
        return 0;
    }
    

    Output

    00
    20
    4-7
    Program ended with exit code: 0
    

    After question edit Removed your buggy line and it works perfectly

    Point(const &Point);
    
    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Point
    {
    protected:
        int x;
        int y;
    
    public:
        Point(int=0,int=0);
        //Point(const &Point);
        void showC() const;
        static void showC(Point);
        virtual ~Point();
    
    
    };
    Point::Point(int a,int b)
    {
        x=a;
        y=b;
    }
    
    Point::~Point()
    {}
    
    void Point::showC() const
    { cout << x << " " << y << endl; }
    
    
    void Point::showC(Point P)
    { cout << P.x << " " << P.y << endl; }
    
    int main()
    {
        Point P1;
        Point P2(2);
        Point P3(4,-7);
        cout << "Call of function member showC\n";
        P1.showC();
        P2.showC();
        P3.showC();
        cout << "Call of static function showC\n";
        Point::showC(P1);
        Point::showC(P2);
        Point::showC(P3);
    
        return 0;
    }
    

    Output

    Call of function member showC
    0 0
    2 0
    4 -7
    Call of static function showC
    0 0
    2 0
    4 -7
    Program ended with exit code: 0
    

    After edit I guess you want to use copy constructor just change it to

      Point(const Point &p2) {x = p2.x; y = p2.y; }