Thread: Beginner Classes Question

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    33

    Beginner Classes Question

    Hi guys,

    I'm new to C++, started learning it recently, just got a question in regards to classes.
    I have some familiarity with how they work (studied Java previously), but have an issue with regards to creating a complete "project" (meaning a Visual C++ / DevCpp "project")

    I'm currently taught to create class definition in a header file, so if my class is called MyClass, create a MyClass.h, then #include that in a main .cpp file.
    However, my tutor mentioned something about creating a MyClass.h, then actually defining all the functions for it in a corresponding MyClass.cpp file, finally using a driver main.cpp which links it all together.

    I can define MyClass.h functions in main.cpp, but if i try to define the functions in MyClass.cpp i get compiler errors..

    Any advice? Not wanting anyone to do my homework for me, just wondering what is the preferred method for creating classes / where to define class functions / how to link into driver file (main.cpp).

    sorry for long first post!

    Regards,

    -Kirill

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Any advice?
    Post the error messages (copying and pasting them into code tags is best). Some code couldn't hurt either if the errors point to specific lines (include the code around the line as well and post in code tags).

    Make sure you have header include guards. Make sure you only ever #include headers (and not cpp files). Make sure you #include or declare everything you need in the file for each file.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help if you told us what errors you get.

    My first guess is that your "myclass.cpp" is not part of your project, so when you compile the code, it doesn't take into account the "myclass.cpp".

    What happens if you put something that can't be compiled into myclass.cpp (typing something random like "asdfhajk^121as*&%" into the file usually works well in generating some errors - if you don't get an error for that line [or near that line], then you are not building myclass.cpp).

    It is also possible that you have made one or more of hundreds of other mistakes - I can't really say without seeing the error message [and I don't think anyone else can either].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Okay, so assuming a header file MyPC.h (which contains the definition of class MyPC),

    I now have a MyPC.cpp, which has the definition of all functions declared in MyPC.h (only one at the moment, void displayMessage(), takes no parameters, displays "hello")

    eg:

    in MyPC.h:


    void displayMessage();


    in MyPC.cpp:

    Code:
    #include "MyPC.h"
    #include <iostream>
    using namespace std;
    
    void MyPC::displayMessage()
    {
    
             cout<< "HELLO\n";
    
    }
    I'm getting:

    Linker error: undefined reference to 'WinMain@16'

    Pretty obscure, but if I had defined the function displayMessage() in a third file, main.cpp, it will compile!

    Regards,

    -Kirill

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void displayMessage();
    If that is NOT inside a class, then your my_pc.cpp doesn't define that function, it defines a function called MyPC::displayMessage(), which is a different function [in fact, I expect that to not compile].

    Code:
    Linker error: undefined reference to 'WinMain@16'
    You are trying to compile a project that is a Windows GUI application, when you probably want a console application. Is your my_pc.cpp in a different project?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think that the problem might be that you need both MyPC.cpp and main.cpp included in the project. I seem to recall some compiler(s) complaining about WinMain even if the project was a console application. The error is because your project does not have a main function, or it cannot find it. Where is your main() function and is that file included in the build?

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Hmm, the definition of the function is not in MyPC.h, only the declaration is.

    so, the MyPC.h is where i define the class MyPC, minus the functions because I was taught to define functions in a corresponding .cpp file. (is this wrong? should I define the functions in MyPC.h also, inside the class definition?)

    Project is definitely a win32 console app, also, MyPC.h, MyPC.cpp and the Main.cpp (which i intended to be the driver) all belong to the same project, so no problems here..

    Advice?

    Regards,

    -Kirill

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by Daved View Post
    I think that the problem might be that you need both MyPC.cpp and main.cpp included in the project. I seem to recall some compiler(s) complaining about WinMain even if the project was a console application. The error is because your project does not have a main function, or it cannot find it. Where is your main() function and is that file included in the build?
    Ah yes, that is it! Thanks Daved, just wrote some skeleton code for Main.cpp (driver) and added that to the project, so now the project has a main() function, everything compiles and links! So indeed, the compiles couldn't find the main() function!

    Sorry for noobish question, i'm still learning!

    Thanks to all who replied.

    Regards,

    -Kirill

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kbro3 View Post
    Hmm, the definition of the function is not in MyPC.h, only the declaration is.

    so, the MyPC.h is where i define the class MyPC, minus the functions because I was taught to define functions in a corresponding .cpp file. (is this wrong? should I define the functions in MyPC.h also, inside the class definition?)

    Regards,

    -Kirill
    Just to clarify this point: You should declare the class and it's components (member functions and member variables) in the .h file, then you define the functions in the .cpp file. So the function should be declared inside the class (e.g. myclass) in the header file, and then defined using "myclass::somefunction" in the .cpp.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by matsp View Post
    Just to clarify this point: You should declare the class and it's components (member functions and member variables) in the .h file, then you define the functions in the .cpp file. So the function should be declared inside the class (e.g. myclass) in the header file, and then defined using "myclass::somefunction" in the .cpp.

    --
    Mats
    Yep, that's what I am taught at the moment. Thanks for confirmation, it was a little confusing to me because, coming from Java, I'm used to declaring and defining class functions in one spot!

    Regards,

    -Kirill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner C Question Help
    By pmacdonald in forum C Programming
    Replies: 32
    Last Post: 06-22-2011, 09:32 PM
  2. Question about classes and inheritance
    By ~Kyo~ in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 05:36 PM
  3. Question about Classes
    By WildFire in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2004, 07:57 PM
  4. Flowchart question: Classes
    By darnok in forum C++ Programming
    Replies: 6
    Last Post: 06-17-2004, 06:25 PM
  5. Passing classes question
    By Daggie in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 11:09 AM