【问题标题】:(C++) Constructor, default parameters, "call of overloaded... ambigous"(C++)构造函数,默认参数,“重载调用......模棱两可”
【发布时间】:2017-08-05 23:13:48
【问题描述】:

我是这个网站的新手,在做了一些研究后我找不到与我相似的问题(有些问题看起来像我的,但它们的代码不同)

所以基本上我要做的是用所有不同的颜色值来表示帧缓冲矩阵。我正在编写一个名为“Point”的类,我有一个构造函数,使用默认参数,这里是:

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

但问题是当我尝试编译程序时

ma​​in.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;
}

创建点 P2 时出现错误:

  • “重载 'Point(int)' 的调用不明确”

在我阅读的所有其他问题上,要么不是同一个问题,要么除了具有默认参数的构造函数之外,它们还有一个默认构造函数,如果您创建没有参数的对象,则会导致使用哪个构造函数的歧义。

在我正在阅读以提高 c++ 技能的一本书中,有一个示例以某种方式起作用,这就是我不太了解的原因

这里是示例:

ma​​in.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"
}

但是他把所有东西都归入了 main.cpp 文件,而且他的构造函数是内联的。

谁能向我解释为什么该示例有效,为什么我的程序无效?我想有一个我不明白的机制......

提前致谢

重新编辑:我复制了所有代码

【问题讨论】:

  • 请阅读发布代码的指南,您确实需要发布准确和完整的代码
  • 无法重现,您没有显示您的确切代码:ideone.com/cReVEs。另外你的也没有编译,因为Class Point: 不是有效的 C++。
  • 我正在编辑它并给出完整的代码
  • 从错误消息看来,您也有一个 Point(int) 构造函数。
  • 还有问题 Point(const &Point);是什么类型的?

标签: c++ inline default-constructor ambiguous


【解决方案1】:

我认为您正在混合使用 python 和 c++ 创建类的方式

python 使用: class Point:

为了在类中声明,c++ 使用 {} 就像 class Point {}; 下面通过更改类声明来工作。

刚刚在你的构造函数中添加了一个 cout

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

输出

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

问题编辑后 删除了你的错误线,它完美地工作

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

输出

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

编辑后我猜你想使用复制构造函数只需将其更改为

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

【讨论】:

  • 实际上是我没有正确复制,在我的代码中 Class 点之后没有“:”。我编辑并给出了完整的代码。但看起来问题发生在我编译单独的文件时
  • 确实,我会突出显示您的答案,我在复制构造函数中反转了“&”运算符的位置!问题解决了 !感谢您的帮助,你们所有人都非常快:')!下次发帖前我会注意的,我以为一切都是正确的,但我犯了很多错误
  • 我想你想使用复制构造函数 Point(const Point &p2) {x = p2.x; y = p2.y; }
  • 没错,但我不知何故写了 Point(const &Point) 而不是 Point(const Point&)。多么可怕的错误......这是最基本的事情之一,但我仍然做得不好......
猜你喜欢
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2016-03-01
  • 1970-01-01
相关资源
最近更新 更多