Jump to content

Classes in forms! (c++)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts
Hello, I have done some console programming in C++ and thought i should give the looks some love in my program, but I have a problem, I have my Source file that look like this:



#include "stdafx.h"

#include "Form1.h"


using namespace test;



class DoStuff{

	int nr1, nr2;

public: 

	void SetStuff(System::Windows::Forms::Button a);

};


void DoStuff::SetStuff(System::Windows::Forms::Button a) {

	a.Text = "text";

}


[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;

}

and my form that looks like this (just done a button and dubbleklicked it to get this function)...


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

	DoStuff d;

	d.SetStuff(this->button1);	 

}


But I get the following errors,

Error 1 error C2065: 'DoStuff' : undeclared identifier Form1.h
Error 2 error C2146: syntax error : missing ';' before identifier 'd' Form1.h
Error 3 error C2065: 'd' : undeclared identifier Form1.h
Error 4 error C2065: 'd' : undeclared identifier Form1.h
Error 5 error C2228: left of '.SetStuff' must have class/struct/union Form1.h

I guess I make something wrong when initializing my Class in the other file or something like that, would really like some help :)

/nick3

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What is Form1.h? That is where all your errors are occurring.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts
That is the form (I started a Windows forms application), it was autogenerated and i only added that function

	


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

		DoStuff d;

		d.SetStuff(this->button1);	 

}

the rest was as I said autogenerated, looking like this thou if its any help


#pragma once



namespace test {


	using namespace System;

	using namespace System::ComponentModel;

	using namespace System::Collections;

	using namespace System::Windows::Forms;

	using namespace System::Data;

	using namespace System::Drawing;

	using namespace test;


	/// <summary>

	/// Summary for Form1

	///

	/// WARNING: If you change the name of this class, you will need to change the

	///          'Resource File Name' property for the managed resource compiler tool

	///          associated with all .resx files this class depends on.  Otherwise,

	///          the designers will not be able to interact properly with localized

	///          resources associated with this form.

	/// </summary>

	


	public ref class Form1 : public System::Windows::Forms::Form

	{

	public:

		Form1(void)

		{

			InitializeComponent();

			//

			//TODO: Add the constructor code here

			//

		}


	protected:

		/// <summary>

		/// Clean up any resources being used.

		/// </summary>

		~Form1()

		{

			if (components)

			{

				delete components;

			}

		}

	private: System::Windows::Forms::Button^  button1;

	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)

		{

			this->button1 = (gcnew System::Windows::Forms::Button());

			this->SuspendLayout();

			// 

			// button1

			// 

			this->button1->Location = System::Drawing::Point(109, 214);

			this->button1->Name = L"button1";

			this->button1->Size = System::Drawing::Size(75, 23);

			this->button1->TabIndex = 0;

			this->button1->Text = L"button1";

			this->button1->UseVisualStyleBackColor = true;

			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

			// 

			// Form1

			// 

			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

			this->ClientSize = System::Drawing::Size(292, 266);

			this->Controls->Add(this->button1);

			this->Name = L"Form1";

			this->Text = L"Form1";

			this->ResumeLayout(false);

			

		}

#pragma endregion

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

		DoStuff d;

		d.SetStuff(this->button1);	 

	}

	};

}




#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I think your problem is that you are attempting to use DoStuff before it has been declared. I would recommend moving class DoStuff into its own header and using a #include to bring it into Form1.h.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
nick3

nick3

    Newbie

  • Members
  • PipPip
  • 29 posts
Yea ok, thanks a lot, will do this. I have been working in mostly java latley so not really used to use the .h-files. Should i put ALL classes in a header file (like a classfile in java) or should I put all classes in the same file?

/Nick3

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Generally, each class declaration gets its own header, with the class definition in a corresponding .cpp file. Then you include headers in each file that needs to know about the class, and the .cpp files are linked together when you compile.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog