Discussion:
I want to declare a class in 2 forms
(too old to reply)
Allen
2009-09-25 02:10:15 UTC
Permalink
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?

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

#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
{

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

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

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

namespace TimeTracking
{

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

};
}
/***************************/
using namespace System;
ref class Time
{
};
/***************************/
#include "stdafx.h"
#include "Time.h"
#using <mscorlib.dll>
--
Thanks
Allen
Jeff Johnson
2009-09-25 13:19:51 UTC
Permalink
Post by Allen
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 don't think this has anything to do with Windows Forms. This sounds like a
pure C++ question, and I think you'll get better responses if you ask in a
C++ group.
Allen
2009-09-26 20:19:38 UTC
Permalink
Trust me it is windows Forms. As the matter of fact it can be regarded as
both C++ and Forms. C++ in other groups told me I have to post in Forms
groups. It looks like it is the matter of how we see the glass full or
empty!!


Thanks
Allen
Post by Jeff Johnson
Post by Allen
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 don't think this has anything to do with Windows Forms. This sounds like
a pure C++ question, and I think you'll get better responses if you ask in
a C++ group.
Jeff Johnson
2009-09-28 13:19:40 UTC
Permalink
Post by Allen
Trust me it is windows Forms. As the matter of fact it can be regarded as
both C++ and Forms. C++ in other groups told me I have to post in Forms
groups. It looks like it is the matter of how we see the glass full or
empty!!
Can you reproduce your problem using "normal" classes (i.e., those not
derived from Form) in a simple console app? If so, then it's a C++ thing,
y'know? Try it and see and post back.
unknown
2009-09-28 13:20:39 UTC
Permalink
It seems that time.h includes a class declaration. As you are included the
same file two times the class declaration is compiled two times hence the
error message...

Also you seems to talk about "using" or "declaring" without making any
difference. This is not the same thing. A class should be declared one time
and can be used multiple times...

Anotehr option would be to declare two classes in each of those forms.
You'll have then to declare this class inside the namespace (but IMO it
would be best to declare this class once).

This is definitively a language issue (this is a compiler error, so I hardly
see how a compiler error couldn't be a language issue).


--
Patrice
valencia
2009-11-11 20:03:26 UTC
Permalink
Post by Allen
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?
1>c:\users\allen\documents\visual studio
'class' type redefinition
1> c:\users\allen\documents\visual studio
2008\projects\timetracking\timetracking\Time.h(5) : see declaration of
'Time'
1>EnterEmployeeName.cpp
#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
{
};
}
/***************************/
#pragma once
#include "Time.h"
//#include "stdafx.h"
#using <mscorlib.dll>
namespace TimeTracking
{
public ref class EmployeeID : public System::Windows::Forms::Form
{
};
}
/***************************/
#pragma once
namespace TimeTracking
{
public ref class AboutBox : public System::Windows::Forms::Form
{
};
}
/***************************/
#pragma once
#include "stdafx.h"
//#include "Time.h" //-------------uncomment and get error C2011
namespace TimeTracking
{
public ref class EnterEmployeeName : public System::Windows::Forms::Form
{
};
}
/***************************/
using namespace System;
ref class Time
{
};
/***************************/
#include "stdafx.h"
#include "Time.h"
#using <mscorlib.dll>
--
Thanks
Allen
Loading...