c++ - C++中的头文件

标签 c++

以下错误的头文件: 对“swimmingPool::timeNeeded(int)”的 undefined reference

我在代码中犯了一些小错误,但无法弄清楚是什么。这门语言相对较新。请帮忙!!

    #include<iostream>
    #include<cstdio>
    #include "swimmingPool.h"

    using namespace std;

    int main()
    {
        cout << "Pool Data: " << endl;
        swimmingPool p(30,15,10,0,0);
        int rate_in;
        cout << "   Length: " << p.length << endl;
        cout << "   Width: "<< p.width << endl;
        cout << "   Depth: "<< p.depth << endl;
        cout << "   Total water in the pool: " << p.water_initially <<endl;
        cout << "To completely fill the pool" << endl;
        cout << "   Enter water fill in rate: ";
        cin >> rate_in;

        cout<< endl;
        cout << endl;
        cout << "Time to fill the pool is approximately: " <<                         p.timeNeeded(rate_in)/3600 << " hours and "<<
        p.timeNeeded(rate_in)%60<< " minutes."<<endl;
        return 0;

    }


    /**********************************************************************************/
    /******************** swimmingPool.h         **********************************************/

    #ifndef __IH__
    #define __IH__

    class swimmingPool{
    public:
        int length;
        int width;
        int depth;
        int rate_out;
        int water_initially;
        int waterNeeded();
        int timeNeeded(int in);
        swimmingPool(int a,int b, int c, int d,int e){}

    };

    #endif

    /********************************************************************************/
    /******************************** swimmingPoolImp.cpp ****************************/
    #include<iostream>
    #include<cstdio>
    #include "swimmingPool.h"

    swimmingPool::swimmingPool(int a,int b, int c, int d,int e) //IT has five         parameters for length/breadth/height/water_initially/rate_out.Rate to be taken input from         user.
    {
            length = a;
            width = b;
            depth = c;
            rate_out = d;
            water_initially = e;
    }

    int swimmingPool::waterNeeded()
    {
        return( length*width*depth - water_initially)*7.48; //conversion factor
    }

    int swimmingPool::timeNeeded(int rate_in)
    {
            if(rate_in > rate_out)
            {
                return (( waterNeeded() / (rate_in - rate_out))*60);
            }
            else
            {
                return (( waterNeeded() / (rate_out - rate_in))*60);
            }
    };

最佳答案

“undefined reference to...”类别中的错误消息通常表示函数、编译的或其他方式未链接到您的可执行文件中。

首先,确保 swimmingPool.cpp 的目标文件(swimmingPool.o 或 windows,swimmingPool.obj)已链接到您的程序中。

如果您已确认,那么您的 make 工具可能在添加 timeNeeded 后忘记重新编译 swimmingPool.cpp,因此该目标文件已过时。如果是这种情况,您应该重新编译 swimmingPool.cpp。

执行此操作所需的特定命令取决于您的工具链。

此外,您的函数不是“const-correct”的,您应该在学习后尽快查找它。

关于c++ - C++中的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103213/

相关文章:

c++ - 从两个模板参数多重继承的奇怪?

c++ - 模板 C++0x lambda 函数……还是仿函数/谓词?

python - 通过在 OPENCV 和实时视频中使用固有矩阵和畸变系数来进行相机校准

c++ - 告诉 C++ 控制台等待

c++ - 是否在退出时清理

c++ - 返回一个指向智能指针的原始指针

c++ - 0.1 与 C++ 中的 0,1,为什么两者都不会导致错误?

c++ - 静态 boost.test 库和动态 boost.test 库

c++ - 具有现有变量的基于范围的 for 循环

c++ - 使用 Maya API 类 MArgList 中的 get 函数时获得神秘的类型转换