c++ - 段错误(核心转储)错误

标签 c++ fault segmentation-fault ostream

我的程序编译罚款,但在输入文件时出现“段错误(核心转储)”错误。我是否没有正确处理 ostream?

#include <std_lib_facilities.h>

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }
    bool operator<(const Reading &r) const;
};

bool Reading::operator<(const Reading &r) const
{
// stub version                                                                                         

    if (temperature < r.temperature){

        return true;

}
    else if (r.temperature < temperature)  {

        return false;
    }


}

/*                                                                                                      
 * function declarations                                                                                
 */

ostream& operator<<(ostream& ost, const Reading &r);

vector<Reading> get_temps();

double check_adjust_temp(double temperature, char scale);

double c_to_f(double temperature);

double mean(vector<Reading> temps);

double median(vector<Reading> temps);

void print_results(const vector<Reading>& temps, double mean_temp,
                   double median_temp);

int main()
    try
        {
            vector<Reading> temps = get_temps();
            if (temps.size() == 0) error("no temperatures given!");
            double mean_temp = mean(temps);
            sort(temps.begin(), temps.end());
            double median_temp = median(temps);
            print_results(temps, mean_temp, median_temp);
        }
    catch (exception& e) {
        cerr << "error: " << e.what() << '\n';
        return 1;
    }
    catch (...) {
        cerr << "Oops: unknown exception!\n";
        return 2;
    }

/*                                                                                                      
 * function definitions                                                                                 
 */

ostream& operator<<(ostream& ost, const Reading &r)
{

    return ost << '(' << r.hour
               << ',' << r.temperature <<')';
}

vector<Reading> get_temps()
{
    cout << "Please enter name of input file name: ";
    string name;;
    cin >> name;
    ifstream ist(name.c_str());
    if(!ist) error("can't open input file ", name);

    vector<Reading> temps;
    int hour;
    double temperature;
    while (ist >> hour >> temperature){
        if (hour <0 || 23 <hour) error("hour out of range");
        temps.push_back( Reading(hour,temperature));
    }

}

double check_adjust_temp(double temperature, char scale)
{
    if (scale == 'c' || 'C'){

        return c_to_f(temperature);
    }
    else if (scale == 'f' || 'F')  {

        return temperature;
    }
    else {

        error("Wrong input type");
    }
}

double c_to_f(double temperature)
{
    double c;
    c = ((temperature * (9.0/5)) + 32);
    return (c);
}

double mean(vector<Reading> temps)
{
    double mean_temp;
    double sum = 0;
    for (int i = 0; i< temps.size(); ++i) sum += temps[i].temperature;
    mean_temp = sum/temps.size();
    return (mean_temp);
}

double median(vector<Reading> temps)
{
    double median_temp;
    sort (temps.begin(), temps.end());
    median_temp = temps[temps.size()/2].temperature;
    return (median_temp);
}

void print_results(const vector<Reading>& temps, double mean_temp,
                   double median_temp)
{

    cout << "The sorted temperatures are:\n";
    cout << get_temps;
    cout << "The mean temperature is " << mean_temp << ".\n";
    cout << "The median temperature is " << median_temp << ".\n";
}

最佳答案

scale == 'c' || 'C'

并没有像你想象的那样做。它的解析如下:

( scale == 'c' ) || 'C'

'C' 始终为真。如果您启用了编译器警告,您应该已经注意到这一点。

(不,这不是您眼前的问题;它在 get_temps 末尾出现。但是启用警告后,您也会看到这一点。)

关于c++ - 段错误(核心转储)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4363133/

相关文章:

c++ - 希望线程在父级退出时不死 - linux

c - 为什么在下面的代码上出现段错误?

c - 我不明白我是如何遇到段错误的

c++ - 为什么 `this`等于0x0,导致我的程序崩溃?

c++ - 如何使用 pthread 在对象内部运行线程

c++ - C++ 中 "using"关键字背后的逻辑是什么?

c# - 通信异常并自动重新连接?

c - C 中的阶乘递归(段错误)

c - 为什么会出现错误 "segmentation fault"?

c++ - 设置为包含//在 Doxygen 中?