Why this is failing to compile.

J

Jack

I'm new to Visual C++ for .NET and during learning I
encountered the following:

In Form1.h file inside class Form1 following the book
example I defined fuction that looks like this:

(file Form1.h)

void function()
{
... some code ...
MessageBox::Show(....);
... some code ...
};

This compiled and worked OK. But because I'm from the old
world of C programming I wanted to move body of the
function to the Form1.cpp file as follows:

(file Form1.h)

void function();

(file Form1.cpp)

#include "Form1.h"

.... some code ...

void Form1::function()
{
... some code ...
MessageBox::Show(...);
... some sode ...
}

The above failed to compile. It was complaining about
MessageBox being neither class nor namespace.

Why it did not compile ?

Jack
 
J

Jon Sturgeon

The above failed to compile. It was complaining about
MessageBox being neither class nor namespace.

Before you can reference "MessageBox" (which I assume is one of your
classes?), you need to #include the header file that defines that
identifier.

Jon
 
M

Marchel

Jon Sturgeon said:
Before you can reference "MessageBox" (which I assume is one of your
classes?), you need to #include the header file that defines that
identifier.

Jon

Jon

MessageBox is a "build in" .NET class included in System.Windows.Forms
Also #include is already there. Try the following.

1) Start fresh new Windows Forms Apllication (.NET)
2) In the "Form1.h" file modify the Form1 constructor as follows:

Form1(void)
{
InitializeComponent();
MessageBox::Show(S"TEST",S"TEST"); // THIS IS THE MODIFICATION LINE ADDED
}

3) Compile it and run it. It runs OK with no errors and the expected .NET MessageBox shows up.

4) Now modify the project as follows: In the file Form1.h change the constructor from definition to declaration as
follows:

Form1(void); // This is file Form1.h

In the file Form1.cpp add constructor definition at the end of the file as follows:

Form1::Form1(void) // This is definition
{
InitializeComponent();
MessageBox::Show(S"TEST",S"TEST");
}

Notice, that file Form1.cpp already has #include "Form1.h" line above the new declaration.

5) When you try to compile this you will get the following error:

c:\Documents and Settings\Jacek\Documents\Programs\Vc\test\Form1.cpp(20): error C2653: 'MessageBoxA' : is not a
class or namespace name

Becuase #include "Form1.h" is in the file "Form1.cpp", anything that was "#included" or "using...ed" in the file
"Form1.h" should
theoretically and traditionally apply in file "Form1.cpp" after #include "Form1.h" line but obviously it is not.
My old school of C++ and C cannot explain this. Please help :-(

Jack
 
J

Jon Sturgeon

Jon

MessageBox is a "build in" .NET class included in System.Windows.Forms
Also #include is already there. Try the following.

Ah, sorry, I didn't realize this was a .NET framework question. Being
posted in the .vc newsgroup I rashly expected it to be a C++ related
issue :)

I have very little experience with .NET so I'll leave your question for
somebody else...

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top