Accessing a class from WinMain

This is a discussion on Accessing a class from WinMain within the C++ Programming forums, part of the General Programming Boards category; This is unbelievable! But, then again, I suppose I have had the same experience with java before. I'm sure it ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    Accessing a class from WinMain

    This is unbelievable! But, then again, I suppose I have had the same experience with java before. I'm sure it is a lack of experience in C++. So I hope you can help.

    I created a class to keep a record of the employees:

    -------------class Employees----------------
    class Employees {

    public:

    Employees ();
    void addEmployees(string employee);

    };

    void Employees::addEmployees(string employee) {
    //Just to check if it is getting here
    MessageBox(hwnd, "addEmployee", "here",
    MB_OK | MB_ICONINFORMATION);
    }
    -------------End class Employees----------------

    So I created a new .h file, since I did not want to use the main.h file that is used for most of the interface utilities.

    -------Employee.h---------

    #include <windows.h>
    #define Employees

    -------End Employee.h----------------------

    And in my main implementation.........

    #include <windows.h>
    #include "main.h"
    #include <string>
    #include "Employee.h"
    /*
    I did actually name the . h file as Employee instead of Employees, in case you were wondering if the problem was that
    */
    using namespace std;


    /* Declare WindowsProcedure */

    /*Not applicable................from here to....*/

    LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
    BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
    void FileBox(HWND hwnd, HWND hListBox);
    /* Make the classname into a global variable */
    char szClassName[] = "Schedule";
    HINSTANCE hThisInstance;
    HWND hListBox;

    /*Not applicable... ............here */

    //on the following line I get an error(see #1 below)
    Employees employ;

    So, when I try to compile, I get:

    #1
    {
    18 C:\Dev-Cpp\myProject\main.cpp
    ISO C++ forbids declaration of `employ' with no type
    }

    I have followed many different tutorials to find a solution but have not yet found one. Hmmm...what to do........what to do.....

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Well all i have to say is code tags please
    Woop?

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    #define Employees
    Remove this line.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    i'm using dev_c++

    It is amazing how many different flavours there are for C++. It's a little frustrating to learn. I'm using dev-C++4.9.8

    I tried removing #define employees from my .h file. It did not make a difference.

    I'm thinking that the problem lies in my declararion of this class. For some resaon it is not recognizing the class. As BennyAndTheJets said, it doesn't seem to be part of the .h file.

    hmmmm, good thing i live on the ground floor(not too far for the computer to fall after throwing it out the window. lol

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    prog-bman

    I don't know what you mean by code tags

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Okay do this. Put the class declaration in Employee.h. Put the class definitions in Employee.cpp. Include Employee.h in Employee.cpp and main.cpp. Eg:

    Code:
    //Employee.h
    
    class Employees
    {
    public:
        void AddEmployee(string name);
    };
    Code:
    //Employee.cpp
    #include "employee.h"
    void Employees::AddEmployee(string name)
    {
        //Put your code here
    }
    Code:
    //main.cpp
    #include <string>
    #include "employee.h"
    
    using namespace std;
    int main()
    {
        Employees EmployeeList;
        string newName="Joe Smith";
        EmployeeList.AddEmployee(newName);
        return 0;
    }
    To use code tags, surround your code in [ code] and [ /code].
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    All very valid thoughts......but, I'm still stuck


    I tried what you had posted but I still get the same message. Hmmmmm.

    I love to program and get results......but I hate to hit a wall like this

    i'm starting to wonder about my compiler at this point

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    concerning main()

    I don't think this makes much difference but......lol... ya never know.

    my main is:

    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

    But the compiler never gets there as I am declaring before WinMain

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Did you try to compile my code exactly as it appears above?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Actually, the code has been compiling all day until I introduced the new class.

    I even tried putting the class into the same file as main but I still got the same error. hmmmmmm

    It's a bit of a big project so I can't cop out.

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You didn't answer my question. Can you compile the sample program I gave you?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Sorry, I assumed you knew i had tried to compile the code as you had given me. Lol, never assume.

    I did try your code. The path, however, was the same as I had in my naive code. At least in the results.

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    The path, however, was the same as I had in my naive code. At least in the results.
    I have no idea what you're talking about. However, I assume that you mean it didn't compile. In that case, your compiler is busted. Try updating to the newest version of Dev-C++.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    I meant the pathe from main, through the . h and into my class.

    And, I'm seriously thinking that you may be right about the compiler thing. I guess the best test would be to take it out to the university and see if it will compile there.

    LOL, it is highly possible that I'm missing something too. I'm not used to C++.

    Anyway, I really appreciate your help on this. I learned lots!

    Have a good one.

    Mark

  15. #15
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    compiler?

    I only downloaded it 2 weeks ago. I need visual studio so I can save time on the little I am sillyI am sillyI am sillyI am silly

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 11:16 AM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 06:30 AM
  3. Replies: 10
    Last Post: 07-26-2008, 09:44 AM
  4. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 03:46 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21