Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 4th, 2007, 4:39 AM   #1
cairo
Newbie
 
Join Date: Dec 2005
Posts: 18
Rep Power: 0 cairo is on a distinguished road
put method, main and member access specifiers separately

is it possible to put methods, main and member access specifiers separately? i mean example like:

a.cpp = methods
test.cpp=main
type.hh=member access specifiers

put whole in one folder to compile, i failed to do so in dev c++ 4.9.9.2
gave me error

Quote:
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.cpp" -o "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.exe" -ansi -pedantic -Wall -fexceptions -g3 -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" -g3
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x131): In function `main':

C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:10: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x13c):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:11: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x15f):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:18: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x17e):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:21: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x1b1):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:25: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x1e8):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:29: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x207):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:32: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x22d):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:36: undefined reference to `clockType::equalTime(clockType const&) const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x308):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:50: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x327):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:53: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x346):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:57: undefined reference to `clockType::incrementSeconds()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x375):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:61: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccG6baaa.o(.text+0x3a9):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:66: undefined reference to `clockType::getTime(int&, int&, int&) const'
collect2: ld returned 1 exit status

Execution terminated
and it work fine if i put methods and main together in one file.
cairo is offline   Reply With Quote
Old Jul 4th, 2007, 7:44 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You can distribute code over multiple .cpp files. You just have to make sure that each of those files has access to the necessary declarations. That's what your .h file is for. You should show a brief example of your files, including the #include statements.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 4th, 2007, 7:53 AM   #3
cairo
Newbie
 
Join Date: Dec 2005
Posts: 18
Rep Power: 0 cairo is on a distinguished road
member access specifiers

Quote:
//clockType.h, the specification file for the class clockType

class clockType
{
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the time.
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The function checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min;
// seconds = sec

void printTime() const;
//Function to print the time.
//Postcondition: The time is printed in the form
// hh:mm:ss.

void incrementSeconds();
//Function to increment the time by one second.
//Postcondition: The time is incremented by one
// second.
// If the before-increment time is 23:59:59, the
// time is reset to 00:00:00.

void incrementMinutes();
//Function to increment the time by one minute.
//Postcondition: The time is incremented by one
// minute.
// If the before-increment time is 23:59:53,
// the time is reset to 00:00:53.

void incrementHours();
//Function to increment the time by one hour.
//Postcondition: The time is incremented by one
// hour.
// If the before-increment time is 23:45:53, the
// time is reset to 00:45:53.

bool equalTime(const clockType& otherClock) const;
//Function to compare the two times.
//Postcondition: Returns true if this time is
// equal to otherClock; otherwise,
// returns false.

clockType(int hours, int minutes, int seconds);
//constructor with parameters
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The constructor checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

clockType();
//default constructor with parameters
//The time is set to 00:00:00.
//Postcondition: hr = 0; min = 0; sec = 0

private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};


methods

Quote:
//Implementation File for the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}

void clockType::incrementHours()
{
hr++;
if(hr > 23)
hr = 0;
}

void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}

void clockType::incrementSeconds()
{
sec++;

if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}

void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";

if (min < 10)
cout << "0";
cout << min << ":";

if (sec < 10)
cout << "0";
cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

clockType::clockType() //default constructor
{
hr = 0;
min = 0;
sec = 0;
}


tester
Quote:

//The user program that uses the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

int main()
{
clockType myClock;
clockType yourClock;

int hours;
int minutes;
int seconds;

//set the time of myClock
myClock.setTime(5, 4, 30); //Line 1

cout << "Line 2: myClock: "; //Line 2
myClock.printTime(); //print the time of myClock //Line 3
cout << endl; //Line 4

cout << "Line 5: yourClock: "; //Line 5
yourClock.printTime(); //print the time of yourClock Line 6
cout << endl; //Line 7

//set the time of yourClock
yourClock.setTime(5, 45, 16); //Line 8

cout << "Line 9: After setting, yourClock: "; //Line 9
yourClock.printTime(); //print the time of yourClock Line 10
cout << endl; //Line 11

//compare myClock and yourClock
if (myClock.equalTime(yourClock)) //Line 12
cout << "Line 13: Both times are equal."
<< endl; //Line 13
else //Line 14
cout << "Line 15: The two times are not equal."
<< endl; //Line 15

cout << "Line 16: Enter the hours, minutes, and "
<< "seconds: "; //Line 16
cin >> hours >> minutes >> seconds; //Line 17
cout << endl; //Line 18

//set the time of myClock using the value of the
//variables hours, minutes, and seconds
myClock.setTime(hours, minutes, seconds); //Line 19

cout << "Line 20: New myClock: "; //Line 20
myClock.printTime(); //print the time of myClock //Line 21
cout << endl; //Line 22

//increment the time of myClock by one second
myClock.incrementSeconds(); //Line 23

cout << "Line 24: After incrementing myClock by "
<< "one second, myClock: "; //Line 24
myClock.printTime(); //print the time of myClock //Line 25
cout << endl; //Line 26

//retrieve the hours, minutes, and seconds of the
//object myClock
myClock.getTime(hours, minutes, seconds); //Line 27

//output the value of hours, minutes, and seconds
cout << "Line 28: hours = " << hours
<< ", minutes = " << minutes
<< ", seconds = " << seconds << endl; //Line 28

system("pause");
return 0;
}//end main
example like this program, dev c++ cant work if i arrange the files like above
cairo is offline   Reply With Quote
Old Jul 4th, 2007, 8:06 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Hard to read that, since it's in quote tags and not code tags. You say in one place that the include file is named "type.hh", but you include "clockType.h". That's either a problem in your code or inaccurate posting.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 4th, 2007, 10:49 AM   #5
cairo
Newbie
 
Join Date: Dec 2005
Posts: 18
Rep Power: 0 cairo is on a distinguished road
errr.. im sorry.. i re-post again, i must be out of my mind -_-!


clockType.h is the member access specifiers i state just now
class clockType
{
public:
    void setTime(int hours, int minutes, int seconds);
       //Function to set the time.
       //The time is set according to the parameters.
       //Postcondition: hr = hours; min = minutes; 
       //               sec = seconds
       //   The function checks whether the values of 
       //   hours, minutes, and seconds are valid. If a 
       //   value is invalid, the default value 0 is 
       //   assigned.

    void getTime(int& hours, int& minutes, int& seconds) const;
       //Function to return the time.
       //Postcondition: hours = hr; minutes = min;
       //               seconds = sec

    void printTime() const;
       //Function to print the time.
       //Postcondition: The time is printed in the form
       //               hh:mm:ss.

    void incrementSeconds();
       //Function to increment the time by one second.
       //Postcondition: The time is incremented by one 
       //               second.
       //   If the before-increment time is 23:59:59, the 
       //   time is reset to 00:00:00.

    void incrementMinutes();
       //Function to increment the time by one minute.
       //Postcondition: The time is incremented by one 
       //               minute. 
       //   If the before-increment time is 23:59:53,
       //   the time is reset to 00:00:53.

    void incrementHours();
       //Function to increment the time by one hour.
       //Postcondition: The time is incremented by one
       //               hour. 
       //   If the before-increment time is 23:45:53, the 
       //   time is reset to 00:45:53.

    bool equalTime(const clockType& otherClock) const;
       //Function to compare the two times.
       //Postcondition: Returns true if this time is 
       //               equal to otherClock; otherwise, 
       //               returns false.

    clockType(int hours, int minutes, int seconds);
       //constructor with parameters
       //The time is set according to the parameters.
       //Postcondition: hr = hours; min = minutes; 
       //               sec = seconds
       //   The constructor checks whether the values of 
       //   hours, minutes, and seconds are valid. If a 
       //   value is invalid, the default value 0 is 
       //   assigned.

    clockType();
       //default constructor with parameters
       //The time is set to 00:00:00.
       //Postcondition: hr = 0; min = 0; sec = 0

private:
    int hr;  //variable to store the hours
    int min; //variable to store the minutes
    int sec; //variable to store the seconds
};


clockTypeImp.cpp is the methods i mean just now
//Implementation File for the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)
{
	if (0 <= hours && hours < 24)
	    hr = hours;
	else 
	    hr = 0;

	if (0 <= minutes && minutes < 60)
	    min = minutes;
	else 
	    min = 0;

	if (0 <= seconds && seconds < 60)
	    sec = seconds;
	else 
	    sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

void clockType::incrementHours()
{
	hr++;
	if(hr > 23)
 	   hr = 0;
}

void clockType::incrementMinutes()
{
	min++;
	if (min > 59)
	{
	    min = 0;
	    incrementHours();
	}
}

void clockType::incrementSeconds()
{
	sec++;

	if (sec > 59)
	{
	    sec = 0;
	    incrementMinutes();
	}
}

void clockType::printTime() const
{
	if (hr < 10)
	    cout << "0";
	cout << hr << ":";

	if (min < 10)
	    cout << "0";
	cout << min << ":";

	if (sec < 10)
	   cout << "0";
	cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
	return (hr == otherClock.hr 
		    && min == otherClock.min 
		    && sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
	if (0 <= hours && hours < 24)
		hr = hours;
	else 
		hr = 0;

	if (0 <= minutes && minutes < 60)
		min = minutes;
	else 
		min = 0;

	if (0 <= seconds && seconds < 60)
		sec = seconds;
	else 
		sec = 0;
}

clockType::clockType()  //default constructor
{
	hr = 0;
	min = 0;
	sec = 0;
}


testClockClass.cpp is the main program
//The user program that uses the class clockType

#include <iostream>
#include "clockType.h"

using namespace std;

int main()
{
    clockType myClock;
    clockType yourClock;  

    int hours;
    int minutes;
    int seconds;

       //set the time of myClock
    myClock.setTime(5, 4, 30);                          //Line 1

    cout << "Line 2: myClock: ";                        //Line 2
    myClock.printTime();    //print the time of myClock //Line 3
    cout << endl;                                       //Line 4

    cout << "Line 5: yourClock: ";                      //Line 5
    yourClock.printTime(); //print the time of yourClock  Line 6
    cout << endl;                                       //Line 7

       //set the time of yourClock
    yourClock.setTime(5, 45, 16);                       //Line 8

    cout << "Line 9: After setting, yourClock: ";       //Line 9
    yourClock.printTime(); //print the time of yourClock  Line 10
    cout << endl;                                       //Line 11

       //compare myClock and yourClock
    if (myClock.equalTime(yourClock))                   //Line 12
        cout << "Line 13: Both times are equal."
             << endl;                                   //Line 13
    else                                                //Line 14
        cout << "Line 15: The two times are not equal."
             << endl;                                   //Line 15

    cout << "Line 16: Enter the hours, minutes, and "
         << "seconds: ";                                //Line 16
    cin >> hours >> minutes >> seconds;                 //Line 17
    cout << endl;                                       //Line 18

       //set the time of myClock using the value of the
       //variables hours, minutes, and seconds
    myClock.setTime(hours, minutes, seconds);           //Line 19

    cout << "Line 20: New myClock: ";                   //Line 20
    myClock.printTime();    //print the time of myClock //Line 21
    cout << endl;                                       //Line 22

       //increment the time of myClock by one second
    myClock.incrementSeconds();                         //Line 23

    cout << "Line 24: After incrementing myClock by " 
         << "one second, myClock: ";                    //Line 24
    myClock.printTime();    //print the time of myClock //Line 25
    cout << endl;                                       //Line 26

      //retrieve the hours, minutes, and seconds of the 
      //object myClock
    myClock.getTime(hours, minutes, seconds);           //Line 27

      //output the value of hours, minutes, and seconds
    cout << "Line 28: hours = " << hours 
         << ", minutes = " << minutes 
         << ", seconds = " << seconds << endl;          //Line 28

system("pause");
    return 0;
}//end main

i take these three files to compile just now by using Dev c++, it didn't work and gave me error

Quote:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.cpp" -o "C:\Documents and Settings\Compaq_Owner\My Documents\SIT\6044-6\Chapter 11\Source Code\Ch11_ClassClock\testClockClass.exe" -ansi -pedantic -Wall -fexceptions -g3 -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" -g3
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x131): In function `main':

C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:10: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x13c):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:11: undefined reference to `clockType::clockType()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x15f):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:18: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x17e):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:21: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x1b1):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:25: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x1e8):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:29: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x207):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:32: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x22d):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:36: undefined reference to `clockType::equalTime(clockType const&) const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x308):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:50: undefined reference to `clockType::setTime(int, int, int)'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x327):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:53: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x346):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:57: undefined reference to `clockType::incrementSeconds()'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x375):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:61: undefined reference to `clockType::printTime() const'
C:\DOCUME~1\COMPAQ~1\LOCALS~1\Temp/ccaOcaaa.o(.text+0x3a9):C:/Documents and Settings/Compaq_Owner/My Documents/SIT/6044-6/Chapter 11/Source Code/Ch11_ClassClock/testClockClass.cpp:66: undefined reference to `clockType::getTime(int&, int&, int&) const'
collect2: ld returned 1 exit status

Execution terminated
i try again by putting the all the methods of this program which is clockTypeImp.cpp inside tester file which is testClockClass.cpp and its work
cairo is offline   Reply With Quote
Old Jul 4th, 2007, 12:47 PM   #6
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
It would be better to include some #ifndef`s to the clockType.h to avoid multiple inlusions.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jul 4th, 2007, 2:54 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
This code compiles and runs for me. Note the additions in red. Always test your input for success. Suppose your user enters, for time, "A". Hard to stash that in an integer, cin won't like you anymore. Also note that the input test is not exhaustive. Suppose that the user enters fewer than the number of expected values, or adds colons, or something. Your program is broken. But those additions are YOUR job.

Note that a grouchier respondent than I would probably want to beat you to death for forcing them to reformat because you didn't use code tags until after I had copied and pasted your code.

clockType.h
class clockType
{
    public:
    void setTime(int hours, int minutes, int seconds);
    void getTime(int& hours, int& minutes, int& seconds) const;
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType& otherClock) const;
    clockType (int hours, int minutes, int seconds);
    clockType();
private:
    int hr;
    int min;
    int sec;
};
clockType.cpp
#include <iostream>
#include "clockType.h"

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

void clockType::setTime (int hours, int minutes, int seconds)
{
    if (0 <= hours && hours < 24) hr = hours;
    else hr = 0;

    if (0 <= minutes && minutes < 60) min = minutes;
    else min = 0;

    if (0 <= seconds && seconds < 60) sec = seconds;
    else sec = 0;
}

void clockType::getTime (int& hours, int& minutes, int& seconds) const
{
    hours = hr;
    minutes = min;
    seconds = sec;
}

void clockType::incrementHours()
{
    hr++;
    if(hr > 23) hr = 0;
}

void clockType::incrementMinutes()
{
    min++;
    if (min > 59)
    {
        min = 0;
        incrementHours();
    }
}

void clockType::incrementSeconds()
{
    sec++;
    if (sec > 59)
    {
        sec = 0;
        incrementMinutes();
    }
}

void clockType::printTime() const
{
    if (hr < 10) cout << "0";
    cout << hr << ":";

    if (min < 10) cout << "0";
    cout << min << ":";

    if (sec < 10) cout << "0";
    cout << sec;
}

bool clockType::equalTime (const clockType& otherClock) const
{
    return (hr == otherClock.hr
        && min == otherClock.min
        && sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
    if (0 <= hours && hours < 24) hr = hours;
    else hr = 0;

    if (0 <= minutes && minutes < 60) min = minutes;
    else min = 0;

    if (0 <= seconds && seconds < 60) sec = seconds;
    else sec = 0;
}

clockType::clockType() //default constructor
{
    hr = 0;
    min = 0;
    sec = 0;
}
main.cpp
#include <iostream>
#include "clockType.h"
#include <string>

using std::cout;
using std::cin;
using std::cerr;
using std::endl;

int ohHell (std::string trouble)
{
    cerr << "Trouble in River City: " << trouble << endl;
    return 1;
}
int main ()
{
    clockType myClock;
    clockType yourClock;

    int hours;
    int minutes;
    int seconds;

    myClock.setTime (5, 4, 30);
    cout << "myClock: ";
    myClock.printTime ();
    cout << endl;

    cout << "yourClock: ";
    yourClock.printTime ();
    cout << endl;

    yourClock.setTime (5, 45, 16);

    cout << "After setting, yourClock: ";
    yourClock.printTime ();
    cout << endl;

    if (myClock.equalTime (yourClock)) cout << "Equal." << endl;
    else cout << "Not equal." << endl;

    cout << "Enter the hours, minutes, and seconds: ";
    cin >> hours >> minutes >> seconds;
    if (!cin.good ()) return ohHell ("input failed");
    cout << endl;

    myClock.setTime (hours, minutes, seconds);

    cout << "New myClock: ";
    myClock.printTime ();
    cout << endl;

    myClock.incrementSeconds ();

    cout << "After incrementing myClock by one second, myClock: ";
    myClock.printTime ();
    cout << endl;

    myClock.getTime (hours, minutes, seconds);

    cout << "Hours = " << hours
    << ", minutes = " << minutes
    << ", seconds = " << seconds << endl;

    return 0;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 4th, 2007, 5:40 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
You need to compile each of your .cpp programs and then link them together. Currently you are just compiling the main .cpp without linking in the class .cpp.

It should be as simple as putting both .cpp file names at the start of the compile command line.
The Dark is offline   Reply With Quote
Old Jul 5th, 2007, 9:14 AM   #9
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
From what i notice, It doesn't read the clockTypeImp.cpp file ( you never tell it to look at it )

For seperate class files: one .h/.hpp and .cpp with the same name. Example Clock.hpp & Clock.cpp.

Notice how Dawei had the three files: clocktype.cpp clocktype.h main.cpp

I haven;t seen a header file go to a code file with a different name before.

Hope that helps... I suck at explaining things.
Ben.Dougall is offline   Reply With Quote
Old Jul 5th, 2007, 11:28 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Ben, source file names don't matter at all when using C/C++. That is your Java background showing
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC