PC Review


Reply
Thread Tools Rate Thread

Unable to declare a class in 2 forms

 
 
Allen
Guest
Posts: n/a
 
      26th Sep 2009

Unable to declare a class in 2 forms

--------------------------------------------------------------------------------

Below are 4 forms. Namely: Form1, EmployeeID, AboutBox and
EnterEmployeeName. I made a class to help in open, write and read from
files, called Time. I managed to declare class Time in EmployeeID space, but
could not declare it in EnterEmployeeName. In other words, I can declare
class Time in one space only. I realy want to use class Time in both forms,
EmployeeID and EnterEmployeeName. Can anybody tell me why I always get
error, below, every time I run the program? And how can I use the Time class
in both the above forms? I am showing below the skeleton and believe me it
will compile.


Error message:

1>c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Time.h(5) : error C2011: 'Time' :
'class' type redefinition
1> c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Time.h(5) : see declaration of
'Time'
1>EnterEmployeeName.cpp

//Form1:

#pragma once
#include "AboutBox.h"
#include "EmployeeID.h"
#include "EnterEmployeeName.h"
namespace TimeTracking
{
using namespace System::Windows::Forms;
public ref class Form1 : public System::Windows::Forms::Form
{

};
}
/***************************/

// EmployeeID

#pragma once
#include "Time.h"
//#include "stdafx.h"
#using <mscorlib.dll>

namespace TimeTracking
{
public ref class EmployeeID : public System::Windows::Forms::Form
{
};
}
/***************************/

// AboutBox:

#pragma once
namespace TimeTracking
{
public ref class AboutBox : public System::Windows::Forms::Form
{

};
}
/***************************/

//EnterEmployeeName:


#pragma once
#include "stdafx.h"
//#include "Time.h" //-------------uncomment and get error C2011

namespace TimeTracking
{

public ref class EnterEmployeeName : public System::Windows::Forms::Form
{

};
}
/***************************/

//Time.h:

using namespace System;
ref class Time
{
};
/***************************/

//Time.cpp:

#include "stdafx.h"
#include "Time.h"
#using <mscorlib.dll>

--------------------------------------------------------------------------------

Thanks
Allen

 
Reply With Quote
 
 
 
 
David Wilkinson
Guest
Posts: n/a
 
      26th Sep 2009
Allen wrote:
>
> Unable to declare a class in 2 forms
>
> --------------------------------------------------------------------------------
>
>
> Below are 4 forms. Namely: Form1, EmployeeID, AboutBox and
> EnterEmployeeName. I made a class to help in open, write and read from
> files, called Time. I managed to declare class Time in EmployeeID space,
> but could not declare it in EnterEmployeeName. In other words, I can
> declare class Time in one space only. I realy want to use class Time in
> both forms, EmployeeID and EnterEmployeeName. Can anybody tell me why I
> always get error, below, every time I run the program? And how can I use
> the Time class in both the above forms? I am showing below the skeleton
> and believe me it will compile.
>
>
> Error message:
>
> 1>c:\users\allen\documents\visual studio
> 2008\projects\timetracking\timetracking\Time.h(5) : error C2011: 'Time'
> : 'class' type redefinition
> 1> c:\users\allen\documents\visual studio
> 2008\projects\timetracking\timetracking\Time.h(5) : see declaration of
> 'Time'
> 1>EnterEmployeeName.cpp
>
> //Form1:
>
> #pragma once
> #include "AboutBox.h"
> #include "EmployeeID.h"
> #include "EnterEmployeeName.h"
> namespace TimeTracking
> {
> using namespace System::Windows::Forms;
> public ref class Form1 : public System::Windows::Forms::Form
> {
>
> };
> }
> /***************************/
>
> // EmployeeID
>
> #pragma once
> #include "Time.h"
> //#include "stdafx.h"
> #using <mscorlib.dll>
>
> namespace TimeTracking
> {
> public ref class EmployeeID : public System::Windows::Forms::Form
> {
> };
> }
> /***************************/
>
> // AboutBox:
>
> #pragma once
> namespace TimeTracking
> {
> public ref class AboutBox : public System::Windows::Forms::Form
> {
>
> };
> }
> /***************************/
>
> //EnterEmployeeName:
>
>
> #pragma once
> #include "stdafx.h"
> //#include "Time.h" //-------------uncomment and get error C2011
>
> namespace TimeTracking
> {
>
> public ref class EnterEmployeeName : public System::Windows::Forms::Form
> {
>
> };
> }
> /***************************/
>
> //Time.h:
>
> using namespace System;
> ref class Time
> {
> };
> /***************************/
>
> //Time.cpp:
>
> #include "stdafx.h"
> #include "Time.h"
> #using <mscorlib.dll>


Allen:

1. Time.h (and all header files) need #pragma once at the beginning.

2. Do not use #include "stdafx.h" in header files.

3. #include "stdafx.h" must be the first line in all .cpp files.

--
David Wilkinson
Visual C++ MVP
 
Reply With Quote
 
 
 
 
Allen
Guest
Posts: n/a
 
      26th Sep 2009
Thanks MR Wilkinson I can use that.

--
Thanks
Allen
"David Wilkinson" <(E-Mail Removed)> wrote in message
news:%23%(E-Mail Removed)...
> Allen wrote:
>>
>> Unable to declare a class in 2 forms
>>
>> --------------------------------------------------------------------------------
>>
>>
>> Below are 4 forms. Namely: Form1, EmployeeID, AboutBox and
>> EnterEmployeeName. I made a class to help in open, write and read from
>> files, called Time. I managed to declare class Time in EmployeeID space,
>> but could not declare it in EnterEmployeeName. In other words, I can
>> declare class Time in one space only. I realy want to use class Time in
>> both forms, EmployeeID and EnterEmployeeName. Can anybody tell me why I
>> always get error, below, every time I run the program? And how can I use
>> the Time class in both the above forms? I am showing below the skeleton
>> and believe me it will compile.
>>
>>
>> Error message:
>>
>> 1>c:\users\allen\documents\visual studio
>> 2008\projects\timetracking\timetracking\Time.h(5) : error C2011: 'Time' :
>> 'class' type redefinition
>> 1> c:\users\allen\documents\visual studio
>> 2008\projects\timetracking\timetracking\Time.h(5) : see declaration of
>> 'Time'
>> 1>EnterEmployeeName.cpp
>>
>> //Form1:
>>
>> #pragma once
>> #include "AboutBox.h"
>> #include "EmployeeID.h"
>> #include "EnterEmployeeName.h"
>> namespace TimeTracking
>> {
>> using namespace System::Windows::Forms;
>> public ref class Form1 : public System::Windows::Forms::Form
>> {
>>
>> };
>> }
>> /***************************/
>>
>> // EmployeeID
>>
>> #pragma once
>> #include "Time.h"
>> //#include "stdafx.h"
>> #using <mscorlib.dll>
>>
>> namespace TimeTracking
>> {
>> public ref class EmployeeID : public System::Windows::Forms::Form
>> {
>> };
>> }
>> /***************************/
>>
>> // AboutBox:
>>
>> #pragma once
>> namespace TimeTracking
>> {
>> public ref class AboutBox : public System::Windows::Forms::Form
>> {
>>
>> };
>> }
>> /***************************/
>>
>> //EnterEmployeeName:
>>
>>
>> #pragma once
>> #include "stdafx.h"
>> //#include "Time.h" //-------------uncomment and get error C2011
>>
>> namespace TimeTracking
>> {
>>
>> public ref class EnterEmployeeName : public System::Windows::Forms::Form
>> {
>>
>> };
>> }
>> /***************************/
>>
>> //Time.h:
>>
>> using namespace System;
>> ref class Time
>> {
>> };
>> /***************************/
>>
>> //Time.cpp:
>>
>> #include "stdafx.h"
>> #include "Time.h"
>> #using <mscorlib.dll>

>
> Allen:
>
> 1. Time.h (and all header files) need #pragma once at the beginning.
>
> 2. Do not use #include "stdafx.h" in header files.
>
> 3. #include "stdafx.h" must be the first line in all .cpp files.
>
> --
> David Wilkinson
> Visual C++ MVP


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am unable to declare a class in 2 separate forms Allen Microsoft Dot NET Framework Forms 0 25th Sep 2009 03:13 AM
Can we declare delegates in class and can we declare delegates in Interface Bhuwan Bhaskar Microsoft Dot NET Framework 4 4th Oct 2007 05:25 AM
what happens really when I declare a class in a class Thomas Microsoft VB .NET 4 23rd Apr 2005 06:24 PM
declare variable in base class that must be set in dervied class? John B Microsoft C# .NET 3 18th Feb 2005 09:34 PM
To declare or not to declare Alan Beban Microsoft Excel Programming 10 30th Oct 2004 03:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:15 PM.