klasseninstanz in vector
-
hallo,
folgendes problem.eine instanz der klasse mynode.cpp
#include "StdAfx.h" #include "mynode.h" mynode::mynode(void) { } mynode::mynode(string n, int i){ name = n; index = i; } mynode::~mynode(void) { } void mynode::setNode(string n, int i){ name = n; index = i; } string mynode::getName(){ return name; } int mynode::getIndex(){ return index; }soll in einem vector der klasse myapp.cpp gespeichert werden.
#include "StdAfx.h" #include "myapp.h" #include "mynode.h" myapp::myapp(void) { } myapp::~myapp(void) { } //System::String^ s void myapp::addNode(std::string s, int i){ mynode *mn = new mynode(s,i); vec_nodes.push_back(*mn); delete mn; } std::string myapp::SysToStd(System::String^ S){ std::string ss; if (S->Length == 0) return ss; System::IntPtr ip = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(S); try{ ss = static_cast<const char*>(ip.ToPointer()); }__finally{ System::Runtime::InteropServices::Marshal::FreeHGlobal(ip); } return ss; }eine instanz von myapp wird wiederum in Form1.h aufgerufen. aus einer xml datei sollen ein string und ein in gelesen werden.
#pragma once #include "mynode.h" #include "myedge.h" #include "myapp.h" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Xml; namespace Ue02 { /// <summary> /// Zusammenfassung für Form1 /// /// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch /// die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern, /// das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt. /// Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen /// arbeiten, die diesem Formular zugewiesen sind. /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Konstruktorcode hier hinzufügen. // } protected: /// <summary> /// Verwendete Ressourcen bereinigen. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::ComboBox^ comboBox1; protected: private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Label^ label2; private: System::Windows::Forms::ComboBox^ comboBox2; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; protected: private: /// <summary> /// Erforderliche Designervariable. /// </summary> System::ComponentModel::Container ^components; myapp *ma; private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Button^ button3; String^ filename; #pragma region Windows Form Designer generated code /// <summary> /// Erforderliche Methode für die Designerunterstützung. /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. /// </summary> void InitializeComponent(void) { this->comboBox1 = (gcnew System::Windows::Forms::ComboBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->label2 = (gcnew System::Windows::Forms::Label()); this->comboBox2 = (gcnew System::Windows::Forms::ComboBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->button3 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // comboBox1 // this->comboBox1->FormattingEnabled = true; this->comboBox1->Location = System::Drawing::Point(159, 39); this->comboBox1->Name = L"comboBox1"; this->comboBox1->Size = System::Drawing::Size(181, 21); this->comboBox1->TabIndex = 0; // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(24, 39); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(32, 13); this->label1->TabIndex = 2; this->label1->Text = L"Start:"; // // label2 // this->label2->AutoSize = true; this->label2->Location = System::Drawing::Point(27, 91); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(63, 13); this->label2->TabIndex = 3; this->label2->Text = L"Destination:"; // // comboBox2 // this->comboBox2->FormattingEnabled = true; this->comboBox2->Location = System::Drawing::Point(159, 83); this->comboBox2->Name = L"comboBox2"; this->comboBox2->Size = System::Drawing::Size(181, 21); this->comboBox2->TabIndex = 4; // // button1 // this->button1->Location = System::Drawing::Point(27, 262); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(101, 23); this->button1->TabIndex = 6; this->button1->Text = L"Start"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(138, 262); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(101, 23); this->button2->TabIndex = 7; this->button2->Text = L"Nodes laden"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // openFileDialog1 // this->openFileDialog1->FileName = L"openFileDialog1"; // // textBox1 // this->textBox1->AcceptsReturn = true; this->textBox1->AcceptsTab = true; this->textBox1->Location = System::Drawing::Point(27, 125); this->textBox1->Multiline = true; this->textBox1->Name = L"textBox1"; this->textBox1->ScrollBars = System::Windows::Forms::ScrollBars::Vertical; this->textBox1->Size = System::Drawing::Size(313, 119); this->textBox1->TabIndex = 8; this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged); // // button3 // this->button3->Location = System::Drawing::Point(251, 262); this->button3->Name = L"button3"; this->button3->Size = System::Drawing::Size(88, 23); this->button3->TabIndex = 9; this->button3->Text = L"Edges laden"; this->button3->UseVisualStyleBackColor = true; this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(365, 301); this->Controls->Add(this->button3); this->Controls->Add(this->textBox1); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->comboBox2); this->Controls->Add(this->label2); this->Controls->Add(this->label1); this->Controls->Add(this->comboBox1); this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { // OpenFile-Dialog mit Filter // Der Pfad wird in String ^filename gespeichert openFileDialog1->Filter = "Xml-Datei (*.xml)|*.xml|Alle Dateien (*.*)|*.*"; openFileDialog1->Title = "Eine *.xml-Datei"; openFileDialog1->FileName = "Nodes.xml"; if(openFileDialog1->ShowDialog() ==System::Windows::Forms::DialogResult::OK){ filename = openFileDialog1->FileName->ToString(); } XmlTextReader^ reader = nullptr; try{ // Lädt den reader mit Datei und ignoriert Whitespaces reader = gcnew XmlTextReader(filename); reader->WhitespaceHandling = WhitespaceHandling::None; if(!reader->IsStartElement("nodes")){ MessageBox::Show("Keine Xml Datei mit Nodes ausgewählt!"); return;} while (reader->Read()) { switch (reader->NodeType) { case XmlNodeType::Element: String ^s; int index; // Wert innerhalb der Tags wird in einen Vektor geschrieben //textBox1->AppendText(reader->Name + "\r\n"); if (reader->Name == "index") index = Convert::ToInt32(reader->ReadElementString()); if (reader->Name == "name") s = reader->ReadElementString(); ma->addNode("test", index); break; case XmlNodeType::Text: // Wert innerhalb der Tags wird in einen Vektor geschrieben //textBox1->AppendText(reader->Value + "\r\n"); break; } } }catch(Exception ^e){ MessageBox::Show("Keine Datei ausgewählt"); MessageBox::Show(e->ToString()); } finally { if (reader != nullptr) // Wenn reader instantiert wurde, dann schließe reader reader->Close(); } } private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) { // OpenFile-Dialog mit Filter // Der Pfad wird in String ^filename gespeichert openFileDialog1->Filter = "Xml-Datei (*.xml)|*.xml|Alle Dateien (*.*)|*.*"; openFileDialog1->Title = "Eine *.xml-Datei"; openFileDialog1->FileName = "Edges.xml"; if(openFileDialog1->ShowDialog() ==System::Windows::Forms::DialogResult::OK){ filename = openFileDialog1->FileName->ToString(); } XmlTextReader^ reader = nullptr; try{ // Lädt den reader mit Datei und ignoriert Whitespaces reader = gcnew XmlTextReader(filename); reader->WhitespaceHandling = WhitespaceHandling::None; if(!reader->IsStartElement("edges")){ MessageBox::Show("Keine Xml Datei mit Edges ausgewählt!"); return;} while (reader->Read()) { switch (reader->NodeType) { case XmlNodeType::Element: // Wert innerhalb der Tags wird in einen Vektor geschrieben textBox1->AppendText(reader->Name + "\r\n"); break; case XmlNodeType::Text: // Wert innerhalb der Tags wird in einen Vektor geschrieben textBox1->AppendText(reader->Value + "\r\n"); break; } } }catch(Exception ^e){ MessageBox::Show("Keine Datei ausgewählt"); MessageBox::Show(e->ToString()); } finally { if (reader != nullptr) // Wenn reader instantiert wurde, dann schließe reader reader->Close(); } } }; }leider bekomme ich jetzt immer einer exception. Nullreferenceexception. "Ein Objektverweis wurde nicht auf eine Objektinstanz festegelegt" Ich verstehe nicht ganz wieso.
vielen dank für die hilfe.
-
das ist die exception:
System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
bei std.vector<mynode,std::allocator<mynode> >.size(vector<mynode\,std::allocator<mynode> >* ) in c:\program files\microsoft visual studio 9.0\vc\include\vector:Zeile 726.
bei std.vector<mynode,std::allocator<mynode> >.push_back(vector<mynode\,std::allocator<mynode> >* , mynode* _Val) in c:\program files\microsoft visual studio 9.0\vc\include\vector:Zeile 810.
bei myapp.addNode(myapp* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* s, Int32 i) in c:\alg-projekte\ue02\ue02\myapp.cpp:Zeile 16.
bei Ue02.Form1.button2_Click(Object sender, EventArgs e) in c:\alg-projekte\ue02\ue02\form1.h:Zeile 230.
-
WO tritt die Exception denn auf ?
Schau mal mit dem Debugger nach.
-
dann wenn er es in den vector schreiben will.
meiner meinung nach ist aber alles korrekt instantiiert.
-
auch das
myapp *ma;
?
Sehe nicht worauf das zeigt. Oder überseh ich das in dem Codehaufen
-
vec_nodes.push_back(*mn);hier?
Wie erstellst du denn "vec_nodes" ?
-
hier ist die myapp.h
meiner meinung nach auch korrekt.#pragma once #include <vector> #include "mynode.h" #include "myedge.h" using namespace std; class myapp { private: vector <mynode> vec_nodes; vector <myedge> vec_edges; public: myapp(void); ~myapp(void); void addNode(std::string s, int i); std::string myapp::SysToStd(System::String^ S); };
-
Das ist kein C++, ist also hier das falsche Forum.
Sieht aber der Fehlermeldung nach zu urteilen schwer danach aus, als wäre ma nicht initialisiert.
-
ich meine worauf zeit ma ?
wer erzeugt wo ein myapp?
-
vec_nodes.push_back(*mn);ja, genau an dieser stelle wird die exception geschmissen.
-
myapp *ma;zeile 65 in Form1.h. ma ist eine instanz von myapp.
-
Wenn du das ganze Projekt als zip hättest wäre das einfacher...
-
glasauge schrieb:
zeile 65 in Form1.h. ma ist eine instanz von myapp.
Nö, ma ist ein Zeiger auf myapp.
Es sei denn, * hat in dieser Sprache eine völlig andere Bedeutung als in C++.
-
ma=new myapp(); ma->addNode("test", index);gehts damit ?
-
ja, damit funktioniert es. jetzt bekomme ich aber eine exception bei SysToStd bzw StdToSys.
-
wo könnte man denn "ma = new myapp()" aufrufen, damit es bei formularaufruf instantiiert wird und nicht erst bei klick auf den button.
-
glasauge schrieb:
wo könnte man denn "ma = new myapp()" aufrufen, damit es bei formularaufruf instantiiert wird und nicht erst bei klick auf den button.
Im Konstruktor?
-
ok, ich hab es jetzt im konstrukto von Form1.h stehen.
Jetzt wirft er mir diese Exception:
System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. bei myapp.SysToStd(myapp* , basic_string<char\,std::char_traits<char>\,std::allocator<char> >* , String S) in c:\alg-projekte\ue02\ue02\myapp.cpp:Zeile 19. bei myapp.addNode(myapp* , String s, Int32 i) in c:\alg-projekte\ue02\ue02\myapp.cpp:Zeile 34. bei Ue02.Form1.button2_Click(Object sender, EventArgs e) in c:\alg-projekte\ue02\ue02\form1.h:Zeile 232.also im grunde genommen, der gleiche fehler.
hier nochmal die veränderte myapp.cpp
#include "StdAfx.h" #include "myapp.h" #include "mynode.h" myapp::myapp(void) { } myapp::~myapp(void) { } //System::String^ s std::string myapp::SysToStd(System::String^ S){ std::string ss; if (S->Length == 0) return ss; System::IntPtr ip = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(S); try{ ss = static_cast<const char*>(ip.ToPointer()); }__finally{ System::Runtime::InteropServices::Marshal::FreeHGlobal(ip); } return ss; } System::String^ myapp::StdToSys(string str){ return gcnew System::String(str.c_str()); } void myapp::addNode(System::String^ s, int i){ mynode *mn = new mynode(SysToStd(s),i); vec_nodes.push_back(*mn); delete mn; }
-
hat niemand eine idee, woran es liegen könnte?
-
System.NullReferenceException frag doch mal asm-forum nach... oder du probierst einfach alle foren durch, bis du das richtige gefunden hast...
oder du weist, was du dort machst und postest in nem .NET Unterforum... hört sich fast vernünftig an, oder?bb