2 Replies - 816 Views - Last Post: 08 February 2017 - 10:20 AM

#1 Putarda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-February 17

Forward declaration is not working

Posted 07 February 2017 - 12:58 PM

Hello :). I'm working on C++/CLI app and I'm having problems with forward declaration. I was reading how compiler works and everything and I still don't get it. Here is my code:

Search.h:

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

namespace SMSALENalozi {

	/// <summary>
	/// Summary for Search
	/// </summary>
	public ref class Search : public System::Windows::Forms::UserControl
	{
	public:
		Search(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			trazi_label->Parent = search_background_picturebox;
			searchby_combobox->SelectedIndex = 0;
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Search()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::PictureBox^  search_background_picturebox;
	protected:

	private: System::Windows::Forms::Label^  trazi_label;
	private: System::Windows::Forms::TextBox^  accountwriting_textbox;
	private: System::Windows::Forms::ComboBox^  searchby_combobox;


	protected:

	private:
		/// <summary>
		/// Required designer variable.
		Control^ window_control;
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Search::typeid));
			this->search_background_picturebox = (gcnew System::Windows::Forms::PictureBox());
			this->trazi_label = (gcnew System::Windows::Forms::Label());
			this->accountwriting_textbox = (gcnew System::Windows::Forms::TextBox());
			this->searchby_combobox = (gcnew System::Windows::Forms::ComboBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->search_background_picturebox))->BeginInit();
			this->SuspendLayout();
			// 
			// search_background_picturebox
			// 
			this->search_background_picturebox->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"search_background_picturebox.Image")));
			this->search_background_picturebox->Location = System::Drawing::Point(0, 0);
			this->search_background_picturebox->Name = L"search_background_picturebox";
			this->search_background_picturebox->Size = System::Drawing::Size(884, 561);
			this->search_background_picturebox->SizeMode = System::Windows::Forms::PictureBoxSizeMode::AutoSize;
			this->search_background_picturebox->TabIndex = 0;
			this->search_background_picturebox->TabStop = false;
			// 
			// trazi_label
			// 
			this->trazi_label->AutoSize = true;
			this->trazi_label->BackColor = System::Drawing::Color::Transparent;
			this->trazi_label->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
				static_cast<System::Byte>(0)));
			this->trazi_label->ForeColor = System::Drawing::Color::White;
			this->trazi_label->Location = System::Drawing::Point(399, 198);
			this->trazi_label->Name = L"trazi_label";
			this->trazi_label->Size = System::Drawing::Size(87, 37);
			this->trazi_label->TabIndex = 0;
			this->trazi_label->Text = L"traži:";
			// 
			// accountwriting_textbox
			// 
			this->accountwriting_textbox->AutoCompleteCustomSource->AddRange(gcnew cli::array< System::String^  >(4) {
				L"FILIP", L"STEFAN",
					L"SALE", L"SLADJA"
			});
			this->accountwriting_textbox->AutoCompleteMode = System::Windows::Forms::AutoCompleteMode::Suggest;
			this->accountwriting_textbox->AutoCompleteSource = System::Windows::Forms::AutoCompleteSource::CustomSource;
			this->accountwriting_textbox->Location = System::Drawing::Point(334, 270);
			this->accountwriting_textbox->Name = L"accountwriting_textbox";
			this->accountwriting_textbox->Size = System::Drawing::Size(216, 20);
			this->accountwriting_textbox->TabIndex = 1;
			this->accountwriting_textbox->PreviewKeyDown += gcnew System::Windows::Forms::PreviewKeyDownEventHandler(this, &Search::accountwriting_textbox_PreviewKeyDown);
			// 
			// searchby_combobox
			// 
			this->searchby_combobox->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
			this->searchby_combobox->FormattingEnabled = true;
			this->searchby_combobox->Items->AddRange(gcnew cli::array< System::Object^  >(4) { L"TABLICA", L"AUTO", L"PREZIME", L"IME" });
			this->searchby_combobox->Location = System::Drawing::Point(558, 270);
			this->searchby_combobox->Name = L"searchby_combobox";
			this->searchby_combobox->Size = System::Drawing::Size(90, 21);
			this->searchby_combobox->TabIndex = 2;
			// 
			// Search
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->Controls->Add(this->searchby_combobox);
			this->Controls->Add(this->accountwriting_textbox);
			this->Controls->Add(this->trazi_label);
			this->Controls->Add(this->search_background_picturebox);
			this->Name = L"Search";
			this->Size = System::Drawing::Size(884, 561);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->search_background_picturebox))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void accountwriting_textbox_PreviewKeyDown(System::Object^  sender, System::Windows::Forms::PreviewKeyDownEventArgs^  e) {

		if (e->KeyCode == Keys::Enter) {

			this->Hide();

		}

	}
};
}


window.h:

#pragma once

namespace SMSALENalozi {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	ref class ProgramItems; //forward declaration

	/// <summary>
	/// Summary for Window
	/// </summary>
	public ref class Window : public System::Windows::Forms::Form
	{
	public:
		Window()
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Window()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::PictureBox^  menu_background_picturebox;
	private: System::Windows::Forms::PictureBox^  logo_picturebox;
	private: System::Windows::Forms::Label^  ulogujse_label;
	private: System::Windows::Forms::TextBox^  passwordwriting_textbox;
	private: System::Windows::Forms::Button^  ulogujse_button;


	protected:

	protected:



	protected:

	protected:

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Window::typeid));
			this->menu_background_picturebox = (gcnew System::Windows::Forms::PictureBox());
			this->logo_picturebox = (gcnew System::Windows::Forms::PictureBox());
			this->ulogujse_label = (gcnew System::Windows::Forms::Label());
			this->passwordwriting_textbox = (gcnew System::Windows::Forms::TextBox());
			this->ulogujse_button = (gcnew System::Windows::Forms::Button());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->menu_background_picturebox))->BeginInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->logo_picturebox))->BeginInit();
			this->SuspendLayout();
			// 
			// menu_background_picturebox
			// 
			this->menu_background_picturebox->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"menu_background_picturebox.Image")));
			this->menu_background_picturebox->Location = System::Drawing::Point(0, 0);
			this->menu_background_picturebox->Name = L"menu_background_picturebox";
			this->menu_background_picturebox->Size = System::Drawing::Size(884, 561);
			this->menu_background_picturebox->SizeMode = System::Windows::Forms::PictureBoxSizeMode::AutoSize;
			this->menu_background_picturebox->TabIndex = 0;
			this->menu_background_picturebox->TabStop = false;
			// 
			// logo_picturebox
			// 
			this->logo_picturebox->BackColor = System::Drawing::Color::Transparent;
			this->logo_picturebox->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"logo_picturebox.Image")));
			this->logo_picturebox->Location = System::Drawing::Point(269, 108);
			this->logo_picturebox->Name = L"logo_picturebox";
			this->logo_picturebox->Size = System::Drawing::Size(346, 206);
			this->logo_picturebox->SizeMode = System::Windows::Forms::PictureBoxSizeMode::AutoSize;
			this->logo_picturebox->TabIndex = 1;
			this->logo_picturebox->TabStop = false;
			// 
			// ulogujse_label
			// 
			this->ulogujse_label->AutoSize = true;
			this->ulogujse_label->BackColor = System::Drawing::Color::Transparent;
			this->ulogujse_label->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
				static_cast<System::Byte>(0)));
			this->ulogujse_label->ForeColor = System::Drawing::Color::White;
			this->ulogujse_label->Location = System::Drawing::Point(390, 414);
			this->ulogujse_label->Name = L"ulogujse_label";
			this->ulogujse_label->Size = System::Drawing::Size(105, 25);
			this->ulogujse_label->TabIndex = 2;
			this->ulogujse_label->Text = L"uloguj se:";
			// 
			// passwordwriting_textbox
			// 
			this->passwordwriting_textbox->Location = System::Drawing::Point(334, 450);
			this->passwordwriting_textbox->Name = L"passwordwriting_textbox";
			this->passwordwriting_textbox->Size = System::Drawing::Size(216, 20);
			this->passwordwriting_textbox->TabIndex = 3;
			this->passwordwriting_textbox->UseSystemPasswordChar = true;
			this->passwordwriting_textbox->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Window::passwordwriting_textbox_KeyPress);
			// 
			// ulogujse_button
			// 
			this->ulogujse_button->Location = System::Drawing::Point(406, 486);
			this->ulogujse_button->Name = L"ulogujse_button";
			this->ulogujse_button->Size = System::Drawing::Size(72, 36);
			this->ulogujse_button->TabIndex = 4;
			this->ulogujse_button->Text = L"uloguj se";
			this->ulogujse_button->UseVisualStyleBackColor = true;
			// 
			// Window
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::White;
			this->ClientSize = System::Drawing::Size(884, 561);
			this->Controls->Add(this->ulogujse_button);
			this->Controls->Add(this->passwordwriting_textbox);
			this->Controls->Add(this->ulogujse_label);
			this->Controls->Add(this->logo_picturebox);
			this->Controls->Add(this->menu_background_picturebox);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
			this->MaximizeBox = false;
			this->Name = L"SMSALENalozi";
			this->Text = L"SMSALE Nalozi";
			this->Load += gcnew System::EventHandler(this, &Window::Window_Load);
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->menu_background_picturebox))->EndInit();
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->logo_picturebox))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Window_Load(System::Object^  sender, System::EventArgs^  e) {

		logo_picturebox->Parent = menu_background_picturebox;

		ulogujse_label->Parent = menu_background_picturebox;

	}
	private: System::Void passwordwriting_textbox_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {

		if (e->KeyChar == (char)13) {

			if (passwordwriting_textbox->Text == "KRASTA") {

				ProgramItems::test; //makes error every time I add this. LINE 167

				Search ^s = gcnew Search();

				this->Controls->Clear();

				this->Controls->Add(s);

			}

		}

	}
};
}


Program.h:

#pragma once

#include "Search.h"
#include "window.h"

namespace SMSALENalozi {

	ref class ProgramItems {

	public:

		static int test;

		static SMSALENalozi::Window ^window;

		static SMSALENalozi::Search ^search;

	};

}


Program.cpp (start point):

#include "Program.h"

using namespace System;
using namespace System::Windows::Forms;

[STAThread]
int main() {

	Application::EnableVisualStyles();
	
	Application::SetCompatibleTextRenderingDefault(false);

	SMSALENalozi::ProgramItems::search = gcnew SMSALENalozi::Search();

	SMSALENalozi::ProgramItems::window = gcnew SMSALENalozi::Window();

	Application::Run(SMSALENalozi::ProgramItems::window);

	return 0;
}


and the errors are:
C2027 use of undefined type 'SMSALENalozi::ProgramItems' File: window.h

C2065 'test': undeclared identifier File: window.h

Thanks :).

Is This A Good Question/Topic? 0
  • +

Replies To: Forward declaration is not working

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: Forward declaration is not working

Posted 07 February 2017 - 03:50 PM

Moving to C++/CLI...
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: Forward declaration is not working

Posted 08 February 2017 - 10:20 AM

With forward declarations of classes, you can only use pointers or references to them. You cannot actually access their contents or instantiate them without actually declaring them.

Part of the issue you are running into is that you are trying to do too much with your header files. It feels like you are trying to transliterate C# or Java code to C++/CLI and so you are ending up putting implementations into headers. Don't do this. Headers are only meant to tell people about the general shape or "interface" of your class. You are trying to put in the details or "implementation" within the headers. In general, you don't do this in C++ except for template classes. The convention is to put the implementation in .cpp files.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1