Getting compiler error C2653 and don't know why.

R

Richard

Hi All,

I am using Visual C++ .Net to create a windows forms application and I am
getting the following errors when I do a MessageBox::Show in my Form1.cpp
file:

Form1.cpp(19): error C2653: 'MessageBoxA' : is not a class or namespace name
Form1.cpp(19): error C2660: 'System::Windows::Forms::Control::Show' :
function does not take 1 arguments

If I move the MessageBox::Show line into the Form1.h file, there is no
problem. Any one have any ideas?

Here is the sample code:

Form1.cpp
-----------
#include "stdafx.h"
#include "Form1.h"
#include <windows.h>

using namespace C2653Error;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}

void Form1::ShowAMessage()
{
MessageBox::Show(S"Test Message");
}

Form1.h
---------
#pragma once


namespace C2653Error
{
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>
/// 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.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
private:
void ShowAMessage();
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * button1;

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::point(96, 104);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);

}
private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
ShowAMessage();
}

};
}

Also, I have tried adding all of the 'using namespace' lines that are in the
..h file to the .cpp file, but no luck.

Any help would be appreciated.

Thanks,

Richard
 
J

Jeff Partch [MVP]

Richard said:
Hi All,

I am using Visual C++ .Net to create a windows forms application and I am
getting the following errors when I do a MessageBox::Show in my Form1.cpp
file:

Form1.cpp(19): error C2653: 'MessageBoxA' : is not a class or namespace name
Form1.cpp(19): error C2660: 'System::Windows::Forms::Control::Show' :
function does not take 1 arguments

If I move the MessageBox::Show line into the Form1.h file, there is no
problem. Any one have any ideas?

Here is the sample code:

Form1.cpp
-----------
#include "stdafx.h"
#include "Form1.h"
#include <windows.h>

using namespace C2653Error;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}

void Form1::ShowAMessage()
{
MessageBox::Show(S"Test Message");
}

I think it's because <windows.h> uses a #define MessageBox preprocessor
macro. See if you can wrap the call in an #ifdef/#undef and #pragma
macro_push/#pragma macro_pop, or somesuch combination.
 
T

TOM

There is a KB article on this problem, but I can't find it right now with a
simle search.
I believe the answer was to #undef MessageBox after including
windows.h

-- Tom
 
R

Richard

Jeff and Tom,

Thanks for both of your replies. I tried to find the KB article, but didn't
have any luck. Anyway, I went ahead and just #undef'd MessageBox and that
did the trick. Thanks again.

Richard

TOM said:
There is a KB article on this problem, but I can't find it right now with a
simle search.
I believe the answer was to #undef MessageBox after including
windows.h

-- Tom
 
N

Nishant S

The proper way to do this would be :-

#pragma push_macro("MessageBox")
#undef MessageBox
MessageBox::Show(S".....");
#pragma pop_macro("MessageBox")

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */


Richard said:
Jeff and Tom,

Thanks for both of your replies. I tried to find the KB article, but didn't
have any luck. Anyway, I went ahead and just #undef'd MessageBox and that
did the trick. Thanks again.

Richard

TOM said:
There is a KB article on this problem, but I can't find it right now
with
 

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