![]() |
| | #1 |
| Kill Clear Channel Join Date: Oct 2005 Location: St. Paul, MN
Posts: 23
| Linking problems, class problems Here's what I have. There are 3 files. I made sure they're all included in the same project before compiling. Code: /***************************************
CTime.h
by Meghan
11/17/05
Lab 10
Contains the definition of the class
CTime.
****************************************/
class CTime
{
public:
CTime(); //default constructor
CTime(int, int, int); //constructor
~CTime(); //destructor
void getHours(int&) const; //function prototypes
void getMinutes(int&) const;
void getSeconds(int&) const;
void setHours(int);
void setMinutes(int);
void setSeconds(int);
CTime add(CTime);
private:
int hours;
int minutes;
int seconds;
};
CTime::CTime() //default constructor
{
hours = 0;
minutes = 0;
seconds = 0;
}
inline CTime::CTime(int hr, int min, int sec) //constructor
{
hr = 0;
min = 0;
sec = 0;
}
inline void CTime::getHours(int& hr) const
{
hr = hours;
}
inline void CTime::getMinutes(int& min) const
{
min = minutes;
}
inline void CTime::getSeconds(int& sec) const
{
sec = seconds;
}
inline void CTime::setHours(int hr)
{
if (hr<=60)
hr = hours;
else
hr = 0;
}
inline void CTime::setMinutes(int min)
{
if (min<=60)
min = minutes;
else
min = 0;
}
inline void CTime::setSeconds(int sec)
{
if (sec<=60)
sec = seconds;
else
sec = 0;
}
Code: /***************************************
CTime.cpp
by Meghan
11/17/05
Lab 10
Contains the CTime member function that
adds the two times together.
****************************************/
#include <cstdlib>
#include <iostream>
#include "CTime.h"
using namespace std;
CTime CTime::add(CTime t2)
{
long t1Seconds;
long t2Seconds;
long t3Seconds;
CTime temp;
//converts time1 from hours:minutes:seconds to seconds
t1Seconds = (long)hours*3600 + (long)minutes*60 + (long)seconds;
//converts time2 from hours:minutes:seconds to seconds
t2Seconds = (long)t2.hours*3600 + (long)t2.minutes*60 + (long)t2.seconds;
//adds time1 and time2
t3Seconds = t1Seconds + t2Seconds;
temp.hours = t3Seconds/60;
temp.minutes = (t3Seconds%60)/60;
temp.seconds = (t3Seconds%60)%60;
return temp;
}
Code: /***************************************
TimeApp.cpp
by Meghan
11/17/05
Lab 10
A program that gets two time values
from the user, adds them, and displays
the result.
****************************************/
#include <cstdlib>
#include <iostream>
#include "CTime.h"
using namespace std;
int main()
{
CTime time1; //first time value from user input
CTime time2; //second time value from user input
CTime sumTime; //sum of times the user entered
char ch; //holds the colon character
int hrs1; //hours value of time1
int min1; //minutes value of time1
int sec1; //seconds value of time1
int hrs2; //hours value of time2
int min2; //minutes value of time2
int sec2; //seconds value of time2
int sumHrs; //hours value of sum
int sumMin; //minutes value of sum
int sumSec; //seconds value of sum
cout << "Enter a time in 00:00:00 format:" << endl;
cin >> hrs1;
cin.get(ch); //gets the colon character
cin >> min1;
cin.get(ch);
cin >> sec1;
time1.setHours(hrs1); //stores user input in hours value
time1.setMinutes(min1); //stores user input in minutes value
time1.setSeconds(sec1); //stores user input in seconds value
cout << "Enter another time in the same format:" << endl;
cin >> hrs2;
cin.get(ch); //gets the colon character
cin >> min2;
cin.get(ch);
cin >> sec2;
time2.setHours(hrs2); //stores user input in hours value
time2.setMinutes(min2); //stores user input in minutes value
time2.setSeconds(sec2); //stores user input in seconds value
sumTime = time1.add(time2); //adds the two times together
sumTime.getHours(sumHrs);
sumTime.getMinutes(sumMin);
sumTime.getSeconds(sumSec);
cout << "The sum of " << hrs1 << ":" << min1 << ":";
cout << sec1 << " and " << hrs2 << ":" << min2;
cout << ":" << sec2 << " is " << sumHrs << ":";
cout << sumMin << ":" << sumSec << endl;
return 0;
}
"multiple definition of `CTime::CTime()' " " first defined here " "[Linker error] undefined reference to `CTime::~CTime()' " See anything I'm doing wrong? Any ideas are appreciated. Thanks.
__________________ to Absent Toast |
| Kheila is offline | |
| | #2 |
| ^ Read Backwards^ Join Date: Sep 2005 Location: Earth
Posts: 282
| |
| Enahs is offline | |
| | #3 |
| Kill Clear Channel Join Date: Oct 2005 Location: St. Paul, MN
Posts: 23
| Hmm. That's good to know (wish that had been covered in class). It's kind of unclear exactly where to place this, here's what I did, and I know it's wrong because the compiler still doesn't like it: Code: /***************************************
CTime.h
by Meghan
11/17/05
Lab 10
Contains the definition of the class
CTime.
****************************************/
#ifndef CTIME_H
#define CTIME_H
#endif
class CTime
{
public:
CTime(); //default constructor
CTime(int, int, int); //constructor
~CTime(); //destructor
void getHours(int&) const; //function prototypes
void getMinutes(int&) const;
void getSeconds(int&) const;
void setHours(int);
void setMinutes(int);
void setSeconds(int);
CTime add(CTime);
private:
int hours;
int minutes;
int seconds;
};
CTime::CTime() //default constructor
{
hours = 0;
minutes = 0;
seconds = 0;
}
inline CTime::CTime(int hr, int min, int sec) //constructor
{
hr = 0;
min = 0;
sec = 0;
}
inline void CTime::getHours(int& hr) const
{
hr = hours;
}
inline void CTime::getMinutes(int& min) const
{
min = minutes;
}
inline void CTime::getSeconds(int& sec) const
{
sec = seconds;
}
inline void CTime::setHours(int hr)
{
if (hr<=60)
hr = hours;
else
hr = 0;
}
inline void CTime::setMinutes(int min)
{
if (min<=60)
min = minutes;
else
min = 0;
}
inline void CTime::setSeconds(int sec)
{
if (sec<=60)
sec = seconds;
else
sec = 0;
}
__________________ to Absent Toast Last edited by Kheila; 11-22-2005 at 12:02 AM. |
| Kheila is offline | |
| | #4 |
| Registered User Join Date: Apr 2003
Posts: 2,662
| Code: #ifndef CTIME_H #define CTIME_H //Your header file code here #endif Last edited by 7stud; 11-22-2005 at 12:04 AM. |
| 7stud is offline | |
| | #5 |
| Kill Clear Channel Join Date: Oct 2005 Location: St. Paul, MN
Posts: 23
| Ah, got it. It seems to be working now. There's another problem, though: in the CTime.cpp file, I need the function add() to return a value of the type CTime. Is this possible? If so, what do I need to change in that bit of code to get it to return a value of the type CTime? Right now the compiler says I don't name a type.
__________________ to Absent Toast |
| Kheila is offline | |
| | #6 |
| Registered User Join Date: Apr 2003
Posts: 2,662
| It looks like it does: Code: CTime CTime::add(CTime t2)
{
long t1Seconds;
long t2Seconds;
long t3Seconds;
CTime temp;
//converts time1 from hours:minutes:seconds to seconds
t1Seconds = (long)hours*3600 + (long)minutes*60 + (long)seconds;
//converts time2 from hours:minutes:seconds to seconds
t2Seconds = (long)t2.hours*3600 + (long)t2.minutes*60 + (long)t2.seconds;
//adds time1 and time2
t3Seconds = t1Seconds + t2Seconds;
temp.hours = t3Seconds/60;
temp.minutes = (t3Seconds%60)/60;
temp.seconds = (t3Seconds%60)%60;
return temp;
}
|
| 7stud is offline | |
| | #7 |
| Kill Clear Channel Join Date: Oct 2005 Location: St. Paul, MN
Posts: 23
| I thought so too, but the compiler says "`CTime' does not name a type ".
__________________ to Absent Toast |
| Kheila is offline | |
| | #8 |
| Registered User Join Date: Apr 2003
Posts: 2,662
| Are you still including CTime.h in your .cpp file? Code: #include <cstdlib> #include <iostream> #include "CTime.h" using namespace std; CTime CTime::add(CTime t2) |
| 7stud is offline | |
| | #9 |
| Registered User Join Date: Apr 2003
Posts: 2,662
| Post the error and mark the line number with a comment in your code. |
| 7stud is offline | |
| | #10 |
| Kill Clear Channel Join Date: Oct 2005 Location: St. Paul, MN
Posts: 23
| Yes, but I put the #ifndef etc. statements in the CTime.cpp file as well as in the header file, because otherwise it still includes the header file multiple times. This seems like it might be why it doesn't recognize the CTime data type. So...either way it doesn't work. This is the exact wording of the compiler error: "CTime.cpp:18: error: `CTime' does not name a type"Here's the code, I marked where the error occurred: Code: /***************************************
CTime.cpp
by Meghan
11/17/05
Lab 10
Contains the CTime member function that
adds the two times together.
****************************************/
#include <cstdlib>
#include <iostream>
#ifndef CTIME_H
#define CTIME_H
#include "CTime.h"
using namespace std;
/
CTime CTime::add(CTime t2) //this line contains the error
{
long t1Seconds;
long t2Seconds;
long t3Seconds;
CTime temp;
//converts time1 from hours:minutes:seconds to seconds
t1Seconds = (long)hours*3600 + (long)minutes*60 + (long)seconds;
//converts time2 from hours:minutes:seconds to seconds
t2Seconds = (long)t2.hours*3600 + (long)t2.minutes*60 + (long)t2.seconds;
//adds time1 and time2
t3Seconds = t1Seconds + t2Seconds;
temp.hours = t3Seconds/60;
temp.minutes = (t3Seconds%60)/60;
temp.seconds = (t3Seconds%60)%60;
return temp;
}
#endif
__________________ to Absent Toast |
| Kheila is offline | |
| | #11 | ||
| Registered User Join Date: Apr 2003
Posts: 2,662
| Quote:
Inclusion guards are only used for header files. You only include header files so there is no possible duplication for your .cpp files. As long as your class declaration is included in main(), the magic of the compiling/linking process will make the connection between the funtion calls you make in main(), to their declarations that are included at the top of the file to their definitions in a separate .cpp file.Quote:
#ifndef CTIME_H will be false. Therefore the compiler enters the if-endif block and includes everything. But, as it does that the compiler encounters the line that says: #define CTIME_H That means that something called CTIME_H now exists. The next time the compiler encounters #include "CTime.h" it looks at the header file again, but this time the statement: #ifndef CTIME_H evaluates to false, so the whole if-endif block is skipped, so the compiler includes a blank file. That means the declarations won't be included more than once, and you won't get any compiler errors saying you are redeclaring the same variables. Last edited by 7stud; 11-22-2005 at 01:00 AM. | ||
| 7stud is offline | |
| | #12 |
| Kill Clear Channel Join Date: Oct 2005 Location: St. Paul, MN
Posts: 23
| Ok, I took them out, but now I'm still getting errors saying "multiple definition of `CTime::CTime()' " like I got before I put them in the header file. What you explained makes sense, which is why I don't understand why I'm still getting the same errors. Argh. I'm going to continue working on this tomorrow morning. Thanks for all your help!
__________________ to Absent Toast Last edited by Kheila; 11-22-2005 at 01:09 AM. |
| Kheila is offline | |
| | #13 |
| Registered User Join Date: Apr 2003
Posts: 2,662
| Ok. The problem is that you have the inline function definitions in the same header file as your class declaration, so when you include the header file in CTime.cpp in order to get the class declaration in there, you end up getting the inline function definitions in there(remember an include statement is replaced with the contents of the file). You also have to include your class declaration in the file containing main(), so you end up getting the inline function definitions in main() as well. The result is you have functions with the same name defined in two different files, and that is not allowed, so that is producing the error you are seeing. You're allowed to include the declaration for your class in more than one file, but you're only allowed to have the definition of a function in a single file. So, try putting your inline functions in a separate header file and only include that header file in one file: main(). The inclusion guards won't help in that header file because they won't prevent you from including a header file in multiple files, they only prevent you from including a header file multiple times in the same file. Last edited by 7stud; 11-22-2005 at 02:05 AM. |
| 7stud is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting an error with OpenGL: collect2: ld returned 1 exit status | Lorgon Jortle | C++ Programming | 6 | 05-08-2009 08:18 PM |
| Linking error with static template class members | cunnus88 | C++ Programming | 6 | 04-02-2009 12:31 PM |
| Derived class linking error | Enahs | C++ Programming | 3 | 11-12-2005 10:18 PM |
| base class pointer problems | ... | C++ Programming | 3 | 11-16-2003 11:27 PM |
| structure vs class | sana | C++ Programming | 13 | 12-02-2002 07:18 AM |