【发布时间】: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; }
但问题是当我尝试编译程序时
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;
}
创建点 P2 时出现错误:
- “重载 'Point(int)' 的调用不明确”
在我阅读的所有其他问题上,要么不是同一个问题,要么除了具有默认参数的构造函数之外,它们还有一个默认构造函数,如果您创建没有参数的对象,则会导致使用哪个构造函数的歧义。
在我正在阅读以提高 c++ 技能的一本书中,有一个示例以某种方式起作用,这就是我不太了解的原因
这里是示例:
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"
}
但是他把所有东西都归入了 main.cpp 文件,而且他的构造函数是内联的。
谁能向我解释为什么该示例有效,为什么我的程序无效?我想有一个我不明白的机制......
提前致谢
重新编辑:我复制了所有代码
【问题讨论】:
-
请阅读发布代码的指南,您确实需要发布准确和完整的代码
-
无法重现,您没有显示您的确切代码:ideone.com/cReVEs。另外你的也没有编译,因为
Class Point:不是有效的 C++。 -
我正在编辑它并给出完整的代码
-
从错误消息看来,您也有一个
Point(int)构造函数。 -
还有问题 Point(const &Point);是什么类型的?
标签: c++ inline default-constructor ambiguous