鱼C论坛

 找回密码
 立即注册
查看: 1378|回复: 6

[已解决]C++分散编译问题

[复制链接]
发表于 2021-5-13 11:20:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
C++调用类头文件为什么只需要include声明部分而不需要类的定义部分
比如C++中新建一个r.h的头文件 ,并在头文件中声明了一个类,
然后添加一个源文件r.cpp来定义这个类,定义中使用#include “r.h”;
然后再建一个m.cpp中实现主函数,使用#include "r.h",不用#include"r.cpp",就能运行得到结果。
最佳答案
2021-5-14 20:53:00
一世轻尘 发表于 2021-5-14 20:37
这是三个文件的内容
Dtime.h:

cpp文件是通过命令行传递给编译器的,不是通过 #include 指令添加到另一个cpp文件中的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-13 11:29:48 | 显示全部楼层
你怎么编译的?这样吗?
g++ -g -Wall -o main m.cpp
这样肯定无法通过编译,会提示你找不到 r.cpp 中的函数

应该是这样
g++ -g -Wall -o main m.cpp r.cpp
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-14 20:33:45 | 显示全部楼层
人造人 发表于 2021-5-13 11:29
你怎么编译的?这样吗?
g++ -g -Wall -o main m.cpp
这样肯定无法通过编译,会提示你找不到 r.cpp 中的 ...

嗯,这个我知道,不是这个问题
我是在测试分散编译过程中用匿名名称空间来隐藏辅助函数的过程中发现的这个问题,以前没有注意,就是这里有三个文件,接口文件:Dtime.h,实现文件:Dtime.cpp,驱动文件:main.cpp,在Dtime.cpp文件中我定义了一个辅助函数readHour来辅助实现Dtime.h中声明的类的一些方法,这个函数没有被用户访问的必要,所以把它放在Dtime.cpp的匿名名称空间里面.然后在主函数里定义了另一个同名readHour函数,此时我用include"Dtime.cpp"的时候,发现出现了readHour重复定义的问题,
然后我就发现书中的main文件只包含了#include"Dtime.h"接口文件,而没有include"Dtime.cpp"文件.所以就想问主函数里为什么可以不用include"Dtime.cpp",如果没有这条语句,那Dtime.h文件里声明的函数不就都没有定义了吗,为什么程序还是可以运行,程序是怎么知道从哪里去找头文件中函数的实现的?

我用的是vscode,下面是其中的tasks.json文件,之前我是用注释掉的哪一行语句来直接编译所有该目录下所有文件的,所以没发现这个问题,现在换成第10行和第12行的那两条语句,编译了Dtime.cpp文件,但主函数中并没有#include"Dtime.cpp",那它们是怎么连接的

1.png
2.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-14 20:37:05 | 显示全部楼层
人造人 发表于 2021-5-13 11:29
你怎么编译的?这样吗?
g++ -g -Wall -o main m.cpp
这样肯定无法通过编译,会提示你找不到 r.cpp 中的 ...

这是三个文件的内容
Dtime.h:
  1. #ifndef DTIME_H
  2. #define DTIME_H

  3. #include <iostream>
  4. using std::istream;
  5. using std::ostream;

  6. namespace DTimeSavitch
  7. {
  8.     class DigitalTime
  9.     {
  10.     public:
  11.         DigitalTime(int theHour, int theMinute);
  12.         DigitalTime();

  13.         int getHour() const;
  14.         int getMinute() const;

  15.         void advance(int minutesAdded);
  16.         void advance(int houtsAdded, int minutesAdded);
  17.         friend bool operator==(const DigitalTime &time1, const DigitalTime &time2);
  18.         friend istream &operator>>(istream &ins, DigitalTime &theObject);
  19.         friend ostream &operator<<(ostream &outs, const DigitalTime &theObject);

  20.     private:
  21.         int hour;
  22.         int minute;
  23.     };
  24. }
  25. #endif
复制代码

Dtime.cpp:
  1. #include <iostream>
  2. #include <cctype>
  3. #include <cstdlib>
  4. using std::cin;
  5. using std::cout;
  6. using std::istream;
  7. using std::ostream;
  8. #include "Dtime.h"

  9. namespace
  10. {
  11.     int digitToInt(char c)
  12.     {
  13.         return (int(c) - int('0'));
  14.     }

  15.     void readMinute(int &theMinute)
  16.     {
  17.         char c1, c2;
  18.         cin >> c1 >> c2;
  19.         if (!(isdigit(c1) && isdigit(c2)))
  20.         {
  21.             cout << "Error: illegal input to reaadMinute\n";
  22.             exit(1);
  23.         }

  24.         theMinute = digitToInt(c1) * 10 + digitToInt(c2);

  25.         if (theMinute < 0 || theMinute > 59)
  26.         {
  27.             cout << "Error: illegal input to readMinute\n";
  28.             exit(1);
  29.         }
  30.     }

  31.     void readHour(int &theHour)
  32.     {
  33.         char c1, c2;
  34.         cin >> c1 >> c2;
  35.         if (!(isdigit(c1) && (isdigit(c2) || c2 == ':')))
  36.         {
  37.             cout << "Error: illegal input to readHour\n";
  38.             exit(1);
  39.         }
  40.         if (isdigit(c1) && c2 == ':')
  41.         {
  42.             theHour = digitToInt(c1);
  43.         }
  44.         else
  45.         {
  46.             theHour = digitToInt(c1) * 10 + digitToInt(c2);
  47.             cin >> c2;
  48.             if (c2 != ':')
  49.             {
  50.                 cout << "Error: illegal input to readHour\n";
  51.                 exit(1);
  52.             }
  53.         }
  54.         if (theHour == 24)
  55.             theHour = 0;
  56.         if (theHour < 0 || theHour > 23)
  57.         {
  58.             cout << "Error: illegal input to readHour\n";
  59.             exit(1);
  60.         }
  61.     }
  62. }

  63. namespace DTimeSavitch
  64. {
  65.     istream &operator>>(istream &ins, DigitalTime &theObject)
  66.     {
  67.         readHour(theObject.hour);
  68.         readMinute(theObject.minute);
  69.         return ins;
  70.     }
  71.     ostream &operator<<(ostream &outs, const DigitalTime &theObject)
  72.     {
  73.         outs << theObject.hour << ':';
  74.         if (theObject.minute < 10)
  75.             outs << '0';
  76.         outs << theObject.minute;
  77.         return outs;
  78.     }
  79.     bool operator==(const DigitalTime &time1, const DigitalTime &time2)
  80.     {
  81.         return (time1.hour == time2.hour && time1.minute == time2.minute);
  82.     }
  83.     DigitalTime::DigitalTime(int theHour, int theMinute)
  84.     {
  85.         if (theHour < 0 || theHour > 24 || theMinute < 0 || theMinute > 59)
  86.         {
  87.             cout << "Illegal argument to DigitalTime constructor.";
  88.             exit(1);
  89.         }
  90.         else
  91.         {
  92.             hour = theHour;
  93.             minute = theMinute;
  94.         }
  95.         if (hour == 24)
  96.             hour = 0;
  97.     }
  98.     DigitalTime::DigitalTime()
  99.     {
  100.         hour = 0;
  101.         minute = 0;
  102.     }
  103.     int DigitalTime::getHour() const
  104.     {
  105.         return hour;
  106.     }
  107.     int DigitalTime::getMinute() const
  108.     {
  109.         return minute;
  110.     }
  111.     void DigitalTime::advance(int minutesAdded)
  112.     {
  113.         int grossMinutes = minute + minutesAdded;
  114.         minute = grossMinutes % 60;
  115.         int hourAdjustment = grossMinutes / 60;
  116.         hour = (hour + hourAdjustment) % 24;
  117.     }
  118.     void DigitalTime::advance(int hoursAdded, int minutesAdded)
  119.     {
  120.         hour = (hour + hoursAdded) % 24;
  121.         advance(minutesAdded);
  122.     }
  123. }
复制代码

main.cpp:
  1. #include <iostream>
  2. #include "Dtime.h"

  3. void readHour(int &theHour);

  4. int main()
  5. {
  6.     using std::cin;
  7.     using std::cout;
  8.     using std::endl;

  9.     using DTimeSavitch::DigitalTime;
  10.     int theHour;
  11.     readHour(theHour);
  12.     DigitalTime clock(theHour, 0), oldClock;

  13.     oldClock = clock;
  14.     clock.advance(15);
  15.     if (clock == oldClock)
  16.         cout << "Sometheing is wrong.";
  17.     cout << "You entered " << oldClock << endl;
  18.     cout << "15 minutes later the time will be " << clock << endl;
  19.     clock.advance(2, 15);
  20.     cout << "2 hours and 15 minutes after that\n"
  21.          << "the time will be " << clock << endl;
  22.     return 0;
  23. }

  24. void readHour(int &theHour)
  25. {
  26.     using std::cin;
  27.     using std::cout;
  28.     cout << "Let's play a time game.\n"
  29.          << "Let's pretend the hour has just changed.\n"
  30.          << "You may write midnight as either 0 or 24,\n"
  31.          << "but, I will always write it as 0.\n"
  32.          << "Enter the hour as a number(0 to 24)";
  33.     cin >> theHour;
  34. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-14 20:53:00 | 显示全部楼层    本楼为最佳答案   
一世轻尘 发表于 2021-5-14 20:37
这是三个文件的内容
Dtime.h:

cpp文件是通过命令行传递给编译器的,不是通过 #include 指令添加到另一个cpp文件中的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-14 21:01:58 | 显示全部楼层
g++ -g -Wall -o main main.cpp Dtime.cpp
没有报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-14 21:10:41 | 显示全部楼层
本帖最后由 一世轻尘 于 2021-5-14 21:13 编辑
人造人 发表于 2021-5-14 20:53
cpp文件是通过命令行传递给编译器的,不是通过 #include 指令添加到另一个cpp文件中的


懂了,蟹蟹,感觉还是太依赖IDE了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 10:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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