C++类错误消息:没有匹配的调用函数
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()
.
此外,在所有成员函数定义(如 void Box::setHeight(double h) {...}
(前面使用 Box::
,否则最终会定义独立函数,并且由于成员函数最终未定义,因此会出现链接器错误。
vsoftco的答案大多是正确的,所以我投票给他,尽管我觉得最好在答案中添加更多信息以帮助更好地解释它,并帮助清理您在这里遇到的其他几个问题。
如果你看它,你的错误清楚地解释了问题。 它提到boxMain.cpp在函数int main()
有问题。 它进一步为您提供了行号,并提到"调用"没有匹配函数",并且在这两种情况下都记录了调用以及可能的候选者,在这种情况下是没有参数的方法。
除此之外,这里有一些其他提示可以帮助您避免进一步的挫败感:
-
首选前向声明,而不是包含在头文件中。 在您的情况下,您的box.hpp根本不需要
#include <iostream>
。 原因是标头中的任何内容都会有效地泄漏到包含标头的任何文件中。 这主要是为了帮助减少循环依赖性,并在进行更改时缩短编译时间。 痘痘成语是用于此咒语的示例模式。 但是,我不建议使用裸指针,而是坚持使用 RAII 类型,例如智能指针或纯引用。 -
您要么没有粘贴所有代码,要么您的包含保护是错误的。 您需要在标题中有一个结束
#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::
的投诉的原因
头文件中有一个主要错误,它与类的声明无关,但与预编译阶段的标头有关。你的头球后卫不正确,因为你首先在后卫面前有你的#include <iostream>
;这应该是在警卫之后。其次,您的#ifndef CLASS_NAME
与您的#define CLASS_NAME
不匹配,并且您缺少头文件底部的匹配#endif
。我将删除#include <iostream>
,因为它不需要。我这样做的原因是,如果用户输入的值小于或等于零,则该值将设置为默认值 1。因此,无需为此类打印出任何错误消息。
课程接近您的需求。有一些事情可以帮助提高你的班级,也许这会有所帮助。
盒子.h
#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
盒子.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
使用此类的设计,体积和表面积的计算仅对此类是私有的,因为它们所做的只是内部计算,该计算将保存到类成员变量中,其中有公共访问方法可用于检索它们。此外,每个三维资源库对小于和等于 0 的值执行与两个构造函数相同的检查,如果是这样,则默认为 1。
如果传递到任何 setter 函数的值与已存储的值相同,则该函数将不执行任何操作,只是返回。如果该值大于 0 且不等于已保存的值,它将覆盖成员变量并调用两个私有计算方法来更新体积和外围应用。
现在,如果您想改进它以获得更好的性能,那么您可以将两个计算方法声明为内联,将它们移出 cpp 文件并将它们添加到类声明之后和#endif
之前的头文件中,或者您可以添加一个 '#include "Box.inl" 并创建另一个与类同名的头文件类型,除了使用 inl 扩展名并剪切和粘贴这两个函数在那里。
- 如何用参数值调用函数(仅在运行时已知)
- 从python中调用C++函数并获取返回值
- 当使用通配符和null指针调用函数时,对输出的说明
- 从R调用C++函数并对其进行集成时出错
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 如何在qt中从另一个类调用函数
- 在 COUT 语句中使用 COUT 调用函数
- 如何从线程中的不同模块调用函数?
- C++从函数指针数组调用函数
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何在 C/C++ 中从外部库调用函数
- 如何使用运算符在同一行中多次调用函数
- 是否可以创建一个从不同类调用函数的线程?
- 无法为类成员调用函数
- 如何从另一个标头 c++ 调用函数
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 如何只允许在调用函数 B 后调用函数 A?
- 我可以这样调用函数吗?
- 如何在 c++ 的类中递归调用函数方法?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?