Wie initialisiere ich convert.gram? Immer wenn ich in der Klassendeklaration "Gramm" definiere, reagiert das Programm entsprechend. Ich versuche, Gramm in den Konstruktor zu setzen, aber es funktioniert nicht. Strukturiere ich auch alles richtig? Danke für die Hilfe!Die Variable benötigt einen Initialisierer C++?
Code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include "math.h"
using namespace std;
struct grams{
grams();
float converter(float pounds);
float gram;
float pounds;
float answer;
};
float grams::converter(float pounds){
answer = pounds * gram;
return answer;
}
grams::grams(){
float convert.gram = 453.592;
}
int main(){
float PtC;
grams convert;
cout<<"Pound to Gram Converter \n";
cout<<"How Many Pounds Do You Want to Convert? \n";
cin>>PtC;
float converter = convert.converter(PtC);
cout<<"Answer = "<<converter<<endl;
return 0;
}
Fehler:
C:/Users/lisa/Desktop/codelight c++/time_of_for_loop/for_loop_time/for_loop_time/main.cpp:31:13: error: expected initializer before '.' token
'float convert.gram = 453.592;' was willst du hier machen? – vu1p3n0x
@ vu1p3n0x Das Programm konvertiert Pfund in Gramm. Ich werde das Programm erweitern, also möchte ich es mit Klassen organisieren. Ich habe Probleme mit dieser Linie. Die Zeile soll "gram" im Konstruktor auf 453.592 setzen, wenn eine neue Instanz von Gramm konvertiert wird. –