鱼C论坛

 找回密码
 立即注册
查看: 1445|回复: 8

[已解决]命名空间

[复制链接]
发表于 2018-12-26 09:16:59 | 显示全部楼层 |阅读模式
50鱼币
以下为小甲鱼课件中的内容,分别在main.cpp   rational.cpp  和 rational.h文件中
分别为主调用函数,类的具体实现和类的声明

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


int main()
{
    myMath::Rational f1(2, 16);
    myMath::Rational f2(7, 8);

    // 测试有理数加法运算
    std::cout << f1 << " + " << f2 << " == " << (f1+f2) << "\n";

    // 测试有理数减法运算
    std::cout << f1 << " - " << f2 << " == " << (f1-f2) << "\n";

    // 测试有理数乘法运算
    std::cout << f1 << " * " << f2 << " == " << (f1*f2) << "\n";

    // 测试有理数除法运算
    std::cout << f1 << " / " << f2 << " == " << (f1/f2) << "\n";

    return 0;
}















#include "rational.h"
#include <iostream>
#include <stdlib.h>

namespace myMath
{

Rational::Rational(int num, int denom)
{
    numerator = num;
    denominator = denom;

    normalize();
}

// normalize() 对分数进行简化操作包括:
// 1. 只允许分子为负数,如果分母为负数则把负数挪到分子部分,如 1/-2 == -1/2
// 2. 利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 => 1/5
void Rational::normalize()
{
    // 确保分母为正
    if( denominator < 0 )
    {
        numerator = -numerator;
        denominator = -denominator;
    }

    // 欧几里德算法
    int a = abs(numerator);
    int b = abs(denominator);

    // 求出最大公约数
    while( b > 0 )
    {
        int t = a % b;
        a = b;
        b = t;
    }

    // 分子、分母分别除以最大公约数得到最简化分数
    numerator /= a;
    denominator /= a;
}

// a   c   a*d   c*b   a*d + c*b
// - + - = --- + --- = ---------
// b   d   b*d   b*d =    b*d
Rational Rational::operator+(Rational rhs)
{
    int a = numerator;
    int b = denominator;
    int c = rhs.numerator;
    int d = rhs.denominator;

    int e = a*b + c*d;
    int f = b*d;

    return Rational(e, f);
}

// a   c   a   -c
// - - - = - + --
// b   d   b   d
Rational Rational::operator-(Rational rhs)
{
    rhs.numerator = -rhs.numerator;

    return operator+(rhs);
}

// a   c   a*c
// - * - = ---
// b   d   b*d
Rational Rational::operator*(Rational rhs)
{
    int a = numerator;
    int b = denominator;
    int c = rhs.numerator;
    int d = rhs.denominator;

    int e = a*c;
    int f = b*d;

    return Rational(e, f);
}

// a   c   a   d
// - / - = - * -
// b   d   b   c
Rational Rational::operator/(Rational rhs)
{
    int t = rhs.numerator;
    rhs.numerator = rhs.denominator;
    rhs.denominator = t;

    return operator*(rhs);
}

std::ostream& operator<<(std::ostream& os, Rational f)
{
    os << f.numerator << "/" << f.denominator;
    return os;
}

}




// Rational.h
// Create by 小甲鱼

// 这个头文件声明了有理数类(Rational class)
// 类里边对四则运算进行重载,以实现分数运算
#ifndef RATIONAL_H

#define RATIONAL_H

#include <iostream>

namespace myMath
{

class Rational
{
public:
    Rational(int num, int denom);  // num = 分子, denom = 分母

    Rational operator+(Rational rhs); // rhs == right hand side
    Rational operator-(Rational rhs);
    Rational operator*(Rational rhs);
    Rational operator/(Rational rhs);

private:
    void normalize(); // 负责对分数的简化处理

    int numerator;    // 分子
    int denominator;  // 分母

    friend std::ostream& operator<<(std::ostream& os, Rational f);
};

}

#endif





我的问题是:
在主调文件main.cpp文件和在rational.cpp(类的实现文件) 中  都进行了#include"rational.h"操作,
主调函数main.cpp是通过何种方式和rational.cpp(类的实现)联系起来的?  毕竟只是#include"rational.h" 而没有#include"rational.cpp",
也即只有声明没有定义?   


好吧 上一回忘记发悬赏了,有人看没人答。。。。 这次发悬赏。。。。





最佳答案
2018-12-26 09:17:00
竟无语凝噎 发表于 2018-12-27 10:45
能在说的简单一些么。。。  小白有些不是很懂 , 在linux下,这里您是既编译了main 又编译了。
但我在wi ...

继续往后学吧,不用纠结这些细节,学到后面你自然就会明白

最佳答案

查看完整内容

继续往后学吧,不用纠结这些细节,学到后面你自然就会明白
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-26 09:17:00 | 显示全部楼层    本楼为最佳答案   
竟无语凝噎 发表于 2018-12-27 10:45
能在说的简单一些么。。。  小白有些不是很懂 , 在linux下,这里您是既编译了main 又编译了。
但我在wi ...

继续往后学吧,不用纠结这些细节,学到后面你自然就会明白
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-26 12:29:13 | 显示全部楼层
ide帮你做了好多事
1.编译main.cpp
2.编译rational.cpp
3.合并第1步和第2步的结果

  1. sh-4.4$ g++ -c -o main.o main.cpp
  2. sh-4.4$ g++ -c -o rational.o rational.cpp
  3. sh-4.4$ g++ -o main main.o rational.o
  4. sh-4.4$ main.exe
  5. sh: main.exe: command not found
  6. sh-4.4$ ./main.exe
  7. 1/8 + 7/8 == 1/1
  8. 1/8 - 7/8 == -3/4
  9. 1/8 * 7/8 == 7/64
  10. 1/8 / 7/8 == 1/7
  11. sh-4.4$
  12. sh-4.4$
  13. sh-4.4$ rm main.exe main.o rational.o
  14. sh-4.4$ g++ -o main main.cpp rational.cpp
  15. sh-4.4$ ./main.exe
  16. 1/8 + 7/8 == 1/1
  17. 1/8 - 7/8 == -3/4
  18. 1/8 * 7/8 == 7/64
  19. 1/8 / 7/8 == 1/7
  20. sh-4.4$
复制代码

  1. g++ -o main main.cpp rational.cpp
复制代码

你当然可以用这一条命令,编译器在内部帮你做上面的步骤
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-26 12:35:05 | 显示全部楼层
  1. sh-4.4$ g++ -E main.cpp
  2. # 1 "main.cpp"
  3. # 1 "<built-in>"
  4. # 1 "<command-line>"
  5. # 1 "main.cpp"
  6. # 1 "rational.h" 1
  7. # 10 "rational.h"
  8. # 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 1 3
  9. # 36 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
  10.       
  11. # 37 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3

  12. # 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 1 3
  13. # 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3

  14. # 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
  15. namespace std
  16. {
  17.   typedef long unsigned int size_t;
  18.   typedef long int ptrdiff_t;


  19.   typedef decltype(nullptr) nullptr_t;

  20. }
  21. # 533 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
  22. # 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/os_defines.h" 1 3
  23. # 534 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 2 3

  24. .... // 这里省略很多不重要的内容(对当前问题来说不重要)

  25. # 41 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 2 3

  26. namespace std
  27. {

  28. # 60 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
  29.   extern istream cin;
  30.   extern ostream cout;
  31.   extern ostream cerr;
  32.   extern ostream clog;


  33.   extern wistream wcin;
  34.   extern wostream wcout;
  35.   extern wostream wcerr;
  36.   extern wostream wclog;




  37.   static ios_base::Init __ioinit;


  38. }
  39. # 11 "rational.h" 2


  40. # 12 "rational.h"
  41. namespace myMath
  42. {

  43. class Rational
  44. {
  45. public:
  46.     Rational(int num, int denom);

  47.     Rational operator+(Rational rhs);
  48.     Rational operator-(Rational rhs);
  49.     Rational operator*(Rational rhs);
  50.     Rational operator/(Rational rhs);

  51. private:
  52.     void normalize();

  53.     int numerator;
  54.     int denominator;

  55.     friend std::ostream& operator<<(std::ostream& os, Rational f);
  56. };

  57. }
  58. # 2 "main.cpp" 2



  59. int main()
  60. {
  61.     myMath::Rational f1(2, 16);
  62.     myMath::Rational f2(7, 8);


  63.     std::cout << f1 << " + " << f2 << " == " << (f1+f2) << "\n";


  64.     std::cout << f1 << " - " << f2 << " == " << (f1-f2) << "\n";


  65.     std::cout << f1 << " * " << f2 << " == " << (f1*f2) << "\n";


  66.     std::cout << f1 << " / " << f2 << " == " << (f1/f2) << "\n";

  67.     return 0;
  68. }
复制代码


这是预处理后的结果,可以看到.h的内容已经include了,之后把这个送给编译器编译,编译器既看到了.h的内容又看到了.c的内容
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-26 12:38:02 | 显示全部楼层
  1. g++ -E rational.cpp
复制代码
  让编译器只执行预处理,不进行后面的编译和链接,直接输出预处理后的结果
  1. # 1 "rational.cpp"
  2. # 1 "<built-in>"
  3. # 1 "<command-line>"
  4. # 1 "rational.cpp"
  5. # 1 "rational.h" 1
  6. # 10 "rational.h"
  7. # 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 1 3
  8. # 36 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3
  9.       
  10. # 37 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/iostream" 3

  11. # 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 1 3
  12. # 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3

  13. # 229 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/x86_64-pc-cygwin/bits/c++config.h" 3
  14. namespace std
  15. {
  16.   typedef long unsigned int size_t;
  17.   typedef long int ptrdiff_t;

  18. .......



  19.   extern wostream wcerr;
  20.   extern wostream wclog;




  21.   static ios_base::Init __ioinit;


  22. }
  23. # 11 "rational.h" 2


  24. # 12 "rational.h"
  25. namespace myMath
  26. {

  27. class Rational
  28. {
  29. public:
  30.     Rational(int num, int denom);

  31.     Rational operator+(Rational rhs);
  32.     Rational operator-(Rational rhs);
  33.     Rational operator*(Rational rhs);
  34.     Rational operator/(Rational rhs);

  35. private:
  36.     void normalize();

  37.     int numerator;
  38.     int denominator;

  39.     friend std::ostream& operator<<(std::ostream& os, Rational f);
  40. };

  41. }
  42. # 2 "rational.cpp" 2

  43. # 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 1 3
  44. # 36 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 3
  45. # 1 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib" 1 3
  46. # 39 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib" 3
  47.       
  48. # 40 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/cstdlib" 3
  49. # 37 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 2 3


  50. # 38 "/usr/lib/gcc/x86_64-pc-cygwin/7.3.0/include/c++/stdlib.h" 3
  51. using std::abort;
  52. using std::atexit;
  53. using std::exit;


  54.   using std::at_quick_exit;


  55.   using std::quick_exit;




  56. using std::div_t;
  57. using std::ldiv_t;

  58. using std::abs;
  59. using std::atof;
  60. using std::atoi;
  61. using std::atol;
  62. using std::bsearch;
  63. using std::calloc;
  64. using std::div;
  65. using std::free;
  66. using std::getenv;
  67. using std::labs;
  68. using std::ldiv;
  69. using std::malloc;

  70. using std::mblen;
  71. using std::mbstowcs;
  72. using std::mbtowc;

  73. using std::qsort;
  74. using std::rand;
  75. using std::realloc;
  76. using std::srand;
  77. using std::strtod;
  78. using std::strtol;
  79. using std::strtoul;
  80. using std::system;

  81. using std::wcstombs;
  82. using std::wctomb;
  83. # 4 "rational.cpp" 2


  84. # 5 "rational.cpp"
  85. namespace myMath
  86. {

  87. Rational::Rational(int num, int denom)
  88. {
  89.     numerator = num;
  90.     denominator = denom;

  91.     normalize();
  92. }




  93. void Rational::normalize()
  94. {

  95.     if( denominator < 0 )
  96.     {
  97.         numerator = -numerator;
  98.         denominator = -denominator;
  99.     }


  100.     int a = abs(numerator);
  101.     int b = abs(denominator);


  102.     while( b > 0 )
  103.     {
  104.         int t = a % b;
  105.         a = b;
  106.         b = t;
  107.     }


  108.     numerator /= a;
  109.     denominator /= a;
  110. }




  111. Rational Rational::operator+(Rational rhs)
  112. {
  113.     int a = numerator;
  114.     int b = denominator;
  115.     int c = rhs.numerator;
  116.     int d = rhs.denominator;

  117.     int e = a*b + c*d;
  118.     int f = b*d;

  119.     return Rational(e, f);
  120. }




  121. Rational Rational::operator-(Rational rhs)
  122. {
  123.     rhs.numerator = -rhs.numerator;

  124.     return operator+(rhs);
  125. }




  126. Rational Rational::operator*(Rational rhs)
  127. {
  128.     int a = numerator;
  129.     int b = denominator;
  130.     int c = rhs.numerator;
  131.     int d = rhs.denominator;

  132.     int e = a*c;
  133.     int f = b*d;

  134.     return Rational(e, f);
  135. }




  136. Rational Rational::operator/(Rational rhs)
  137. {
  138.     int t = rhs.numerator;
  139.     rhs.numerator = rhs.denominator;
  140.     rhs.denominator = t;

  141.     return operator*(rhs);
  142. }

  143. std::ostream& operator<<(std::ostream& os, Rational f)
  144. {
  145.     os << f.numerator << "/" << f.denominator;
  146.     return os;
  147. }

  148. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-12-27 10:45:39 | 显示全部楼层
人造人 发表于 2018-12-26 12:29
ide帮你做了好多事
1.编译main.cpp
2.编译rational.cpp

能在说的简单一些么。。。  小白有些不是很懂 , 在linux下,这里您是既编译了main 又编译了。
但我在windows下直接编译运行main就可以直接跑起来,没有显示的编译运行rational.cpp
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-27 19:22:24 | 显示全部楼层
表示不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-28 08:21:50 From FishC Mobile | 显示全部楼层
千锦襄 发表于 2018-12-27 19:22
表示不懂

这样,如果楼主有兴趣可以了解一下编译原理。就本贴来说,rational.cpp肯定是要编译的。main.cpp编译也产生一个目标模块,最后由链接程序将rational.obj链接到main中,生成exe文件。就先简单这样理解吧,编译原理还是挺难得一门课。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-12-28 10:40:49 | 显示全部楼层
小酒酒呢 发表于 2018-12-28 08:21
这样,如果楼主有兴趣可以了解一下编译原理。就本贴来说,rational.cpp肯定是要编译的。main.cpp编译也产 ...

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-14 15:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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