C++ .h file help!

Discussion in 'Programming' started by SIM0N, Nov 24, 2012.

  1. SIM0N

    SIM0N Junior Member

    Joined:
    Nov 24, 2012
    Messages:
    8
    Likes Received:
    0
    I'm an undergrad taking my first C++ class. So I'm new with C++.

    I have a homework assignment to create my own class, with a header file for that class, a cpp file for the member functions, and a cpp file with a main function to drive it.

    I haven't started on the assignment yet. I've just copied an example out of the text book to get comfortable linking all of the files.

    I'm using Dev C++, and my textbook mentions nothing about projects. so I'm kinda just throwing darts with project, but I think they all linked properly. Idon't see anything wrong with the code, but when I try to compile it I get a huge list of errors. I think it might have something to do with where these files are located.

    If anyone could please help me figure out why this isn't working on my machine I would really appreciate it.

    Here is the header file:
    Code:
    #ifndef TIME_H
    #define TIME_H
    
    class Time
    {
    public:
           Time();
           void setTime(int, int, int);
           void printUniversal();
           void printStandard();
    Private:
            int hour;
            int minute;
            int second;
    };
    #endif
    
    
    
    Here is my cpp file containing the member functions:
    Code:
    #include <iostream>
    #include <iomanip>
    #include "Time.h"
    
    using namespace std;
    
    Time::Time()
    {
        hour = minute = second = 0;                       
    }
    
    void Time::setTime(int h, int m, int s)
    {
     hour = (h>= 0 && < 24)) ? h:0;    
     minute = (m>= 0 && < 60)) ? m:0; 
     second = (s>= 0 && < 60)) ? s:0; 
    }
    
    void Time::printUniversal()
    {     
     cout << setfill('0') << setw(2) << hour << ":"
     << setw(2) << minute << ":" << setw(2) << second; 
    }
    
    void Time::printStandard()
    {
     cout << ((hour == 0 || hour == 12) ? 12:hour % 12) << ":"
     << setfill('0') << setw(2) << minute <<  ":" << setw(2)
     << second << (hour<12? " AM":" PM");       
    }
    
    
    and here is my main driver:
    Code:
    #include <iostream>
    #include "Time.h"
    
    using namespace std;
    
    int main()
    {    
    Time t;
    
    t.printUniversal();
    t.printStandard();
    
    t.setTime(5,22,35);
    
    t.printUniversal();
    t.printStandard();
        
    system("PAUSE");
    }
    
    
    

    I didn't type out the entire example of the main driver out of the text book, but both Time.cpp and Time.h are exactly from the text.

    I have a separate homework directory on my computer for coding for school, but when I tried it in that I got even more errors than in it's current location, the Dev C++ program file.

    Here is my compile log: (It says there are 15 total errors)
    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Makefile.win"
    Executing  make...
    make.exe -f "C:\Dev-Cpp\Makefile.win" all
    g++.exe -c Time.cpp -o Time.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   
    
    In file included from C:/Dev-Cpp/include/c++/3.4.2/ctime:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/cwchar:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/bits/postypes.h:46,
                     from C:/Dev-Cpp/include/c++/3.4.2/iosfwd:50,
                     from C:/Dev-Cpp/include/c++/3.4.2/ios:44,
    
                     from C:/Dev-Cpp/include/c++/3.4.2/ostream:45,
    
                     from C:/Dev-Cpp/include/c++/3.4.2/iostream:45,
                     from Time.cpp:1:
    C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include/time.h:12: error: expected primary-expression before "int"
    C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include/time.h:12: error: expected `;' before "int"
    In file included from C:/Dev-Cpp/include/c++/3.4.2/cwchar:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/bits/postypes.h:46,
                     from C:/Dev-Cpp/include/c++/3.4.2/iosfwd:50,
                     from C:/Dev-Cpp/include/c++/3.4.2/ios:44,
                     from C:/Dev-Cpp/include/c++/3.4.2/ostream:45,
                     from C:/Dev-Cpp/include/c++/3.4.2/iostream:45,
                     from Time.cpp:1:
    C:/Dev-Cpp/include/c++/3.4.2/ctime:64: error: expected unqualified-id before "namespace"
    C:/Dev-Cpp/include/c++/3.4.2/ctime:64: error: expected `,' or `;' before "namespace"
    
    Time.cpp: In constructor `Time::Time()':
    Time.cpp:9: error: `hour' undeclared (first use this function)
    Time.cpp:9: error: (Each undeclared identifier is reported only once for each function it appears in.)
    
    Time.cpp: In member function `void Time::setTime(int, int, int)':
    Time.cpp:14: error: `hour' undeclared (first use this function)
    Time.cpp:14: error: expected primary-expression before '<' token
    Time.cpp:14: error: expected `;' before ')' token
    Time.cpp:15: error: expected primary-expression before '<' token
    Time.cpp:15: error: expected `;' before ')' token
    Time.cpp:16: error: expected primary-expression before '<' token
    
    Time.cpp:16: error: expected `;' before ')' token
    Time.cpp: In member function `void Time::printUniversal()':
    
    Time.cpp:21: error: `hour' undeclared (first use this function)
    Time.cpp: In member function `void Time::printStandard()':
    Time.cpp:27: error: `hour' undeclared (first use this function)
    
    make.exe: *** [Time.o] Error 1
    
    Execution terminated
    
    
    If anyone could help that would be great
     
    #1 SIM0N, Nov 24, 2012
    Last edited: Nov 24, 2012
  2. Chris27

    Chris27 Member

    Joined:
    Sep 19, 2005
    Messages:
    140
    Likes Received:
    0
    I just glanced at your code, but it looks like you have some extra right parens in your setTime function definition.
     
  3. SIM0N

    SIM0N Junior Member

    Joined:
    Nov 24, 2012
    Messages:
    8
    Likes Received:
    0
    That was incorrect and did fix three errors. I didn't think there were any syntax problems because they weren't marked after I compiled. Usually the lines that contain errors are highlighted.

    I don't know why it isn't showing me where any syntax errors are, but that did take the total errors listed in the log from 15 to 12. So thank you very much.

    I'm going through it now with a fine eye and haven't found anything else.

    UPDATE: I found that private: in the header file was capitalized, but it still lists 12 errors.
     
  4. Merad

    Merad Platinum Member

    Joined:
    May 31, 2010
    Messages:
    2,484
    Likes Received:
    2
    Step 1: Ditch Dev-C++ and get a modern, supported IDE. It's been like 7 years since Dev-C++ was updated and it wasn't that great even back then.

    Get Visual Studio express or Code::Blocks with mingw.
     
  5. degibson

    degibson Golden Member

    Joined:
    Mar 21, 2008
    Messages:
    1,389
    Likes Received:
    0
    Hello Sim0n and welcome.

    When dealing with compiler errors like the one you're facing, just deal with the first problem, then try again. Trying to deal with all the errors at once is way too complicated, and the first error often causes a lot of subsequent errors.

    At first, it's hard to even identify what the first error is. It's this:
    Code:
    In file included from C:/Dev-Cpp/include/c++/3.4.2/ctime:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/cwchar:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/bits/postypes.h:46,
                     from C:/Dev-Cpp/include/c++/3.4.2/iosfwd:50,
                     from C:/Dev-Cpp/include/c++/3.4.2/ios:44,
    
                     from C:/Dev-Cpp/include/c++/3.4.2/ostream:45,
    
                     from C:/Dev-Cpp/include/c++/3.4.2/iostream:45,
                     from Time.cpp:1:
    C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include/time.h:12: error: expected primary-expression before "int"
    C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include/time.h:12: error: expected `;' before "int"
    
    Edit: OP discovered he was accidentally editing the include tree. Oops

    Which I believe is an error you've already found (accidental capitalization of Private: -- it should be private: ).

    After fixing each error, try to compile again. Eventually you'll get there.

    Now, on to a specific issue I noticed:
    Code:
      hour = (h>= 0 && < 24)) ? h:0; 
    
    There are a couple errors on this line (and the two lines following). Chris27 nailed one, here's a lame hint at another: Which variable must be less than 24?
     
    #5 degibson, Nov 24, 2012
    Last edited: Nov 25, 2012
  6. SIM0N

    SIM0N Junior Member

    Joined:
    Nov 24, 2012
    Messages:
    8
    Likes Received:
    0
    Thank you so much Degibson for the welcome and the help! I don't know if I'm warm or not here, but I think there may be something wrong with the current state of the Dev C++ environment now because none of my programs are compiling. Smaller simpler programs I wrote for earlier assignments that were very successful are no longer compiling.

    I only bring this up because I feel like whatever is wrong can be what's causing the remaining problems, and because I am starting to get really worried that I am going to have to uninstall and re install dev c++ or something.

    So here is my example. Here is a stand alone program that I wrote that calculates an NFL passer rating after taking in all of the quarterbacks stats calculates and prints that rating. As you can see it only has two include statements and is so small the only function in the whole damn thing is the main. This program used to compile just fine, and still runs (I assume it's because it has been successfully compiled before creating an executable program.)

    However now when I compile it I get a very long complicated error message similar to what I'm getting with this class issue:

    here is the program:
    Code:
    #include<iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    
        float attempts = 0;
        float completions = 0;
        float completion_percentage = 0;
        float passing_yards = 0;
        float touchdowns = 0;
        float interceptions = 0;
        float passer_rating = 0;
        float part_a = 0;    
        float part_b = 0;
        float part_c = 0;
        float part_d = 0;
        
        cout << "Hello and welcome to PASSER RATING CALCULATOR O-RAMA!" << endl << "(Brought to you by Simoney Studios)" << endl;
        cout << endl;
        cout << endl;
        cout << "Please enter the number of passes attempted." << endl;
        std::cin >> attempts;
        cout << "Please enter the number of passes completed." << endl;
        std::cin >> completions;
        completion_percentage = (completions/attempts)*100;
        cout.setf(ios::fixed);
        cout << "Your completion percentage is: " << setprecision(2) << completion_percentage << endl;
        cout << "Please enter the number of touchdown passes thrown." << endl;
        std::cin >> touchdowns;
        cout << "Please enter the number of interceptions thrown." << endl;
        std::cin >> interceptions;
        cout << "Please enter the number of passing yards accumulated." << endl;
        std::cin >> passing_yards;
        
        
        //Calculate part A
        part_a = (completion_percentage - 30)/20;
        if(part_a < 0)
        part_a = 0;
        else if(part_a > 2.375)
        part_a = 2.375;
        
        //Calculate part B
        part_b = ((passing_yards/attempts) - 3)*.25;
        if(part_b < 0)
        part_b = 0;
        else if(part_b > 2.375)
        part_b = 2.375;
            
        //Calculate part C
        part_c = (touchdowns/attempts)*20;
        if(part_c < 0)
        part_c = 0;
        else if(part_c > 2.375)
        part_c = 2.375;
                
        //Calculate part D
        part_d = 2.375 - ((interceptions/attempts)*25);
        if(part_d < 0)
        part_d = 0;
        else if(part_d > 2.375)
        part_d = 2.375;
        
    
        
        //Calculate part Passer Rating
        passer_rating = ((part_a + part_b + part_c + part_d)/6)*100;
        
        cout.setf(ios::fixed);
        cout << "Your NFL passer rating is: " << setprecision(2) << passer_rating << endl;
        
    
    system("pause");
    }
    
    
    This used to compile, now when I try I get this:
    Code:
    Compiler: Default compiler
    Executing  g++.exe...
    g++.exe "C:\Users\Simoney\Documents\CS225ProgrammingConcepts\C++ Applications\PasserRating.cpp" -o "C:\Users\Simoney\Documents\CS225ProgrammingConcepts\C++ Applications\PasserRating.exe"   -O3  -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" 
    In file included from C:/Dev-Cpp/include/c++/3.4.2/ctime:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/cwchar:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/bits/postypes.h:46,
                     from C:/Dev-Cpp/include/c++/3.4.2/iosfwd:50,
                     from C:/Dev-Cpp/include/c++/3.4.2/ios:44,
    
                     from C:/Dev-Cpp/include/c++/3.4.2/ostream:45,
    
                     from C:/Dev-Cpp/include/c++/3.4.2/iostream:45,
                     from C:\Users\Simoney\Documents\CS225ProgrammingConcepts\C++ Applications\PasserRating.cpp:1:
    C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include/time.h:12: error: expected primary-expression before "int"
    C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include/time.h:12: error: expected `;' before "int"
    In file included from C:/Dev-Cpp/include/c++/3.4.2/cwchar:51,
                     from C:/Dev-Cpp/include/c++/3.4.2/bits/postypes.h:46,
                     from C:/Dev-Cpp/include/c++/3.4.2/iosfwd:50,
                     from C:/Dev-Cpp/include/c++/3.4.2/ios:44,
                     from C:/Dev-Cpp/include/c++/3.4.2/ostream:45,
                     from C:/Dev-Cpp/include/c++/3.4.2/iostream:45,
                     from C:\Users\Simoney\Documents\CS225ProgrammingConcepts\C++ Applications\PasserRating.cpp:1:
    C:/Dev-Cpp/include/c++/3.4.2/ctime:64: error: expected unqualified-id before "namespace"
    C:/Dev-Cpp/include/c++/3.4.2/ctime:64: error: expected `,' or `;' before "namespace"
    
    Execution terminated
    
    
    Someone please tell me what the hell happened?
     
  7. SIM0N

    SIM0N Junior Member

    Joined:
    Nov 24, 2012
    Messages:
    8
    Likes Received:
    0
    To anyone else who reads this:

    My time.h file was saved in the dev C++ include folder and this was causing the error. I have fixed my problem, and don't know if I can close a thread out, but thank you for looking.
     
  8. EagleKeeper

    EagleKeeper Discussion Club Moderator<br>Elite Member
    Staff Member

    Joined:
    Oct 30, 2000
    Messages:
    42,600
    Likes Received:
    0
    Threads do not need to be closed out unless there is a serious problem with posts within a thread.

    Best practice is for you to indicate that you have isolated the issues; fixed them and everything moves on.

    Thank people for their assistance.



    Now being later to the party (cleaning up a honey-do-list:$ = being gone for a year); I did notice overhead in the time statement
    Code:
    hour = (h>= 0 && < 24)) ? h:0;
    while not much; unless you ever plan on hours (h) being negative; define h as unsigned int.

    Code:
    hour = (h < 24)) ? h:0;
    That solves the need to test for 0; by definitions the value will always be positive.
    the same also applies to your minute and second variables
     
    #8 EagleKeeper, Nov 25, 2012
    Last edited: Nov 25, 2012
  9. Merad

    Merad Platinum Member

    Joined:
    May 31, 2010
    Messages:
    2,484
    Likes Received:
    2
    Actually it bears pointing out that this line (and the 2 following it) make up several of the compiler errors, and it's not possible to do comparisons the way that he seems to be attempting to do them.

    Each comparison has to be explicitly entered. If you want "h greater than or equal to 0 and h less than 24" then you need to do:

    Code:
    h >= 0 && h < 24
     
  10. EagleKeeper

    EagleKeeper Discussion Club Moderator<br>Elite Member
    Staff Member

    Joined:
    Oct 30, 2000
    Messages:
    42,600
    Likes Received:
    0
    fully agree.

    and while it does not cost any execution time, for support work if it much better (IMHO) to embedd each comparison in ().
    Code:
    (h >= 0) && (h < 24)
    Stringing them together without () can lead to confusion in terms of coding and/or support; order of execution may not be what was intended.
     
Loading...