locked
simple namespace c++ question RRS feed

  • 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

    #pragma once

     

    namespace NamespaceWork {

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

    ///

    public ref class 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;

    #pragma region 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);

    }

    #pragma endregion

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

    CompClass cc1;

    }

    };

    }

     

    CompClass.h

    #pragma once

    using namespace System;

    using namespace System::ComponentModel;

    using namespace System::Collections;

    using namespace System::Diagnostics;

     

    namespace NamespaceWork {

    ///

    /// Summary for CompClass

    ///

    public ref class 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;

    #pragma region 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();

    }

    #pragma endregion

    };

    }

    ref class 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");

    }

     



     

     

     

    Wednesday, May 16, 2007 7:57 PM

Answers

  • 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.
    Wednesday, May 16, 2007 8:09 PM

All replies

  • 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.
    Wednesday, May 16, 2007 8:09 PM
  • 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?
     

    Wednesday, May 16, 2007 8:20 PM
  • Sorry, I was thinking about other things at that time. Try doing what my edited post says.
    Wednesday, May 16, 2007 8:33 PM
  • 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.

    Wednesday, May 16, 2007 8:37 PM