2015-05-16 48 views
2

在C++中练习成员赋值,您可以将一个对象的值设置为同一个类的另一个对象。该程序的想法是用一些值初始化一个矩形对象,并创建另一个矩形对象,但将第一个值赋给第二个。C++错误:没有匹配的构造函数用于初始化

它给我一个错误,这是贴在下面,我无法弄清楚它是什么,它的驾驶我坚果笑

这是我Rectangle.h

#ifndef RECTANGLE_H 
#define RECTANGLE_H 

class Rectangle { 
    private: 
     double length; 
     double width; 
    public: 
     Rectangle(double, double); 
     double getLength() const; 
     double getWidth() const; 
}; 

Rectangle::Rectangle(double l, double w) { 
    length = l; 
    width = w; 
} 

double Rectangle::getWidth() const { return width; } 
double Rectangle::getLength() const { return length; } 

#endif 

这是我的Rectangle.cpp

#include <iostream> 
#include "rectangle.h" 
using namespace std; 

int main() 
{ 
    Rectangle box1(10.0, 10.0); 
    Rectangle box2; 

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl; 
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl; 

    box2 = box1; 

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl; 
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl; 

    return 0; 
} 

这是编译时的错误。

skipper~/Desktop/Programming/Memberwise: g++ rectangle.cpp 
rectangle.cpp:7:12: error: no matching constructor for initialization of 
     'Rectangle' 
     Rectangle box1(10.0, 10.0); 
       ^ ~~~~~~~~~~ 
./rectangle.h:4:7: note: candidate constructor (the implicit copy constructor) 
     not viable: requires 1 argument, but 2 were provided 
class Rectangle { 
^
./rectangle.h:4:7: note: candidate constructor 
     (the implicit default constructor) not viable: requires 0 arguments, but 2 
     were provided 
1 error generated. 

编辑:这是我如何能够使它工作。我将所有东西都移动到了rectangle.cpp中,并给出了构造函数的默认参数。

EDITED rectangle.cpp

#include <iostream> 
using namespace std; 

class Rectangle { 
    private: 
     double length; 
     double width; 
    public: 
     //Rectangle(); 
     Rectangle(double = 0.0, double = 0.0); 
     double getLength() const; 
     double getWidth() const; 
}; 

int main() 
{ 
    Rectangle box1(10.0, 10.0); 
    Rectangle box2; 

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl; 
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl; 

    box2 = box1; 

    cout << "box1's width and length: " << box1.getWidth() << ", " << box1.getLength() << endl; 
    cout << "box2's width and length: " << box2.getWidth() << ", " << box2.getLength() << endl; 

    return 0; 
} 

Rectangle::Rectangle(double l, double w) { 
    length = l; 
    width = w; 
} 

double Rectangle::getWidth() const { return width; } 
double Rectangle::getLength() const { return length; } 

只有我做出了让默认参数到我的用户定义构造函数的变化。但是,如果更改位于rectangle.h中,则无法工作。但是,当我将类和成员函数定义移动到rectangle.cpp时,它能够工作。所以,我得到了该程序的工作,但我没有解决真正的问题,这是当类和成员函数定义在rectangle.h中,它不会编译。

如果有人遇到此问题并找到了解决办法,请告诉我您是如何做到的。谢谢:)

回答

2

在行

Rectangle box2; // no default constructor, error 

你试图调用的Rectangle默认构造函数。编译器不再生成这样的默认构造函数,因为您的Rectangle具有一个用户定义的构造函数,它具有2个参数。因此,你需要指定的参数,如

Rectangle box2(0,10); 

编译代码时,我得到的错误是:

Rectangle.cpp:8:15: error: no matching function for call to 'Rectangle::Rectangle()' Rectangle box2;

一个解决方案是创建一个默认的构造函数为Rectangle,因为它是不会自动由于用户不再生成定义之一:

Rectangle(); // in Rectangle.h 

Rectangle::Rectangle(){} // in Rectangle.cpp (or Rectangle::Rectangle() = default; in C++11) 

另一种解决方案(以及优选的一个,因为它不保留数据未初始化)是为将默认参数标记到现有的构造函数中。

Rectangle::Rectangle(double l = 0, double w = 0); // only in Rectangle.h 

通过这种方式,您可以使您的类成为Default-Constructible。

+0

因此,据我所知,无论何时我创建一个用户定义的构造函数,我还必须创建一个默认构造函数。在C++中总是这样吗? – skipper

+0

@skipper是的。但是,如果您提供默认参数,则您的用户定义构造函数也可以是默认构造函数,因此不需要提供2个不同的构造函数。默认构造函数是允许不带参数调用的任何构造函数。有关更多详细信息,请参阅[此处](http://en.cppreference.com/w/cpp/language/default_constructor)。 – vsoftco

0

编译器生成的默认构造函数只有在没有定义的构造函数时才会生成。你定义一个构造函数,所以如果你想要一个默认构造函数,你必须自己提供它。也许最简单的(可以说)是在你的两个参数的构造函数使用默认参数为它提供:

Rectangle(double l=0, double w=0) 

而且你应该使用inline关键字如下图所示,或者你可能会发现你会得到链接错误:

inline Rectangle::Rectangle(double l, double w) { 
    length = l; 
    width = w; 
} 

inline double Rectangle::getWidth() const { return width; } 
inline double Rectangle::getLength() const { return length; } 
+0

不应该有一个默认的构造函数没有参数? – skipper

+0

@skipper:应该调用默认构造函数而不带任何参数。所有缺省参数都满足这个要求。 – Steve

+0

我无法通过在rectangle.h中添加默认参数来使其工作。但是,我将类和成员函数定义移到了rectangle.cpp中,并且它使用默认参数正常工作。因此,它是一个解决方案,但不是一个真正令人满意的解决方案,因为我将来可能面临同样的问题,而且没有解决方案。 – skipper

相关问题