C++ .h file help!

SIM0N

Junior Member
Nov 24, 2012
8
0
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
 
Last edited:

Chris27

Member
Sep 19, 2005
140
0
0
I just glanced at your code, but it looks like you have some extra right parens in your setTime function definition.
 

SIM0N

Junior Member
Nov 24, 2012
8
0
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.
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
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.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
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?
 
Last edited:

SIM0N

Junior Member
Nov 24, 2012
8
0
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?
 

SIM0N

Junior Member
Nov 24, 2012
8
0
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.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,591
5
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
 
Last edited:

Merad

Platinum Member
May 31, 2010
2,586
19
81
I did notice overhead in the time statement
Code:
hour = (h>= 0 && < 24)) ? h:0;

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
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,591
5
0
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

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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |