winforms - Can't access global variables -
in globaldata.h defining typedef struct globaldata. in main cpp program, i'm including globaldata.h, declaring
globaldata globaldata;
after declaration, include createnetdialog.h , gui.h. why can reference globaldata in gui.h not createnetdialog.h? both include files should pasted .cpp file during precompile, globaldata should visible in both right?
i have globaldata structure defined here:
globaldata.h
typedef struct { string netfilename; datafilereader *datafilereader; } globaldata;
in c++ code, have:
gui.cpp
#include <cstdlib> #include <iostream> #include <fstream> #include <string> #include "datafilereader.h" #include "globaldata.h" globaldata globaldata; #include "createnetdialog.h" #include "gui.h" using namespace system; using namespace system::windows::forms; using namespace std; [stathread] void main(array<string^>^ args) { application::enablevisualstyles(); application::setcompatibletextrenderingdefault(false); carnac::gui formmain; application::run(%formmain); }
createnetdialog.h (in createnetdialog constructor)
#pragma once namespace carnac { 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> /// summary createnetdialog /// </summary> public ref class createnetdialog : public system::windows::forms::form { public: createnetdialog(void) { initializecomponent(); // left of '.size' must have class/struct/union // left of '.datafilereader' must have class/struct/union // 'globaldata':undeclared identifier despite being declared right before // file included in .cpp file (unsigned int i=0; i<globaldata.datafilereader->_headeritems.size(); i++) { // } } protected: /// <summary> /// clean resources being used. /// </summary> ~createnetdialog() { if (components) { delete components; } } protected: protected: private: system::windows::forms::button^ buttoncreate; private: system::windows::forms::button^ buttoncreatecancel; private: system::componentmodel::container ^components; #pragma region windows form designer generated code void initializecomponent(void) { this->buttoncreate = (gcnew system::windows::forms::button()); this->buttoncreatecancel = (gcnew system::windows::forms::button()); this->suspendlayout(); // // buttoncreate // this->buttoncreate->anchor = static_cast<system::windows::forms::anchorstyles>((system::windows::forms::anchorstyles::bottom | system::windows::forms::anchorstyles::left)); this->buttoncreate->location = system::drawing::point(16, 288); this->buttoncreate->name = l"buttoncreate"; this->buttoncreate->size = system::drawing::size(75, 23); this->buttoncreate->tabindex = 7; this->buttoncreate->text = l"create"; this->buttoncreate->usevisualstylebackcolor = true; // // buttoncreatecancel // this->buttoncreatecancel->anchor = static_cast<system::windows::forms::anchorstyles>((system::windows::forms::anchorstyles::bottom | system::windows::forms::anchorstyles::right)); this->buttoncreatecancel->location = system::drawing::point(404, 288); this->buttoncreatecancel->name = l"buttoncreatecancel"; this->buttoncreatecancel->size = system::drawing::size(75, 23); this->buttoncreatecancel->tabindex = 8; this->buttoncreatecancel->text = l"cancel"; this->buttoncreatecancel->usevisualstylebackcolor = true; this->buttoncreatecancel->click += gcnew system::eventhandler(this, &createnetdialog::buttoncreatecancel_click); // // createnetdialog // this->autoscaledimensions = system::drawing::sizef(6, 13); this->autoscalemode = system::windows::forms::autoscalemode::font; this->clientsize = system::drawing::size(491, 323); this->controls->add(this->buttoncreatecancel); this->controls->add(this->buttoncreate); this->name = l"createdialog"; this->text = l"createdialog"; this->resumelayout(false); this->performlayout(); } #pragma endregion private: system::void buttoncreatecancel_click(system::object^ sender, system::eventargs^ e) { this->close(); } }; }
gui.h (at end)
#pragma once #include <msclr\marshal_cppstd.h> namespace carnac { 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> /// summary gui /// </summary> public ref class gui : public system::windows::forms::form { public: gui(void) { initializecomponent(); // //todo: add constructor code here // } protected: /// <summary> /// clean resources being used. /// </summary> ~gui() { if (components) { delete components; } } protected: private: int _datacolumnselected; private: system::windows::forms::button^ buttonload; private: /// <summary> /// required designer variable. /// </summary> system::componentmodel::container ^components; #pragma region windows form designer generated code /// <summary> /// required method designer support - not modify /// contents of method code editor. /// </summary> void initializecomponent(void) { this->buttonload = (gcnew system::windows::forms::button()); this->suspendlayout(); // // buttonload // this->buttonload->anchor = static_cast<system::windows::forms::anchorstyles>((system::windows::forms::anchorstyles::bottom | system::windows::forms::anchorstyles::left)); this->buttonload->text = l"load data"; this->buttonload->usevisualstylebackcolor = true; this->buttonload->click += gcnew system::eventhandler(this, &gui::buttonload_click); this->buttonload->location = system::drawing::point(67, 66); this->buttonload->name = l"buttonload"; this->buttonload->size = system::drawing::size(75, 23); this->buttonload->tabindex = 0; this->buttonload->usevisualstylebackcolor = true; // // gui // this->autoscaledimensions = system::drawing::sizef(6, 13); this->autoscalemode = system::windows::forms::autoscalemode::font; this->backcolor = system::drawing::systemcolors::appworkspace; this->clientsize = system::drawing::size(234, 171); this->controls->add(this->buttonload); this->name = l"gui"; this->text = l"carnac"; this->resumelayout(false); } #pragma endregion static std::string tostandardstring(system::string^ string) { using system::runtime::interopservices::marshal; system::intptr pointer = marshal::stringtohglobalansi(string); char* charpointer = reinterpret_cast<char*>(pointer.topointer()); std::string returnstring(charpointer, string->length); marshal::freehglobal(pointer); return returnstring; } private: system::void buttonpredictrun_click(system::object^ sender, system::eventargs^ e) { } private: system::void buttontestload_click(system::object^ sender, system::eventargs^ e) { } private: system::void buttonload_click(system::object^ sender, system::eventargs^ e) { openfiledialog^ openfiledialog1 = gcnew openfiledialog; openfiledialog1->filter = "data files|*.dat"; if (openfiledialog1->showdialog() == system::windows::forms::dialogresult::ok) { ifstream infile; string^ strfilename = openfiledialog1->initialdirectory + openfiledialog1->filename; // works // why can use globaldata here not in createnetdialog.h? globaldata.datafilereader = new datafilereader(tostandardstring(strfilename)); } } }; }
thanks help
yes, globaldata visible in both gui.h , createnetdialog.h, when compiled gui.cpp. in globaldata.h have defined type, not global variable of type. when compiled other cpp file, gui.h , createnetdialog.h can't see declaration of global variable in gui.cpp.
here's want do: declare both type, , extern
global variable in globaldata.h. include globaldata.h every file want use in, don't rely on other files have included before 1 want use in. in just one cpp file (currently gui.cpp), declare actual global variable.
// globaldata.h #include "datafilereader.h" typedef struct { string netfilename; datafilereader *datafilereader; } globaldata; extern globaldata globaldata;
.
// every file use globaldata #include "globaldata.h"
.
// gui.cpp #include "globaldata.h" globaldata globaldata;
Comments
Post a Comment