Answered by:
C++\CLR error C2248: 'test1::MyForm::textBox1' : cannot access private member declared in class

Question
-
Hi
i use visual studio 2012. i have 2 problems
1) why my string not get values from textbox2?
2)this -> textBox1->Text = nip; // error C2248: 'test1::MyForm::textBox1' : cannot access private member declared in class 'test1::MyForm'
my testz.cpp
#include "sdr.h" #include <iostream> #include <string> #include <sstream> #include "MyForm.h" using namespace std; using namespace System; using namespace System::Data; using namespace test1; public ref class testq : public MyForm { public: // testq(){} bool testx() { System::String^ nip = textBox2->Text; this -> textBox1->Text = nip;//error C2248: 'test1::MyForm::textBox1' : cannot access private member declared in class 'test1::MyForm' return 0; } private: }; bool MyForm::testa() { testq xc; xc.test(); // any code // // return 0; }
how to fix what?//and from MyForm.h
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { testa(); }
thanks.....
- Edited by d22xxxx Monday, October 27, 2014 2:09 PM
Monday, October 27, 2014 2:08 PM
Answers
-
hi again any idea why not get values my string nip from textbox2?
When you do something like
bool MyForm::testa() { testq xc; xc.testx(); // ... return 0; }
you are creating a new testq object xc that has nothing to do with your form that has some value in TestBox2. If you ran your code in the debugger (as you should when something like this happens), you would find that 'nip' is empty. You need to be sure you understand the difference between classes and objects.
What are you really trying to do here? What is the purpose of deriving testq from MyForm? Whatever it is, you probably want to create a testq object in main(), not a MyForm object.
Then if you want testa to be a member function of MyForm, you might consider making testx a virtual function in MyForm, implemented in testq. Then you could do
bool MyForm::testa() { testx(); // calls testq::testx // ... return 0; }
Your notations test, testx, testa, testq are very confusing [test and testx I think are actually the same; you have a typo here.] Good naming is very important.
David Wilkinson | Visual C++ MVP
- Proposed as answer by pvdg42MVP Tuesday, October 28, 2014 1:12 PM
- Edited by davewilkMVP Tuesday, October 28, 2014 3:25 PM
- Marked as answer by Shu HuModerator Tuesday, November 4, 2014 12:04 PM
Tuesday, October 28, 2014 11:39 AM
All replies
-
Well, from what you have provided the answer to #1 is -- your code didn't compile, so of course it didn't do what you wanted.
#2: Find the class test1::MyForm and look at the code. Somewhere there is a variable called textBox1 declared. That variable is private. Make it public. There is a good chance that the next line in the output window after your error message will tell you exactly where textBox1 is declared.
- Proposed as answer by pvdg42MVP Tuesday, October 28, 2014 1:12 PM
Monday, October 27, 2014 2:29 PM -
thanks for replay me
#2:problem is ok: public: System::Windows::Forms::TextBox^ textBox1;
my code compile now and run my application ok.
i write in textbox2: test!!!
but not show my textbox1,
System::String^ nip = textBox2->Text;
my String nip not get values from textbox2 i check with IDE
************************************************
public ref class testq : public MyForm { public: // testq(){} bool testx() { System::String^ nip = textBox2->Text; textBox1->Text = nip; return 0; } private: };
Monday, October 27, 2014 3:03 PM -
hi again any idea why not get values my string nip from textbox2?Tuesday, October 28, 2014 9:34 AM
-
hi again any idea why not get values my string nip from textbox2?
When you do something like
bool MyForm::testa() { testq xc; xc.testx(); // ... return 0; }
you are creating a new testq object xc that has nothing to do with your form that has some value in TestBox2. If you ran your code in the debugger (as you should when something like this happens), you would find that 'nip' is empty. You need to be sure you understand the difference between classes and objects.
What are you really trying to do here? What is the purpose of deriving testq from MyForm? Whatever it is, you probably want to create a testq object in main(), not a MyForm object.
Then if you want testa to be a member function of MyForm, you might consider making testx a virtual function in MyForm, implemented in testq. Then you could do
bool MyForm::testa() { testx(); // calls testq::testx // ... return 0; }
Your notations test, testx, testa, testq are very confusing [test and testx I think are actually the same; you have a typo here.] Good naming is very important.
David Wilkinson | Visual C++ MVP
- Proposed as answer by pvdg42MVP Tuesday, October 28, 2014 1:12 PM
- Edited by davewilkMVP Tuesday, October 28, 2014 3:25 PM
- Marked as answer by Shu HuModerator Tuesday, November 4, 2014 12:04 PM
Tuesday, October 28, 2014 11:39 AM -
big thanks David!!!Tuesday, October 28, 2014 9:41 PM