Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;类错误消息:没有用于调用的匹配函数_C++ - Fatal编程技术网

C++ C++;类错误消息:没有用于调用的匹配函数

C++ C++;类错误消息:没有用于调用的匹配函数,c++,C++,我的程序应该是计算一个盒子的表面积和体积。我将拥有以下功能:setHeight、setWidth、setLength、getVolume、getsurfaceearea 编译程序时,我收到以下错误消息: boxMain.cpp: In function ‘int main()’: boxMain.cpp:21: error: no matching function for call to 'Box::getVolume(double&, double&, double&

我的程序应该是计算一个盒子的表面积和体积。我将拥有以下功能:setHeight、setWidth、setLength、getVolume、getsurfaceearea

编译程序时,我收到以下错误消息:

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();
 };
#包括
#ifndef箱式水电站
#定义箱式水电站
类框{
私人:
双h,w,l;
双高、宽、长;
公众:
空隙高度(双倍h);
空隙设置宽度(双w);
空隙设置长度(双l);
双getVolume();
双getsurfaceearea();
Box();
};
这是我的function.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);
 }
 #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.hpp”
双高;
双倍宽度;
双倍长度;
Box::Box(){
高度=1.0;
宽度=1.0;
长度=1.0;
}
空隙设置高度(双h){
if(h<0){

std::cout
Box::getVolume
声明为不带参数,但在第21行中,您使用3个参数调用它:

box1.getVolume(h, w, l)
只需使用
box1.getVolume()
即可。类似地,对于
Box::getSurface()


另外,在所有成员函数定义前面使用
Box::
,如
void Box::setHeight(双h){…}
,否则您将最终定义独立函数,并且您将得到链接器错误,因为成员函数最终未定义。

Box::getVolume
声明为不带参数,但在第21行中,您使用3个参数调用它:

box1.getVolume(h, w, l)
只需使用
box1.getVolume()
即可。类似地,对于
Box::getSurface()


另外,在所有成员函数定义前面使用
Box::
,如
void Box::setHeight(双h){…}
,否则你最终会定义独立函数,并且你会得到链接器错误,因为成员函数最终没有定义。

vsoftco的答案基本上是正确的,所以我投票给了他,尽管我觉得最好在答案中添加更多的信息,以帮助更好地解释它,并帮助清理其他几个问题你这里有很多问题

如果仔细观察,您的错误清楚地解释了问题。它在函数
int main()
中提到了boxMain.cpp存在问题。它进一步提供了行号,并提到“调用”没有“匹配函数”在这两种情况下,注意调用以及可能的候选者,在本例中是没有参数的方法

除此之外,这里还有一些其他技巧可以帮助您避免进一步的挫折:

  • 更喜欢转发声明,而不是包含在头文件中。在您的情况下,您根本不需要在box.hpp中包含
    #include
    。原因是头文件中的任何内容都会有效地泄漏到包含头文件的任何文件中。这主要是为了减少循环依赖性,并缩短编译时间n进行更改。这是一个用于此咒语的示例模式。我不建议使用裸指针,但是,请坚持使用或普通引用之类的类型

  • 您可能没有粘贴所有代码,或者您的代码错误。您需要在标题中添加一个结束符
    #endif

  • 您应该使用约定来命名您的成员变量,以避免冲突,并使大型.cpp文件更易于阅读。这是一个首选项,而不是一个规则,但它很有帮助。例如
    \u height
    mHeight
    \u mHeight
    而不是
    height
    。小写骆驼式变量名称很常见对于局部变量和参数。看起来您根本不需要
    h,w,l
    成员

  • >P>您可能应该使用<代码> AsHeals<代码>来进行错误处理,以便在发布版本中优化它。这是粗略的,除非您打算向终端用户显示错误消息。您也可以考虑使用。

  • .cpp中的
    高度
    宽度
    长度
    变量也是无关的,不会按照您的想法执行

  • 你应该在构造器中使用初始化列表,而不是简单地在主体中分配变量。也有一些特殊情况,但可以让你开始。 例如:
    Box::Box():高度(1.0)、宽度(1.0)、长度(1.0){}

  • 在cpp中,您唯一可以完全限定的方法是构造函数。
    Box::
    。您还需要对所有方法都这样做。例如:
    double-Box::getVolume()

  • 您也没有在
    main
    函数中调用正确的方法(事实上,您根本没有调用方法。您基本上是在向前声明它们。)您应该使用对象来调用这些方法。这就是为什么您没有在错误日志中看到关于缺少
    框的投诉:


  • vsoftco的答案基本上是正确的,所以我投了他的票,尽管我觉得在答案中添加更多的信息以帮助更好地解释它,以及帮助解决您在这里遇到的其他几个问题是很好的

    如果仔细观察,您所遇到的错误清楚地解释了问题。它在函数
    int main()
    中提到boxMain.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