2015-05-16 46 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

相關問題