Working with classes, need help...

Discuss all aspects of programming here.

Moderator: The Mod Squad

Working with classes, need help...

Postby Snowfire » Wed Apr 13, 2005 9:45 am

Howdy all, I've been reading up on using classes in C++ for over a week now so that I might actually know what I'm doing fairly well. My C++ class has finally started working with them. My teacher saved a class file for us to look at (it is in our W: drives and in our textbook) and I thought that I might try my hand at modifying it because it has a main() statement in the class file and I wanted to make the class executable from a client program. Everything has been going ok, I guess, execpt that now I have 1 error, and I can't figure out how to fix it. I will post the original, and then my modified version to see if you can help me:

*Original*

#include <iostream>

using std::cout;
using std::endl;


class Time
{
public:
Time(); // constructor
void setTime(int, int, int);
void printMilitary();
void printStandard();

private:
int hour;
int minute;
int second;

};


//Constructor which initializes each data member to zero
//Ensures all Time objects start in a consistent state
Time::Time()
{
hour = minute = second = 0;
}


// Set a new Time value using military time
// Sets invalid values to zero

void Time::setTime (int h, int m , int s)
{
hour = (( h >= 0 && h < 24) ? h : 0);

minute = (( m >= 0 && m <60) ? m : 0);

second = (( s >= 0 && s <60) ? s : 0);

}

// print time in military format

void Time::printMilitary()
{
cout << (hour < 10 ? "0" : "") << hour << ":"
<< (minute < 10 ? "0" : "") << minute;

}

// print time in standard format

void Time::printStandard()
{
cout << (( hour == 0 || hour == 12) ? 12 : hour %12)
<< ":" << ( minute < 10 ? "0" : "") << minute
<< ":" << ( second < 10 ? "0" : "") << second
<< (hour < 12 ? " AM" : " PM");

}

//Test our class

int main()
{

Time t; //initializing an object of type time

cout << "The intitial military time is ";
t.printMilitary();

cout << "\nThe intitial standard time is ";
t.printStandard();

t.setTime(13, 27, 6);

cout << "\n\nMilitary time after set time ";
t.printMilitary();

cout << "\nStandard time after set time ";
t.printStandard();

cout << endl;

return 0;

}


*Modified*

#include <iostream>

using std::cout;
using std::endl;


class Time
{

private:
int hour;
int minute;
int second;

public: Time()
{
cout << "Time set constructor" << endl;
hour = minute = second = 0;
}


public:
Timecalc();
void printMilitary();
void printStandard();

public: void setTime(int h, int, m, int s)
{
hour = (( h >= 0 && h < 24) ? h : 0);
minute = (( m >= 0 && m <60) ? m : 0);
second = (( s >= 0 && s <60) ? s : 0);
}

};


void Time::printMilitary()
{
cout << (hour < 10 ? "0" : "") << hour << ":"
<< (minute < 10 ? "0" : "") << minute;

}


void Time::printStandard()
{
cout << (( hour == 0 || hour == 12) ? 12 : hour %12)
<< ":" << ( minute < 10 ? "0" : "") << minute
<< ":" << ( second < 10 ? "0" : "") << second
<< (hour < 12 ? " AM" : " PM");

}


*Error for Modified*
W:\C++\time.cpp(27) : error C2061: syntax error : identifier 'm'

I know it's big, but does anyone have any ideas?
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Re: Working with classes, need help...

Postby pa3lsvt » Wed Apr 13, 2005 3:17 pm

Rarou11 wrote:public: void setTime(int h, int, m, int s)


You have an extra comma in there. The line should read:

public: void setTime(int h, int m, int s)

:)
pa3lsvt
 

Postby Roach412 » Wed Apr 13, 2005 9:10 pm

:mb_yeahthat::

-Roach
Lian Li Lancool First Knight Series PC-K59W
Intel Core i7-960 Bloomfield 3.2GHz
EVGA X58 FTW3 132-GT-E768-TR
EVGA GeForce GTX 1070 FTW GAMING ACX 3.0
G.SKILL Ripjaws Series 12GB (3 x 4GB)
Crucial M4 128gb SATAIII SSD x2
Crucial M4 256gb SATAIII SSD
OCZ Saber 1000 960GB Enterprise SSD
Corsair Professional Series HX850 PSU
Dell UltraSharp U2713HM 27" w/drop ceiling mount
Logitech G930 7.1 Headset
DBPOWER RGB LED Keyboard
Logitech G402 Hyperion Fury
Roach412
Black Belt 2nd Degree
Black Belt 2nd Degree
 
Posts: 2718
Joined: Mon Aug 09, 2004 7:33 pm
Location: Milwaukee - Wisconsin

Postby Snowfire » Thu Apr 14, 2005 10:10 am

Otay, that fixed the problem in the class file, and yet I'm still getting an error in my client program. Spot the problem and you win some joy of helping:

*Main Program*

#include <iostream>

using namespace std;

#include "time.cpp";

int main()
{

Time t;

cout << "The intitial military time is ";
t.printMilitary();

cout << "\nThe intitial standard time is ";
t.printStandard();

t.setTime(13, 27, 6);

cout << "\n\nMilitary time after set time ";
t.printMilitary();

cout << "\nStandard time after set time ";
t.printStandard();

cout << endl;

return 0;

}

*Error when compiling*

w:\c++\time main.cpp(5) : warning C4067: unexpected tokens following preprocessor directive - expected a newline


Well?
Tono Sien


Love is the greatest conspiracy, by the most loving genius.
Snowfire
Black Belt 1st Degree
Black Belt 1st Degree
 
Posts: 1486
Joined: Sun May 11, 2003 8:47 pm
Location: Omaha, NE

Postby pa3lsvt » Wed May 04, 2005 3:20 pm

Rarou11 wrote:*Main Program*

#include <iostream>

...

#include "time.cpp";


No semicolon after any statement that starts with '#'. Those statements are preprocessor directives and are delimited (finished) with a newline character (hitting enter). Conversely, the compiler expects the semicolon as the delimiter, which is why nearly every line of code ends that way. The preprocessor has no idea what to do with the extraneous semicolon and so it pukes on you.

The preprocessor does its thing before the compiler even sees the code and those lines don't exist when the compiler gets its turn at bat. The preprocessor takes the #include statement, looks for the file named as an include. (i.e. iostream in the default include path(s) on your system - the carets around the name tell it to look in its default path(s) only. Also, time.cpp in the current working directory - the quotes tell it to use the current working directory.) Once it finds the file, it replaces the #include statement with the contents of the file, then passes all that to the compiler. The compiler has no idea that they are seperate files.
pa3lsvt
 


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron