|
- //mydate.h
- #ifndef MYDATE_H
- #define MYDATE_H
- #include<iostream>
- class mydate{
- public:
- mydate( short unsigned = 1900, short unsigned = 1, short unsigned = 1 );
- short unsigned gety() const;
- short unsigned getm() const;
- short unsigned getd() const;
- private:
- short unsigned year, month, day;
- };
- #endif //MYDATE_H
- //mydate.cpp
- #include"mydate.h"
- #include<iostream>
- #include<iomanip>
- mydate::mydate( short unsigned y , short unsigned m , short unsigned d )
- {
- year = y;
- month = m;
- day = d;
- }
- inline short unsigned mydate::gety() const
- { return year; }
- inline short unsigned mydate::getm() const
- { return month; }
- inline short unsigned mydate::getd() const
- { return day; }
-
- //main.cpp
- #include"mydate.h"
- #include<iostream>
- using namespace std;
- int main()
- {
- mydate md( 2003, 7, 21 );
- cout << md.gety() << endl;
- }
复制代码
>g++ -o main mydate.cpp main.cpp
/tmp/cclXKKOO.o(.text+0x38): In function `main':
: undefined reference to `mydate::gety() const'
collect2: ld returned 1 exit status
如果写在一个文件里面:
- //main.cpp
- #include<iostream>
- using namespace std;
- class mydate{
- public:
- mydate( short unsigned = 1900, short unsigned = 1, short unsigned = 1 );
- short unsigned gety() const;
- short unsigned getm() const;
- short unsigned getd() const;
- private:
- short unsigned year, month, day;
- };
- int main()
- {
- mydate md(2003, 7, 21 );
- cout << md.gety() << endl;
- }
- mydate::mydate( short unsigned y , short unsigned m , short unsigned d )
- {
- year = y;
- month = m;
- day = d;
- }
- inline short unsigned mydate::gety() const
- { return year; }
- inline short unsigned mydate::getm() const
- { return month; }
- inline short unsigned mydate::getd() const
- { return day; }
复制代码
>g++ -o main main.cpp
通过
请问这时什么原因? |
|