LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 620|回复: 2

分开放在几个文件里面就连接通不过了??

[复制链接]
发表于 2003-7-21 21:41:39 | 显示全部楼层 |阅读模式

  1. //mydate.h
  2. #ifndef MYDATE_H
  3. #define MYDATE_H

  4. #include<iostream>


  5. class mydate{
  6. public:
  7.         mydate( short unsigned = 1900, short unsigned = 1, short unsigned = 1 );
  8.         short unsigned gety() const;
  9.         short unsigned getm() const;
  10.         short unsigned getd() const;

  11. private:
  12.         short unsigned year, month, day;
  13. };

  14. #endif //MYDATE_H

  15. //mydate.cpp
  16. #include"mydate.h"
  17. #include<iostream>
  18. #include<iomanip>

  19. mydate::mydate( short unsigned y , short unsigned m , short unsigned d )
  20. {
  21.         year = y;
  22.         month = m;
  23.         day = d;
  24. }


  25. inline short unsigned mydate::gety() const
  26. { return year; }

  27. inline short unsigned mydate::getm() const
  28. { return month; }

  29. inline short unsigned mydate::getd() const
  30. { return day; }

  31. //main.cpp
  32. #include"mydate.h"
  33. #include<iostream>
  34. using namespace std;


  35. int main()
  36. {
  37.         mydate md( 2003, 7, 21 );
  38.         cout << md.gety() << endl;
  39. }

复制代码


>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

如果写在一个文件里面:

  1. //main.cpp
  2. #include<iostream>
  3. using namespace std;


  4. class mydate{
  5. public:
  6.         mydate( short unsigned  = 1900, short unsigned  = 1, short unsigned  = 1 );
  7.         short unsigned gety() const;
  8.         short unsigned getm() const;
  9.         short unsigned getd() const;

  10. private:
  11.         short unsigned year, month, day;
  12. };

  13. int main()
  14. {
  15.         mydate md(2003, 7, 21 );
  16.         cout << md.gety() << endl;
  17. }
  18. mydate::mydate( short unsigned y , short unsigned m , short unsigned d )
  19. {
  20.         year = y;
  21.         month = m;
  22.         day = d;
  23. }


  24. inline short unsigned mydate::gety() const
  25. { return year; }

  26. inline short unsigned mydate::getm() const
  27. { return month; }

  28. inline short unsigned mydate::getd() const
  29. { return day; }
复制代码


>g++ -o main main.cpp
通过

请问这时什么原因?
发表于 2003-7-21 22:16:24 | 显示全部楼层
inline函数必须和声明在一个文件里,你可以把inline函数写在.h文件里,不要写在.cpp文件里。
 楼主| 发表于 2003-7-21 23:20:16 | 显示全部楼层
谢谢
解决了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表