c++ - 奇怪的 "Undefined symbols"错误?

标签 c++

我正在学习 C++,但遇到了这个错误:

Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

我的代码是这样的

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

  class Fraction {
      private:
          int num, den;
      public:
          void set(int n, int d) {num=n; den=d; normalize();}
          int get_num(){return num;}
          int get_den(){return den;}
      private:
          void normalize();

          int gcf(int a, int b);
          int lcm(int a, int b);
  };

  void Fraction::normalize() {
      if (den == 0 || num == 0) {
          num = 0;
          den = 1;
      }

      if (den < 0) {
          num *= -1;
          den *+ -1;
      }

      int n = gcf(num, den);
      num = num / n;
      den = den / n;
  }

  int Fraction::gcf(int a, int b) {
      if (a % b == 0)
          return abs(b);
      else return gcf(b, a % b);
  }
  int Fraction::lcm(int a, int b) {
      return(a / gcf(a, b)) * b;
  }

如果有帮助的话,我正在使用 GCC 和命令 g++ -o

非常感谢任何帮助!

最佳答案

main 函数在哪里? C++ 中的每个“普通”程序都从 main 函数开始,这就是链接器寻找一个的原因。你没有提供。因此错误。

关于c++ - 奇怪的 "Undefined symbols"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7197982/

相关文章:

c++ - 如何使用 LowLevelKeyboardProc 在 C++ 中重新映射键盘键?

c++ - 调试 Visual Studio 链接器

c++ - 给定这两个值的函数会产生第三个值?

c++ - Arduino C++ - 尝试实例化具有非类型模板参数的类时出错

c++ - add_lvalue_reference/add_rvalue_reference 和 cv 限定类型

c++ - 体验在 Raspberry Pi 上为 PCF8575 I/O 扩展器编写 C 代码

c++ - 如何在 vector 中找到 vector

c++ - 在 C++ 中键入安全数字常量

c++ - 类方法的返回类型推导? C++1y

c++ - 当 bWaitAll 为 TRUE 时,WaitForMultipleObjects 返回值