0

I have implemented following in Windows Form C++ project

template<class T> class MyQueue
{
    T *m_data;
    int m_numElements;

public:
    MyQueue() : m_data(NULL), m_numElements(0) { }

  ..... code .....

};

MyQueue<char> logData; // I need to acces it from Form1.h

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{

    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    Application::Run(gcnew Form1());

    return 0;
}

I would like to access it within Form1.h under the

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
{
  ??? MyQueue<char> logData; // I need to acces it 
}

Any clue?

NoWar
  • 36,338
  • 80
  • 323
  • 498

2 Answers2

1

You could declare logdata static. Accessing raw data members is not normally considered good practice, so you may also want to provide a static method to put characters into your queue. Here's a tutorial on static members in C++.

http://www.tutorialspoint.com/cplusplus/cpp_static_members.htm

  • I am sorry I am new in C++ would you mind to provide a little bit more extended answer please – NoWar Nov 11 '15 at 17:42
1

Outside of the Form1 class, provide

extern MyQueue<char> logData;

Inside of the Form1_Load function definition, just access it:

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
    logData.pop(); // Access it 
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I have got several erros when I do this. Probably because `template class MyQueue` is implemented in a `main` cpp class. Let me how it could be done properly, please. – NoWar Nov 11 '15 at 17:52
  • @dimi See [here](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) please, regarding implementation of `MyQueue` please. You probably did this wrong. – πάντα ῥεῖ Nov 11 '15 at 17:54