Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I have a class I wrote in Visual Studio 2010 C++/CLI. I'm trying to get the class to be known by the main form. I get the error when I compile:

error C2065: 'Simulate' : undeclared identifier

The class looks something like this:

Simulate is the class I created in a file separate from the main form:

C++
public ref class Simulate // In simulate.h
{
  // Initialize here
}

and the form looks something like:
C++
public ref class Form1
{
  // The rest of the form stuff

  private: System::void button1_click()
   {
     Simulate ^simEvent = gcnew Simulate; <<<==== This is getting the error
     // More code.
   }
};
Posted
Updated 25-Jun-11 15:35pm
v3

You must include the file before using a type defined inside it. In C++/CLI, you must include files as in standard C++ for any type inside the current project.

#include "simulate.h"
 
Share this answer
 
If you need C++ code for Simulate class (maybe you want to uses existing C++ code), one solution would be to have a DLL project for that code (no UI in that DLL) and have the an application written in C# that reference that C++/CLI DLL and do the UI stuff in C#.
 
Share this answer
 
Well, in the first code snippet, a ; is missing after the class declaration
public ref class Simulate 
{ 
    /* code here */ 
};  // <-- missing ; in sample code above.

In this is the case, then the compiler would try to append to the declaration whatever it find afterward...

Next, you will have to ensure that the file is indeed include using
#error testing inclusion...

A file might not be include is a file similary named exists elsewhere and it picked up. Also, it might happen when include guard are uses to prevent multiple inclusion of the same file. A common case where this is a problem is when a file is copied from another one and include guard are not properly updated. This problem is eliminated is you uses #pragma once. Include guard are something like:

simulate.h
#ifndef SIMULATE_H     // Might be problematic if incorrect (after cut & paste)
#define SIMULATE_H

public ref class Simulate // In simulate.h
{
  // Initialize here
};

#endif


It is easier to replace those 3 extra lines with #pragma once when the code does not need to be portable on a compiler that does not support it.

Another thing to check is which source file is being compiled when the error occurs. Since the missing reference is in "Form1.h" one can assume that the error was occuring while compiling "Form1.cpp" but if the file is included from multiple location this might not be the case. And if the #include is not inside "Form1.h" and the other source file that uses "Form1.h" does not uses precompiled headers (does not include "stdafx.h" or options set to use them), then it can go wrong.

The best way to find such problem is to do a "clean" the compile "stdafx.cpp" and then compile each file one by one (starting by the suspect ones like "Form1.cpp"). Doing that (and also with the #error trick), you should be able to find what is done incorrectly.

Renaming files and/or class to see if the problem persist might help to identify possible conflict. Or in some case move code around or comment out some blocks of code. Often it is a small mistake that we simply does not see. All those tricks help to find the cause when we don't see it.
 
Share this answer
 
v2
Here is the complete program with all the files. The project as it is compiles and links with no errors. If you enable the line where noted, I get the error:
"error C2065: 'Simulator' : undeclared identifier" on that line.

*****************
Form1.h
*****************

#pragma once

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

public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}

protected:
~Form1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::ListBox^ listBox1;
protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;

private:
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// &lt;summary&gt;
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// &lt;/summary&gt;
void InitializeComponent(void)
{
this-&gt;button1 = (gcnew System::Windows::Forms::Button());
this-&gt;button2 = (gcnew System::Windows::Forms::Button());
this-&gt;SuspendLayout();
this-&gt;listBox1 = (gcnew System::Windows::Forms::ListBox());
//
// listBox1
//
this-&gt;listBox1-&gt;FormattingEnabled = true;
this-&gt;listBox1-&gt;Location = System::Drawing::Point(22, 27);
this-&gt;listBox1-&gt;Name = L"listBox1";
this-&gt;listBox1-&gt;Size = System::Drawing::Size(250, 121);
this-&gt;listBox1-&gt;TabIndex = 0;
//
// button1
//
this-&gt;button1-&gt;Location = System::Drawing::Point(197, 172);
this-&gt;button1-&gt;Name = L"button1";
this-&gt;button1-&gt;Size = System::Drawing::Size(75, 23);
this-&gt;button1-&gt;TabIndex = 1;
this-&gt;button1-&gt;Text = L"Exit";
this-&gt;button1-&gt;UseVisualStyleBackColor = true;
this-&gt;button1-&gt;Click += gcnew System::EventHandler(this, &amp;Form1::button1_Click);
//
// button2
//
this-&gt;button2-&gt;Location = System::Drawing::Point(22, 170);
this-&gt;button2-&gt;Name = L"button2";
this-&gt;button2-&gt;Size = System::Drawing::Size(68, 23);
this-&gt;button2-&gt;TabIndex = 2;
this-&gt;button2-&gt;Text = L"Start";
this-&gt;button2-&gt;UseVisualStyleBackColor = true;
this-&gt;button2-&gt;Click += gcnew System::EventHandler(this, &amp;Form1::button2_Click);
//
// Form1
//
this-&gt;AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this-&gt;AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this-&gt;ClientSize = System::Drawing::Size(284, 205);
this-&gt;Controls-&gt;Add(this-&gt;button2);
this-&gt;Controls-&gt;Add(this-&gt;button1);
this-&gt;Controls-&gt;Add(this-&gt;listBox1);
this-&gt;Name = L"Form1";
this-&gt;StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this-&gt;Text = L"Form1";
this-&gt;ResumeLayout(false);
}
#pragma endregion

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
// Simulator ^simClass = gcnew Simulator(); <<== FAILS HERE
}

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

*****************
Simulator.h
*****************
#pragma once
#include "Form1.h"

public ref class Simulator
{
public:
Simulator(MySimulator::Form1 ^);

public:
void
setup(MySimulator::Form1 ^);
};


*****************
Simulator.cpp
*****************
#include "stdafx.h"
#include "Simulator.h"
#include "Form1.h"

Simulator::Simulator(MySimulator::Form1 ^)
{
// Do nothing here unless there are variables to initialize.
}

void Simulator::setup(MySimulator::Form1 ^frmInstance)
{
frmInstance-&gt;listBox1-&gt;Items-&gt;Add("Starting setup.");
}

*****************
stdafx.h
*****************
#pragma once
#include "Form1.h"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900