There is a class Source.h

#pragma once #include "logic.h" namespace tetstst { class Sourse{ public: Sourse(); logic player1; }; } 

Source.cpp assigns player1 parameters

 #include "Sourse.h" namespace tetstst { tetstst::Sourse::Sourse() { player1.setName("ARRA"); player1.setScore(20); } } 

The logic class itself looks like this

 #pragma once namespace tetstst { class logic { private: int score; char* name; public: logic(); void setScore(int score); int getScore(); void setName(char*); char* getName(); ~logic(); }; } 

And now to the point. There is a window with a textBox in which you want to display the Score value for player1 . If in the class in which the window elements are created write

 textBox1->Text = player1.getScore(); 

then nothing will come of it. And in this class player1 not available. What should be done?

Code MyForm.h

 #pragma once #include "Sourse.h" namespace tetstst { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Сводка для MyForm /// </summary> public ref class MyForm : public System::Windows::Forms::Form{ public: MyForm(void){ InitializeComponent(); // //TODO: добавьте код конструктора // } protected: /// <summary> /// Освободить все используемые ресурсы. /// </summary> ~MyForm() { if (components) { delete components; } } private: System::Windows::Forms::TextBox^ textBox1; protected: private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; private: /// <summary> /// Обязательная переменная конструктора. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Требуемый метод для поддержки конструктора — не изменяйте /// содержимое этого метода с помощью редактора кода. /// </summary> void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // textBox1 // this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204))); this->textBox1->Location = System::Drawing::Point(104, 52); this->textBox1->Multiline = true; this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(264, 54); this->textBox1->TabIndex = 0; this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Center; this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged); this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox1_KeyPress); // // button1 // this->button1->Location = System::Drawing::Point(104, 125); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(124, 50); this->button1->TabIndex = 1; this->button1->Text = L"+1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(244, 125); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(124, 50); this->button2->TabIndex = 2; this->button2->Text = L"-1"; this->button2->UseVisualStyleBackColor = true; // // MyForm // this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(474, 249); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->textBox1); this->Name = L"MyForm"; this->Text = L"Fucking nigger"; this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { /*textBox1->Text = "123";*/ } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) { } private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) { char number = e->KeyChar; if (!Char::IsDigit(number)){ e->Handled = true; } } }; } "; #pragma once #include "Sourse.h" namespace tetstst { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Сводка для MyForm /// </summary> public ref class MyForm : public System::Windows::Forms::Form{ public: MyForm(void){ InitializeComponent(); // //TODO: добавьте код конструктора // } protected: /// <summary> /// Освободить все используемые ресурсы. /// </summary> ~MyForm() { if (components) { delete components; } } private: System::Windows::Forms::TextBox^ textBox1; protected: private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; private: /// <summary> /// Обязательная переменная конструктора. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Требуемый метод для поддержки конструктора — не изменяйте /// содержимое этого метода с помощью редактора кода. /// </summary> void InitializeComponent(void) { this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // textBox1 // this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204))); this->textBox1->Location = System::Drawing::Point(104, 52); this->textBox1->Multiline = true; this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(264, 54); this->textBox1->TabIndex = 0; this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Center; this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged); this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox1_KeyPress); // // button1 // this->button1->Location = System::Drawing::Point(104, 125); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(124, 50); this->button1->TabIndex = 1; this->button1->Text = L"+1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(244, 125); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(124, 50); this->button2->TabIndex = 2; this->button2->Text = L"-1"; this->button2->UseVisualStyleBackColor = true; // // MyForm // this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(474, 249); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->textBox1); this->Name = L"MyForm"; this->Text = L"Fucking nigger"; this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { /*textBox1->Text = "123";*/ } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) { } private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) { char number = e->KeyChar; if (!Char::IsDigit(number)){ e->Handled = true; } } }; } 
  • And where textBox1 from in C ++? What is it and what type is it? And what type of textBox1->Text ? - VladD
  • Did not quite understand the question, but the textBox was created with the help of the designer - Vitali
  • one
    Well, you have nowhere in the code to declare this textbox itself. Well, I do not quite understand the phrase “with the help of the constructor” - maybe show the code of this constructor itself? (Well, this is the constructor of a class?) - VladD
  • one
    If we are talking about "managed" with ++, player1.getScore () needs to be converted to "string" before assignment - Alexander Muksimov
  • The bottom line is that you cannot get access to player1, he just does not see him - Vitali

1 answer 1

In your case, player1 is a variable of the Sourse class Sourse . To get the variable player1 , you need to create an object of the Sourse class.

Also your classes should be in the form of cli classes.

 public ref class Sourse{ public: Sourse(); logic player1; }; public ref class logic { private: int score; char* name; public: logic(); void setScore(int score); int getScore(); void setName(char*); char* getName(); ~logic(); }; Sourse^ sourse = gcnew Sourse(); textBox1->Text = sourse->player1.getScore(); 
  • I understood the essence, but when adding Sourse sourse = new Sourse(); begins to emphasize as an error source and generate an error "a member of a class that is managed cannot belong to a class that is not managed" - Vitali
  • ah, yes, the class should be special for the CLI, now I’ll correct the answer - Komdosh
  • Something went wrong again in the form of underlined gcnew and the error "there is no suitable user-defined conversion from" tetstst :: Sourse ^ "to" tetstst :: Sourse " - Vitali
  • Well, still corrected, try so, for a long time did not work with cli - Komdosh
  • Earned, thank you: D - Vitali