【问题标题】:C++ functions within struct and calling values by reference结构中的 C++ 函数和通过引用调用值
【发布时间】:2016-06-23 13:48:57
【问题描述】:

所有-我已经对此进行了很多研究。我的程序编译没有错误,但结构中函数的值没有传递给程序。你能帮我弄清楚为什么他们不是吗?我包含了显示相关组件的代码的 sn-ps。主要是,我的代码如:“&allData::ConvertToC”没有从结构“allData”中的函数返回任何值。无论“allData.temperature”的输入如何,它都只会返回值 1。我知道该程序的所有组件,除了提到的那些都在工作。

代码 sn-ps:

//defining the struct

struct allData {
    char selection; 
    double centigrade; 
    double fahrenheit; 
    double temperature; 
    double ConvertToC (const double& temperature);
    double ConvertToF (const double& temperature);
} allData;

//adding data to the struct for the functions within the struct to use

cout << "Enter C for converting your temperature to Celsius, or enter F for converting your temperature to Fahrenheit, and press ENTER." << endl << endl;

cin >> allData.selection; 

cout << "Enter your starting temperature to two decimal places, and press ENTER." << endl << endl; 

cin >> allData.temperature; 

switch (allData.selection) {

//my attempt to reference the functions within the struct and the data in the struct, but it is not working and always returns a value of 1. 

case 'c': { &allData::ConvertToC; 

    cout << "Your temperature converted to Celsius is: " << &allData::ConvertToC   
    << endl << endl; 
    break; 
    }

case 'C': { &allData::ConvertToC; 

    cout << "Your temperature converted to Celsius is: " << &allData::ConvertToC    
    << endl << endl; 
    }
}


//Function definitions that are located in the struct. Do I define the functions in the normal way, like this, if they are located in the struct?

double allData::ConvertToF (const double& temperature) {

    double fahrenheit = 0;
    fahrenheit = temperature * 9 / 5 + 32;
    return fahrenheit; 

}


double allData::ConvertToC (const double& temperature) {

    double centigrade = 0;
    centigrade = (temperature - 32) * 5 /9; 
    return centigrade; 

}

【问题讨论】:

  • &amp;allData::ConvertToC是方法的地址,你没有调用它,你需要allData.ConvertToC(allData.temperature)
  • 这也是正确答案,我相信!谢谢!

标签: c++ function struct pass-by-reference


【解决方案1】:

您没有执行函数调用,您只是将函数指针传递给 cout 流。

我认为你真正想要的是这样的:

 cout << "Your temperature converted to Celsius is: " << allData.ConvertToC(allData.temperature) << endl; 

此外,您不需要在“ConvertToC”方法中通过引用传递,因为您并没有真正保存任何东西(“双精度”是 8 个字节宽,而引用/指针在 32 位系统上是 4 个字节,或 64 位系统上的 8 个字节)。

【讨论】:

  • 注意:像这样反复调用endl 是不好的做法,因为它会不必要地刷新流。相反,您应该将 \ns 添加到流中,如果确实需要,请在最后刷新它。
  • 确实是我自己的错在没有检查其余行的情况下复制和粘贴!我现在就编辑。
  • 谢谢大家 - 我将使用 \n 而不是 endl。但是,我进行了以下编辑无济于事。我将“&allData::ConvertToC”转换为“allData::ConvertToC(allData.temperature)”。我现在得到一个编译器错误:“无法在没有对象案例'c'的情况下调用成员函数'double allData::ConvertToC(double)':{allData::ConvertToC(allData.temperature)”
  • 我再次编辑了我的答案,应该是“allData.ConvertToC”而不是“allData::ConvertToC”,对不起!
  • 谢谢!我相信这已经奏效了。不过,我需要进一步测试它。
【解决方案2】:
// Name of struct made distinct from its instance, for clarity.
struct AllData {
    char selection;
    double centigrade;
    double fahrenheit;
    double temperature;
    double ConvertToC ();
    double ConvertToF ();
} allData;

...

allData.selection = 'C';
allData.temperature = 74.5;

switch (allData.selection)
{
case 'c':
case 'C':
    cout << "Your temperature converted to Celsius is: " << allData.ConvertToC() << endl << endl;
    break;
}

...

double AllData::ConvertToF ()
{
    //double fahrenheit = 0;    Why not store the result in the struct?
    fahrenheit = temperature * 9 / 5 + 32;
    return fahrenheit;
}

double AllData::ConvertToC ()
{
    //double centigrade = 0;
    centigrade = (temperature - 32) * 5 / 9;
    return centigrade;
}

【讨论】:

  • 这也正常工作。我之前的函数定义中存在冗余,您也指出了这一点。谢谢!
猜你喜欢
  • 2018-09-22
  • 2018-01-18
  • 2015-11-14
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 2012-01-04
相关资源
最近更新 更多