simple namespace c++ question

i've created a C++ windows form project. i've added a c++ class(CompClass.cpp) to the same namespace where Form1.cpp exists. In the ClassView i can see both of these classes.

i've added a button control to the form1 and double-click it on design time.

Button Clicking code

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

CompClass cc1;

}

};

As you see I wanted to define cc1 variable with the type of CompClass but when i build the solution following errors identified for the same line:

Error 1 error C2065: 'CompClass' : undeclared identifier c:\..Form1.h

Error 2 error C2146: syntax error : missing ';' before identifier 'cc1' c:\..Form1.h 86

Error 3 error C2065: 'cc1' : undeclared identifier c:\..\Form1.h 86

Below are the codes of Form1.h, CompClass.h, CompClass.cpp

Form1.h

#pragmaonce

namespace NamespaceWork {

usingnamespace System;

usingnamespace System::ComponentModel;

usingnamespace System::Collections;

usingnamespace System::Windows::Forms;

usingnamespace System::Data;

usingnamespace System::Drawing;

///

/// 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.

///

publicrefclass Form1 :public System::Windows::Forms::Form

{

public:

Form1(void)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

protected:

///

/// Clean up any resources being used.

///

~Form1()

{

if (components)

{

delete components;

}

}

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

protected:

private:

///

/// Required designer variable.

///

System::ComponentModel::Container ^components;

#pragmaregion Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

void InitializeComponent(void)

{

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

this->SuspendLayout();

//

// button1

//

this->button1->Location = System::Drawing::Point(110, 72);

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

}

#pragmaendregion

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

CompClass cc1;

}

};

}

CompClass.h

#pragmaonce

usingnamespace System;

usingnamespace System::ComponentModel;

usingnamespace System::Collections;

usingnamespace System::Diagnostics;

namespace NamespaceWork {

///

/// Summary for CompClass

///

publicrefclass CompClass :public System::ComponentModel::Component

{

public:

CompClass(void)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

CompClass(System::ComponentModel::IContainer ^container)

{

///

/// Required for Windows.Forms Class Composition Designer support

///

container->Add(this);

InitializeComponent();

}

void PrintToScreen(void)

{

}

protected:

///

/// Clean up any resources being used.

///

~CompClass()

{

if (components)

{

delete components;

}

}

private:

///

/// Required designer variable.

///

System::ComponentModel::Container ^components;

#pragmaregion Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

void InitializeComponent(void)

{

components =gcnew System::ComponentModel::Container();

}

#pragmaendregion

};

}

refclass CompClass

{

public:

CompClass(void);

void PrintToScreen(void);

};

CompClass.cpp

#include"StdAfx.h"

#include"CompClass.h"

CompClass::CompClass(void)

{

Console::WriteLine("serdar");

}

void CompClass::PrintToScreen(void)

{

Console::WriteLine("Printed to screen");

}

[15596 byte] By [asilter] at [2008-1-3]
# 1
This means that to Form1 there is no previous definition of CompClass.
Even though there is metadata checking, in the C++ compiler it seems to work a little differently.
Anyway, before your Form1 include the CompClass header file.

Edit

Thinking about it there is possibly a better way of doing this by avoiding multiple definitions. Using the using preprocessor directive you can point it to the object file created from CompClass. The only problem with this method is you must make sure CompClass.cpp is compiled before the rest. You may have to resort to manual building to get this right.

crescens2k at 2007-9-25 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2

When I did the first thing you say, i get the error as addition to others i've mentioned(errors become 4).

Error 1 error C2039: 'CompClass' : is not a member of 'NamespaceWork' c:\..Form1.h 87

When I did the second thing you said, i get those 3 errors i've mentioned.

And also both .h files are in the same namespace why don't one see the other?

asilter at 2007-9-25 > top of Msdn Tech,Visual C++,Visual C++ General...
# 3
Sorry, I was thinking about other things at that time. Try doing what my edited post says.
crescens2k at 2007-9-25 > top of Msdn Tech,Visual C++,Visual C++ General...
# 4

i'm a c#.net programmer and i have to get used to c++.net quickly Smile

thanx for including .h file advise. it worked.

asilter at 2007-9-25 > top of Msdn Tech,Visual C++,Visual C++ General...