CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2009
    Posts
    12

    .cpp and .h file not compiling

    here is the code for my .cpp and .h files, they are not compiling, i am getting the error in .cpp file in the line #include "Class.h"
    The error message is
    3 class.cpp Class.h:6: parse error before `;'
    Please help!


    Class.h
    Code:
    #ifndef Class_H
    #define Class_H
    
     using namespace std;
       class Class
       {
       private;
       char class_name;
       char no_of_subjects;
    
       public:
       Class();
       int findtotal();
    
       }  ;
       #endif

    Class.cpp
    Code:
    #include <iostream>
    #include "Class.h"
    
    
    
         Class::Class Class()
       {  ;
       }
        int Class::findtotal()
        { ;
        }

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: .cpp and .h file not compiling

    Code:
    private;
    should be:
    Code:
    private:
    Also, please give a meaningfull name to your class.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: .cpp and .h file not compiling

    And constructor implementation is not correct...

    Code:
    Class::Class()
    {
    }
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: .cpp and .h file not compiling

    It's also unwise (though not technically incorrect) to put a "using" statement in a header file.

  5. #5
    Join Date
    Apr 2009
    Posts
    12

    Re: .cpp and .h file not compiling

    I overlooked the constructor syntax. Thanks for pointing out. Its compiling now. I included the namespace std, because i saw this syntax somewhere,infact i am not clear about what it does, i want to know "what exactly is namespace doing in this case?", please answer.

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: .cpp and .h file not compiling

    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  7. #7
    Join Date
    Apr 2009
    Posts
    12

    Re: .cpp and .h file not compiling

    The link you posted is helpful but why is it so that i never used "namespace std" in any of my programs, which had cout and cin keywords and they all compiled without error. I just included the "isotream" header file.
    This is the first time hen i worte a separate header file and used the namspace std, Why?

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: .cpp and .h file not compiling

    As long as you are using a recent compiler, you have to use the full name, like std::cout and std::cin. Otherwise, you need "using namespace std" in your code. One unlikely possibility is that you may be using a old compiler that is not compliance with C++90 standard.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  9. #9
    Join Date
    Apr 2009
    Posts
    12

    Re: .cpp and .h file not compiling

    Now this is giving the error
    [Linker error] undefined reference to `WinMain@16'

    I have not created any "main.cpp", because it is not required by the teacher in the assignment. Will the program compile without the "main()" function, also if i create a "main.cpp" do i include the Class.cpp file in it only or the Class.h file too?

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: .cpp and .h file not compiling

    Quote Originally Posted by emerald09
    I have not created any "main.cpp", because it is not required by the teacher in the assignment. Will the program compile without the "main()" function, also if i create a "main.cpp" do i include the Class.cpp file in it only or the Class.h file too?
    You do not necessarily need the global main function, e.g., you can compile individual source files separately into object files. However, you need the global main function to have an executable program, but of course you are not required to name the source file containing the global main function "main.cpp".

    You probably would want to define a global main function that allows you to test your class. The source file containing the definition of the global main function would need to include your class' header file, but not the source file.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: .cpp and .h file not compiling

    Console applications require main(); Windows (non-console) applications require WinMain(). Make sure you've got the right type of project.

  12. #12
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    Re: .cpp and .h file not compiling

    It seems me you use Dev-C++. In this program (as I remember) there should not be any .cpp files with the same name as the project name. You may use clear MinGW with no Dev-C++

    g++ your_file_1.cpp your_file_2.cpp your_file_3.cpp -o your_program.exe

    Note: no .h files in a list

  13. #13
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: .cpp and .h file not compiling

    Quote Originally Posted by andrey_zh View Post
    It seems me you use Dev-C++. In this program (as I remember) there should not be any .cpp files with the same name as the project name.
    I've used Dev-C++ in the past and never had problems having a .cpp file with the same name as the project.
    Old Unix programmers never die, they just mv to /dev/null

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured