[英]C++ Error: 'no matching function for call to [class]::[function]'
[英]C++ class error message: no matching function for call
我的程序應該計算盒子的表面積和體積。 我要具有以下功能:setHeight,setWidth,setLength,getVolume,getSurfaceArea。
編譯程序時,出現以下錯誤消息:
boxMain.cpp: In function ‘int main()’:
boxMain.cpp:21: error: no matching function for call to 'Box::getVolume(double&, double&, double&)’
Box.hpp:20: note: candidates are: double Box::getVolume()
boxMain.cpp:23: error: no matching function for call to ‘Box::getSurfaceArea(double&, double&, double&)’
Box.hpp:21: note: candidates are: double Box::getSurfaceArea()
根據我在該站點上的搜索,大多數答案似乎都暗示默認構造函數不會退出或無法正常工作。 我也不知道這是否也是我的問題,但是我根據課本為我的默認構造函數編寫了代碼,因此我很難弄清楚自己做錯了什么。
我下面的代碼中缺少的行僅僅是我刪除的描述和注釋。
任何幫助,將不勝感激! 提前謝謝大家。
這是我的.hpp文件:
#include <iostream>
#ifndef Box_HPP
#define BOX_HPP
class Box {
private:
double h, w, l;
double height, width, length;
public:
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double getVolume();
double getSurfaceArea();
Box();
};
這是我的功能.cpp文件:
#include <iostream>
#include <iomanip>
#include "Box.hpp"
double height;
double width;
double length;
Box::Box() {
height = 1.0;
width = 1.0;
length = 1.0;
}
void setHeight(double h) {
if(h < 0) {
std::cout << "Error. Height must be positive."
<< std::endl;
}
else {
height = h;
}
}
void setWidth(double w) {
if(w < 0) {
std:: cout << "Error. Width must be positive."
<< std::endl;
}
else {
width = w;
}
}
void setLength(double l) {
if(l < 0) {
std::cout << "Error. Length must be positive."
<< std::endl;
}
else {
length = l;
}
}
double getVolume() {
return (height * width * length);
}
double getSurfaceArea() {
return (2.0 * length * height + 2.0 * width * height);
}
這是我的主要.cpp文件:
#include <iostream>
#include <fstream>
#include "Box.hpp"
int main() {
Box box1;
double h, w, l;
std::cout << "Enter height" << std::endl;
std::cin >> h;
std::cout << "Enter width" << std::endl;
std::cin >> w;
std::cout << "Enter length" << std::endl;
std::cin >> l;
void setHeight(double h);
void setWidth (double w);
void setLength (double l);
std::cout << "volume: "
<< box1.getVolume(h, w, l) << std::endl;
std::cout << "surface: "
<< box1.getSurfaceArea(h, w, l) << std::endl;
return 0;
}
Box::getVolume
被聲明為不帶任何參數,但是在第21行中,您使用3個參數對其進行了調用:
box1.getVolume(h, w, l)
只需使用box1.getVolume()
。 與Box::getSurface()
類似。
另外,在所有成員函數定義前使用Box::
,例如void Box::setHeight(double h) {...}
,否則最終將定義獨立的函數,並且由於成員函數結束而導致鏈接器錯誤沒有定義。
vsoftco的答案基本上是正確的,所以我對他投了贊成票,盡管我覺得最好在答案中添加一些信息,以更好地說明問題,並幫助您清理此處遇到的其他問題。
如果您查看該錯誤,則可以清楚地解釋該問題。 它在int main()
函數中提到boxMain.cpp就是有問題。 它還為您提供了行號,並提到“調用”沒有“匹配功能”,並且在兩種情況下都記錄了調用以及可能的候選對象,在這種情況下,這是沒有參數的方法。
除了這些,這里還有其他一些技巧可以幫助您避免進一步的沮喪:
優先使用前向聲明,而不要包含在頭文件中。 在您的情況下,您的box.hpp中根本不需要#include <iostream>
。 原因是標題中的所有內容實際上都會泄漏到包含標題的任何文件中。 這主要是為了減少循環依賴,並縮短進行更改時的編譯時間。 Pimpl Idiom是此口頭禪使用的示例模式。 但是,我不建議使用裸指針,而應該堅持使用RAII類型,例如智能指針或純引用。
您要么沒有粘貼所有代碼,要么您的include Guard錯誤。 您需要在標題中添加一個#endif
結束#endif
。
您應該使用約定來命名成員變量,以避免沖突,並使大型.cpp文件更易於閱讀。 這比規則更重要,但這很有幫助。 像_height
, mHeight
, _mHeight
而不是height
。 小寫的駱駝樣式變量名稱對於本地變量和參數是通用的。 看起來您真的真的不需要h,w,l
成員。
您可能應該使用assert
進行錯誤處理,以便在發布版本期間對其進行優化。 除非您打算向最終用戶顯示錯誤消息,否則這很粗糙。 您也可以考慮使用std :: cerr代替。
.cpp中的height
, width
和length
變量也是無關緊要的,不會按照您的想法做。
您應該在構造函數中使用初始化列表,而不要簡單地為主體中的變量賦值。 有幾種特殊情況很好,但這個可以讓你開始。 例如: Box::Box() : height(1.0), width(1.0), length(1.0){}
使用Box::
完全限定的cpp中唯一的方法是構造函數。 您還需要使用所有方法來執行此操作。 例如: double Box::getVolume()
您還沒有在main
函數中調用正確的方法(實際上您根本沒有在調用方法。基本上是在向前聲明它們。)您應該使用一個對象來調用方法。 這就是為什么您沒有在錯誤日志中看到有關缺少Box::
的投訴的原因Box::
頭文件中有一個主要錯誤與您的類的聲明無關,但與預編譯階段的頭有關。 您的標頭防護不正確,因為您必須先將#include <iostream>
置於防護之前; 這應該在警衛之后。 其次,您的#ifndef CLASS_NAME
與您的#define CLASS_NAME
不匹配,並且您在頭文件底部缺少匹配的#endif
。 我將刪除#include <iostream>
因為它不是必需的。 我這樣做的原因是因為如果用戶輸入的值小於或等於零,它將被設置為默認值1。因此,無需為此類打印任何錯誤消息。
您的課程接近您的需求。 有幾件事可以幫助改善您的課堂,也許這會有所幫助。
盒子
#ifndef BOX_H
#define BOX_H
class Box {
private:
double m_width;
double m_length;
double m_height;
double m_volume;
double m_surfaceArea;
public:
Box(); // Default Constructor
Box( double width, double length, double height );
// virtual ~Box(); // Default Okay
void setWidth( double width );
void setLength( double length );
void setHeight( double height );
double getWidth() const;
double getLegnth() const;
double getHeight() const;
double getVolume() const;
double getSurfaceArea() const;
private:
// If You Need Either The Copy Constructor Or The Assignment Operator
// Remove Them From The Private Section Into The Public Section
// Where You May Need To Implement Them Where The Default May Not Be Appropriate
Box( const Box& c ); // Not Implemented
Box& operator=( const Box& c ); // Not Implemented
void calculateVolume();
void calculateSurfaceArea();
}; // Box
#endif // BOX_H
Box.cpp
#include "Box.h"
// -------------------------------------------------------------------------
// Box() - Default Box Will Have a 1 x 1 x 1 Dimension
Box::Box() :
m_width( 1.0 ),
m_length( 1.0 ),
m_height( 1.0 ),
m_volume( 0.0 ),
m_surfaceArea( 0.0 ) {
calculateVolume();
calculateSuraceArea();
} // Box
// -------------------------------------------------------------------------
// Box() - User Defined Constructor
Box::Box( double width, double length, double height ) :
m_volume( 0.0 ),
m_surfaceArea( 0.0 ) {
if ( width <= 0 ) {
m_width = 1.0;
} else {
m_width = width;
}
if ( length <= 0 ) {
m_length = 1.0;
} else {
m_length = length;
}
if ( height <= 0 ) {
m_height = 1.0;
} else {
m_height = height;
}
calculateVolume();
calculateSurfaceArea();
} // Box
// -------------------------------------------------------------------------
// setWidth()
void Box::setWidth( double width ) {
// First Check To See If Value Passed In Is Same Member Value
if ( width == m_width ) {
// Nothing To Do
return;
} else if ( width <= 0 ) {
m_width = 1.0
} else {
m_width = width;
}
calculateVolume();
calculateSurfaceArea();
} // setWidth
// -------------------------------------------------------------------------
// setLength()
void Box::setLength( double length ) {
// First Check To See If Value Passed In Is Same Member Value
if ( length == m_length ) {
// Nothing To Do
return;
} else if ( length <= 0 ) {
m_length = 1.0
} else {
m_length = length;
}
calculateVolume();
calculateSurfaceArea();
} // setLength
// -------------------------------------------------------------------------
// setHeight()
void Box::setHeight( double height ) {
// First Check To See If Value Passed In Is Same Member Value
if ( height == m_height ) {
// Nothing To Do
return;
} else if ( height <= 0 ) {
m_height = 1.0
} else {
m_height = height;
}
calculateVolume();
calculateSurfaceArea();
} // setHeight
// -------------------------------------------------------------------------
// getWidth()
double Box::getWidth() const {
return m_width;
} // getWidth
// -------------------------------------------------------------------------
// getLength()
double Box::getLength() const {
return m_length;
} // getLength
// -------------------------------------------------------------------------
// getHeight()
double Box::getHeight() const {
return m_height;
} // getHeight;
// -------------------------------------------------------------------------
// getVolume()
double Box::getVolume() const {
return m_volume;
} // getVolume
// -------------------------------------------------------------------------
// getSurfaceArea()
double Box::getSurfaceArea() const {
return m_surfaceArea;
} // getSurfaceArea
// -------------------------------------------------------------------------
// calculateVolume()
void Box::calculateVolume() {
m_volume = m_width * m_length * m_height;
} // calculateVolume
// -------------------------------------------------------------------------
// calculateSurfaceArea()
void Box::calculateSurfaceArea {
m_dSurfaceArea = (m_width * m_length * 2) +
(m_width * m_height * 2) +
(m_length * m_height * 2);
} // calculateSurfaceArea
通過此類的設計,體積和表面積的計算僅對此類專有,因為它們所做的全部是內部計算,該計算保存到類成員變量中,在該成員變量中可以使用公共訪問方法來檢索它們。 同樣,3維設置器中的每一個都與兩個構造器進行相同的檢查,以得出小於和等於0的值,如果是,則將其默認設置為1。
如果傳遞給任何setter函數的值與已存儲的值相同,則該函數將不執行任何操作,僅返回。 如果該值大於0且不等於已保存的值,它將覆蓋成員變量並調用兩個私有計算方法以更新體積和表面積。
現在,如果您想改善此性能以獲得更好的性能,則可以將兩個計算方法都聲明為內聯,將它們移出cpp文件,然后在類聲明之后和#endif
之前將它們添加到頭文件中,或者可以添加` #include“ Box.inl”,只是創建另一個與該類同名的頭文件類型,除了帶有inl擴展名,然后在其中剪切和粘貼這兩個函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.