c++ - 尝试编译时对 class::function 的 undefined reference

标签 c++

我有三个文件:Main.cpp、Security.h 和 Security.cpp。

  • 我已经在我的头文件中声明了我的类 Security(包括一个函数)。

  • 我已经在 Security.cpp 中定义了函数。

  • 我的头文件已包含在 Main.cpp 和 Security.cpp 中。

  • 在 Main.cpp 中,我正在创建一个对象,并尝试运行成员函数并不断出现编译错误。

main.cpp

#include<iostream>
#include "Security.h"
using namespace std;

int main()
{
  Security S1;
  S1.Driver();
}

安全.h

class Security
{private:

 public:
 void Driver();
};

安全.cpp

#include<iostream>
#include<fstream>
#include "Security.h"
using namespace std;

void Securtiy::Driver()
{
  cout << "Enter a number: ";
  int answer;
  cin >> answer;

  cout << answer;
}

最佳答案

你应该编译这两个文件,因为 Security::Driver 的定义在 Security.cpp 中。

最简单的方法是调用单个命令:

g++ Main.cpp Security.cpp

但是,如果您想单独编译文件,则必须使用 -c 标志将它们编译成中间(“对象”)格式:

g++ -c Main.cpp
g++ -c Security.cpp

这将为您提供两个目标文件。现在链接它们:

g++ Main.o Security.o

关于c++ - 尝试编译时对 class::function 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18937877/

相关文章:

c++ - 如何使用 boost 条件变量来等待线程完成处理?

c++ - 如何告诉 std::set 到 'refresh' 它的顺序?

c++ - 全局函数模板重载和 const 参数

c++ - EM_ASM 中的 js 代码发生了什么变化?

c++ - 如何在远程 C++ 的 Eclipse 中将执行显示输出到控制台?

c++ - 使用具有多态行为的重载函数

c++ - 快速浮点运算的编译指示

c++ - 对于使用 Makefile 的大型 C++ 项目,什么是好的目录结构?

c++ - dip::EuclideanSkeleton不起作用(未处理的异常)

C++分配数组语法问题