vc++ global nesne tanımlama - nasıl ?

Başlatan magnetron, 23 Temmuz 2013, 19:33:05

magnetron

merhaba forum,

projemde bir form oluşturdum ve bu forma bir buton ve winsock kontrol ekledim
şimdi başka bir *.cpp dosyasında bu class'ın bir nesnesini oluşturmak istiyorum

mesela aşağıdaki kod örneğinde olduğu gibi

#include "MOCON1.h"  // MOCON clasın tanımlandığı dosya 
......
MOCON * winsok ;  // 6.ncı satır


ama aşağıdaki hatayı veriyor

1>Compiling...
1>MyHardware.cpp
1>.\MyHardware.cpp(6) : error C2143: syntax error : missing ';' before '*'
1>.\MyHardware.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\MyHardware.cpp(6) : error C2365: 'MOCON' : redefinition; previous definition was 'namespace'
1>.\MyHardware.cpp(6) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


neden hata veriyor ? ne yapmalıyım ? teşekkür
MOCON1.h aşağıda
#pragma once

#include "stdafx.h"
#include "math.h"

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 MOCON {

	/// <summary>
	/// Summary for MOCON
	///
	/// 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 MOCON : public System::Windows::Forms::Form
	{
	public:
		MOCON(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MOCON()
		{
			if (components)
			{
				delete components;
			}
		}
	public /*private*/: AxMSWinsockLib::AxWinsock^  axWinsock1;
	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)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MOCON::typeid));
			this->axWinsock1 = (gcnew AxMSWinsockLib::AxWinsock());
			this->button1 = (gcnew System::Windows::Forms::Button());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->axWinsock1))->BeginInit();
			this->SuspendLayout();
			// 
			// axWinsock1
			// 
			this->axWinsock1->Enabled = true;
			this->axWinsock1->Location = System::Drawing::Point(165, 124);
			this->axWinsock1->Name = L"axWinsock1";
			this->axWinsock1->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^  >(resources->GetObject(L"axWinsock1.OcxState")));
			this->axWinsock1->Size = System::Drawing::Size(28, 28);
			this->axWinsock1->TabIndex = 0;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(61, 59);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(91, 32);
			this->button1->TabIndex = 1;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &MOCON::button1_Click);
			// 
			// MOCON
			// 
			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->Controls->Add(this->axWinsock1);
			this->Name = L"MOCON";
			this->Text = L"MOCON";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->axWinsock1))->EndInit();
			this->ResumeLayout(false);

		}
#pragma endregion

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 //strncpy(&st3[0],this->axWinsock1->LocalIP,15);
			 }
	};
}

fatih6761

Hocam namespace ile class adı aynı olduğundan hata veriyor. "MOCON" namespace adını değiştirip bir dener misiniz?

magnetron

hocam class ismini değiştirdim

ve classın tanımlamasına hemen bir nesne tanımladım

#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 MOCON {

	/// <summary>
	/// Summary for MOTION
	///
	/// 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 MOTION : public System::Windows::Forms::Form
	{
	public:
		MOTION(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MOTION()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	private: AxMSWinsockLib::AxWinsock^  axWinsock1;
	private: System::Windows::Forms::TextBox^  textBox1;
	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(MOTION::typeid));
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->axWinsock1 = (gcnew AxMSWinsockLib::AxWinsock());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->axWinsock1))->BeginInit();
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(64, 68);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(125, 33);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			// 
			// axWinsock1
			// 
			this->axWinsock1->Enabled = true;
			this->axWinsock1->Location = System::Drawing::Point(161, 141);
			this->axWinsock1->Name = L"axWinsock1";
			this->axWinsock1->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^  >(resources->GetObject(L"axWinsock1.OcxState")));
			this->axWinsock1->Size = System::Drawing::Size(28, 28);
			this->axWinsock1->TabIndex = 1;
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(62, 198);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(126, 20);
			this->textBox1->TabIndex = 2;
			// 
			// MOTION
			// 
			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->textBox1);
			this->Controls->Add(this->axWinsock1);
			this->Controls->Add(this->button1);
			this->Name = L"MOTION";
			this->Text = L"MOTION";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->axWinsock1))->EndInit();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	} *winsok;     // burada hemen nesne tanımlanıyor 
}


bunu compile ettiğimde

1>Compiling...
1>MOTION.cpp
1>c:\documents and settings\xp\belgelerim\visual studio 2008\projects\mocon\mocon\MOTION.h(110) : error C3699: '*' : cannot use this indirection on type 'MOCON::MOTION'
1>        compiler replacing '*' with '^' to continue parsing
1>c:\documents and settings\xp\belgelerim\visual studio 2008\projects\mocon\mocon\MOTION.h(110) : error C3145: 'winsok' : global or static variable may not have managed type 'MOCON::MOTION ^'
1>        may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap


bu hatayı veriyor

eğer

#pragma endregion
	} ^winsok;     // burada hemen nesne tanımlanıyor


bu şekilde tanımlarsam

1>Compiling...
1>MOTION.cpp
1>c:\documents and settings\xp\belgelerim\visual studio 2008\projects\mocon\mocon\MOTION.h(110) : error C3145: 'winsok' : global or static variable may not have managed type 'MOCON::MOTION ^'
1>        may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap


bu hatayı veriyor

bu C++ ne kadar karışıkmış C gibi güzel dil yok yani

bir yardım , teşekkür