Problem mit Funktionen auf eigene private Variable in Klasse zuzugreifen
-
Hallo.
Ich habe mir eine Klasse geschrieben. Diese ist in der headerdatei:
// solarCoordinateTransformation.h #ifndef solarCoordinateTransformation_h #define solarCoordinateTransformation_h #include <string> typedef struct fileInformation { std::string path; std::string filename; std::string picturename; std::string pathOfTheApplication; } fileInformation; typedef struct dataFromFile { //gives us the state of the sun double b0; double l0; double pAngle; bool satelliteState; //true if SESW, false if NENW } dataFromFile; typedef struct imageCoordinates { int x; int y; } imageCoordinates; typedef struct sunspotCoordinates { double lat; //Breite double lon; //Länge } sunspotCoordinates; class solarCoordinateTransformation { public: bool readFromFileForCalculating(fileInformation readFromFileInformation); void setInformationForCalculatingIfFailure(dataFromFile setIfFailure); sunspotCoordinates getSolarCoordinatesFromImageCoordinates(imageCoordinates coordinatesForCalculating); private: dataFromFile informationForCalculating; }; #endifDanach habe ich die Funktionen in der cpp dann deklariert.
//solarCoordinateTransformation.cpp #include <iostream> #include <fstream> #include <sstream> #define _USE_MATH_DEFINES #include <math.h> #include "solarCoordinateTransformation.h" using namespace std; bool solarCoordinateTransformation::readFromFileForCalculating(fileInformation readFromFileInformation) { bool successful; // informationForCalculating readFromFileInformation.picturename.insert(0,"\\"); fstream openFile; string::size_type pathfinder = readFromFileInformation.path.find("/"); if (pathfinder != string::npos) { for (unsigned z = 0; z < readFromFileInformation.path.size(); z++) { if (readFromFileInformation.path[z] == '/') readFromFileInformation.path.replace(z,1,"\\"); } } for (unsigned i = 0; i < readFromFileInformation.path.size(); ++i) if (readFromFileInformation.path[i] == '\\') readFromFileInformation.path.insert(i++, "\\"); bool cancelReading = false; openFile.open(readFromFileInformation.path+"\\\\"+readFromFileInformation.filename+".txt", ios::in); if (openFile.is_open()) { string fileContent [6]; string::size_type picturenameFindInFile; while (openFile.eof() == false || cancelReading == false) { getline(openFile,fileContent[0]); picturenameFindInFile = fileContent[0].find(readFromFileInformation.picturename); if (picturenameFindInFile != string::npos) { cancelReading = true; for (int w = 0; w < 6; ++w) { getline(openFile,fileContent[w]); } } } openFile.close(); cout<<endl<<fileContent[1].substr (10,15)<<endl; cout<<fileContent[2].substr (10,15)<<endl; cout<<fileContent[3].substr (10,15)<<endl; cout<<fileContent[4].substr (10,4)<<endl<<endl; stringstream convertStringToDouble; //In die Variable schreiben convertStringToDouble << fileContent[1].substr (10,15); convertStringToDouble >> informationForCalculating.b0; convertStringToDouble << fileContent[2].substr (10,15); convertStringToDouble >> informationForCalculating.l0; convertStringToDouble << fileContent[3].substr (10,15); convertStringToDouble >> informationForCalculating.pAngle; if (fileContent[4].substr(10,4) == "SESW") { informationForCalculating.satelliteState = true; } else { informationForCalculating.satelliteState = false; } // END successful = true; } else { cout << "Die Datei konnte nicht eingelesen werden" << endl; successful = false; } return successful; } void solarCoordinateTransformation::setInformationForCalculatingIfFailure(dataFromFile setIfFailure) { informationForCalculating.b0 = setIfFailure.b0; informationForCalculating.l0 = setIfFailure.l0; informationForCalculating.pAngle = setIfFailure.pAngle; informationForCalculating.satelliteState = setIfFailure.satelliteState; } sunspotCoordinates solarCoordinateTransformation::getSolarCoordinatesFromImageCoordinates(imageCoordinates coordinatesForCalculating) { //true if SESW, false if NENW sunspotCoordinates solarCoordinates; int sunCoorWithOriginInMidX; int sunCoorWithOriginInMidY; //aus Variable lesen cout << informationForCalculating.b0 << " " << informationForCalculating.l0 << " " << informationForCalculating.pAngle << " " << informationForCalculating.satelliteState << endl; //END if (informationForCalculating.satelliteState == true) { sunCoorWithOriginInMidX = coordinatesForCalculating.x - 512; sunCoorWithOriginInMidY = coordinatesForCalculating.y - 512; } else { sunCoorWithOriginInMidX = coordinatesForCalculating.x - 512; sunCoorWithOriginInMidY = (-coordinatesForCalculating.y + 512); } double thetas = atan((double)(sunCoorWithOriginInMidY) / (double) (sunCoorWithOriginInMidY)) *180. / M_PI; if (sunCoorWithOriginInMidX < 0) { thetas += 180; } else if (sunCoorWithOriginInMidY < 0) { thetas += 360; } double r = sqrt((double)((sunCoorWithOriginInMidX*sunCoorWithOriginInMidX)+ (sunCoorWithOriginInMidY*sunCoorWithOriginInMidY))); double theta = 180 - thetas - informationForCalculating.pAngle; double rho = asin(r/512.) * 180. / M_PI; double b2 = asin( cos(rho * M_PI/180.) * sin(informationForCalculating.b0 * M_PI/180.) + sin(rho * M_PI/180.) * cos(informationForCalculating.b0 * M_PI/180.) * sin(theta * M_PI/180.)) * 180./M_PI; solarCoordinates.lat = -b2; double azimuth = asin( cos(theta * M_PI/180.) * sin(rho * M_PI/180.) / cos(b2 * M_PI/180.)) * 180./M_PI; solarCoordinates.lon = informationForCalculating.l0 + azimuth; return solarCoordinates; }MEin Problem ist es, dass ich in "readFromFileForCalculating(fileInformation readFromFileInformation)" Werte in die variable informationForCalculating schreibe, diese dann aber nicht in "getSolarCoordinatesFromImageCoordinates(imageCoordinates coordinatesForCalculating)" benutzen kann. Da kommen immer falsche Werte raus.
bei dem bool "informationForCalculating.satelliteState" zum beispiel 214

Ich habe man die Codepassagen markiert.
-
Und der Debugger zeigt Dir, dass das schreiben funktioniert?
PS: Da du sowieso schon Klassen und anderes C++ benutzt, kannst Du das typedef-struct-Zeug meiner Meinung nach auch gleich lassen.
-
Also Compilerfeher habe ich keine.
-
danach ist auch nicht gefragt, der code sieht legal aus (ok, unschönheiten gibt es aber das ist eine andere sache)
Decimad legt dir die benutzung eines debuggers nahe. welchen compiler nutzt du? msvc?? g++?
egal welchen, deine IDE sollte einen debugger haben, mit dem du einen breakpoint in dein programm setzen kannst und ab da kannst du alles verfolgen und überprüfen ob die ergebnisse deinen erwartungen entsprechen
wenn du das noch nie gemacht hast, oder nie was davon gehört hast, dann mach es jetzt!
-
Hatte ich noch nie gemacht

dann mache ich es jetzt. benutze visual studio.hmm. also er hat probleme mit den stringstream.
denn die werte liest er ja richtig ein, aber schreibst sie halt falsch in der stringstream oder in die variable.Aber das übergeben von Werte in der private Variable ist doch so ok. Also sollte funktionieren oder?
-
ehrlich gesagt hab ich keinen überblick über dein programm, d ablicke ich jetzt auf die schnelle auch nicht durch
welche zeile in deinem geposteten quellcode?
-
In Zeile 49-59 wird es mMn in die variable geschriebn.
dann in Z.86 ausgegeben.
Die Variable ist deklariert in der Headerdate in Zeile 40
-
Wo wird denn convertStringToDouble erstellt und worum handelt es sich?
Btw: Kannst du die eine lange Codezeile umbrechen bitte, mein Browser läuft hier auf dem zweiten, kleinen Monitor
PS: Wer sponsort mir einen zweiten, großen Monitor, es ist doch bald Weihnachten
:xmas1:
-
convertStringToDuoble ist ein stringstream.
ich hatte es schon mit atof versucht, leider funktioniert das nicht.
Habe den Code jetzt mal für dich verbessert.
EDIT:
atof funktioniert jetzt doch. habe and den string noch ein .c_str() drangehangen.
-
Hab mir jetzt mal das eingelesene anzeigen lassen.
code gekürzt:
//solarCoordinateTransformation.cpp #include <iostream> #include <fstream> #include <sstream> #define _USE_MATH_DEFINES #include <math.h> #include "solarCoordinateTransformation.h" using namespace std; bool solarCoordinateTransformation::readFromFileForCalculating(fileInformation readFromFileInformation) { /*.....*/ informationForCalculating.b0 = atof(fileContent[1].substr (10,15).c_str()); informationForCalculating.l0 = atof(fileContent[2].substr (10,15).c_str()); informationForCalculating.pAngle = atof(fileContent[3].substr (10,15).c_str()); if (fileContent[4].substr(10,4) == "SESW") { informationForCalculating.satelliteState = true; } else { informationForCalculating.satelliteState = false; } cout << "einlesen: " << " " << informationForCalculating.b0 << " " << informationForCalculating.l0 << " " << informationForCalculating.pAngle << " " << informationForCalculating.satelliteState; //Hier stimmt es. Das richtige wird mir angezeigt. /*.......*/ return successful; } sunspotCoordinates solarCoordinateTransformation::getSolarCoordinatesFromImageCoordinates(imageCoordinates coordinatesForCalculating) { /*.......*/ cout << informationForCalculating.b0 << " " << informationForCalculating.l0 << " " << informationForCalculating.pAngle << " " << informationForCalculating.satelliteState << endl; //Heir wird mir das falsche ausgegeben. /*.......*/ }Hat hier jemand eine Idee? Ich weiss nicht mehr weiter

Die header Datei habe ich nicht verändert.
-
Wie werden die beiden methoden der Klasse aufgerufen?
-
Ja das werden sie mMn. Ich header Datei mit der Klasse ist im ersten Post.
Falls was falsch ist sag es mir bitte.
Wie meinst du das? Also wie sie im int main aufgerufwn werden?
-
Crysis_02 schrieb:
Wie meinst du das? Also wie sie im int main aufgerufwn werden?
ja das meine ich.
-
Kann ich dir erst sagen wenn ich heute abend zuhause bin.
-
So. Nun bin ich zuhase und habe selbst das Problem gefunden. Ich hatte der Klasse immer wenn ich eine Funktion aufgerufen habe einen neuen Instanzname gegeben. So funktioniert es nun
