Unable to declare a class in 2 forms

Discussion in 'Microsoft VC .NET' started by Allen, Sep 26, 2009.

  1. Allen

    Allen Guest

    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
     
    Allen, Sep 26, 2009
    #1
    1. Advertisements

  2. 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
     
    David Wilkinson, Sep 26, 2009
    #2
    1. Advertisements

  3. Allen

    Allen Guest

    Thanks MR Wilkinson I can use that.

    --
    Thanks
    Allen
    "David Wilkinson" <> wrote in message
    news:%23%...
    > 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
     
    Allen, Sep 26, 2009
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. René Estrella
    Replies:
    1
    Views:
    271
    Carl Daniel [VC++ MVP]
    Dec 5, 2003
  2. Guest

    Declare COM properties at run-time?

    Guest, Jun 3, 2004, in forum: Microsoft VC .NET
    Replies:
    0
    Views:
    176
    Guest
    Jun 3, 2004
  3. Jeff Partch

    Re: errors trying to declare COMBOBOXINFO structure...

    Jeff Partch, Jun 21, 2004, in forum: Microsoft VC .NET
    Replies:
    3
    Views:
    502
    Jeff Partch [MVP]
    Jun 22, 2004
  4. Guest

    How declare array of DataRow

    Guest, Feb 3, 2005, in forum: Microsoft VC .NET
    Replies:
    2
    Views:
    1,748
    Bart Mermuys
    Feb 3, 2005
  5. Geoff Cox

    declare & initialize array?

    Geoff Cox, Aug 16, 2005, in forum: Microsoft VC .NET
    Replies:
    15
    Views:
    861
    Geoff Cox
    Aug 17, 2005
Loading...

Share This Page