All- ich habe das ziemlich viel recherchiert. Mein Programm kompiliert ohne Fehler, aber die Werte aus den Funktionen innerhalb der Struktur werden nicht an das Programm übergeben. Kannst du mir helfen, herauszufinden, warum sie es nicht sind? Ich habe die Codeschnipsel eingefügt, die die fraglichen Komponenten zeigen. Hauptsächlich, mein Code wie: "& allData :: ConvertToC" gibt keinen Wert von der Funktion innerhalb der Struktur "allData" zurück. Es gibt nur den Wert 1 zurück, unabhängig von der Eingabe von "allData.temperature". Ich kenne alle Komponenten des Programms, andere als die genannten funktionieren.C++ - Funktionen innerhalb von Struct und Aufruf von Werten durch Referenz
Code-Schnipsel:
//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;
}
'& ALLDATA :: ConvertToC' ist die Adresse des Verfahrens Ihr nennen es nicht, müssen Sie' allData.ConvertToC (allData.temperature) 'tun. – Holt
Das ist auch die richtige Antwort, glaube ich! Vielen Dank! – cppstudent1