Variable für Bibliothekenfunktionen global machen?
-
Hallo, ich möchte eine cpp mit den eigenen Funktionen erstellen.
Ich habe ein CLR Win32Form Anwendung erstellt. Da ist im Form.h der haupsächliche code.
hier ein kurzer Auszug: es geht umd die Variable cmd.
Die cmd Variable greift auf die CommandsPD4I- Bibliothek zu.
Diese Variable möchte ich in einer cpp Datei (eigene Funktionen) benutzen.
Im moment ist sie nur in Form1.h sichtbar.#pragma once #include <string> #include <iostream> #include "eigeneFunktionen.h" namespace nano { . . using namespace CommandsPD4I; public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Konstruktorcode hier hinzufügen. //Funktion ComMotorCommands übergeben this->cmd = (gcnew ComMotorCommands()); } protected: ..... // Klasse von Nanotec der Variablen übergeben private: ComMotorCommands^ cmd; //Klasse instanzieren .......
Ich habe dann noch ein header eigeneFunktionen.h .
Wie kann ich die cmd-Variable in der eigeneFunktionen.cpp benutzen ?Gruß
-
Hallo vielleicht kam mein problem nicht richtig rüber:
Also es geht um eine motorsteuerung die Variable cmd greift auf die Bibliothek mit deren Funktionen zu.
Ich möchte eine eigene cpp mit den Funktionen erstellen , so wie void starteMotor(...) . Aber die cmd Variable ist nur in Form1.h sichtbar. Was muss ich machen damit ich in der datei eigeneFunktionen.cpp die cmd variable benutzen kann? (cmd->.... )
und sowas geht leider auch nicht:
CommandsPD4I::ComMotorCommands::Baudrate=115200;
error C2761: 'int CommandsPD4I::ComMotorCommands::Baudrate': Die erneute Deklaration der Memberfunktion ist unzulässig.#pragma once #include <string> #include <iostream> #include "eigeneFunktionen.h" namespace nano { 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 CommandsPD4I; public ref class Form1 : public System::Windows::Forms::Form { public: ComMotorCommands^ cmd; Form1(void) { InitializeComponent(); //Funktion ComMotorCommands übergeben this->cmd = (gcnew ComMotorCommands()); } protected: ~Form1() { if (components) { delete components; } } #pragma region Windows Form Designer generated code void InitializeComponent(void) { ......... } #pragma endregion //Hier fängt der eigenltiche Code fürs Formula an //##################################################################################### void starteMotor(int Baudrate_var,int Adresse_var,int Profil_var) { cmd->Baudrate=Baudrate_var; //int cmd->MotorAddresse=Adresse_var; cmd->ChooseRecord(Profil_var); } //############################################################################// //---------manuelles Anfahren-------------------------------------------------// //---------X-Achse------------------------------------------------------------// //---------links-------// private: System::Void button1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) { cmd->SelectedPort=this->comboBox1->Text; //cmd->Baudrate=115200; //int //cmd->MotorAddresse=1; //cmd->ChooseRecord(3); //cmd->StartTravelProfile(); starteMotor(115200,1,3); cmd->StartTravelProfile(); } private: Void button1_MouseUp(Object^sender,Windows::Forms::MouseEventArgs^ e) { cmd->StopTravelProfile(); this->textBox1->Text=Convert::ToString(cmd->GetPosition()); } }; }
Danke für die Hilfe
-
Dann übergib doch auch die Variable 'cmd' als Parameter an die Funktionen.
-
Th69 schrieb:
Dann übergib doch auch die Variable 'cmd' als Parameter an die Funktionen.
mm wie mach ich das? cmd greift doch auf die bibliothek zu.
void starteMotor(????,int Baudrate_var,int Adresse_var,int Profil_var)
-
void starteMotor(ComMotorCommands^ cmd,int Baudrate_var,int Adresse_var,int Profil_var)
-
ok es klappt super:)
void starteMotor(CommandsPD4I::ComMotorCommands^ cmd,int Baudrate_var,int Adresse_var,int Profil_var) { cmd->Baudrate=Baudrate_var; //int cmd->MotorAddresse=Adresse_var; cmd->ChooseRecord(Profil_var); }
-
noch ein anderes Problem Anfängerproblem : wie greif ich auf die Steuerelemente von Form1.h zu . Also in der Funktion.cpp ghet natürlich sowas nicht
this->textBox11->Text=.......und this->textBox11 der Funktion zuübergeben geht nicht.??
-
Übergib die Form1 (oder schöner stelle ein Interface bereit).
Dann implementierst Du in Form1 entsprechende Methoden, z.B. setText(..).Simon