Simple C++ Error: “… undeclared (first use this function)”

后端 未结 3 1966
醉话见心
醉话见心 2020-12-21 05:52

I am working on my first C++ program for school. For some reason I am getting the following error when I try to compile it:

`truncate\' undeclared (first use          


        
3条回答
  •  粉色の甜心
    2020-12-21 06:29

    You need forward declaration before your main:

    double truncate(double d);
    double round(double d);
    

    You could just define your functions before main, that will solve the problem too:

    #include 
    #include 
    
    using namespace std;
    
    #define CENTIMETERS_IN_INCH 2.54
    #define POUNDS_IN_KILOGRAM 2.2
    
    // round result
    double round(double d) {
       return floor(d + 0.5);
    }
    
    // round and truncate to 1 decimal place
    double truncate(double d) {
       return round(double * 10) / 10;
    }
    
    int main() {
    ...
    }
    

提交回复
热议问题