【问题标题】:VS Code C++ OOP don't work Mac OS High SierraVS Code C++ OOP 不工作 Mac OS High Sierra
【发布时间】:2020-07-11 11:28:19
【问题描述】:

大家好,我是 vs 代码新手,我找不到使用面向对象编程的解决方案

当我创建一个 .h 文件来调用对象函数时出现错误

123MacBook-Pro-de-Rogerio: life DJMatrix $ cd "/ Users / DJMatrix / Documents / Classes / c ++ / life /" && g ++ main.cpp -o main && "/ Users / Dtrix / Documents / Classes / c ++ / life / "main
Undefined symbols for architecture x86_64:
  "Life :: tryAgain ()", referenced from:
      _main in main-ea3ce4.o
ld: symbol (s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

ma​​in.cpp:

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

using namespace std;

int main()
{
    Life life;
    life.tryAgain();
    return 0;
}

life.h:

#include <iostream>
using namespace std;

class Life
{
public:
    bool sucess;
  void tryAgain();
  void improve();
};

life.cpp:

#include "life.h"

void Life::tryAgain()
{
  cout << "Trying again!!!" << endl;
}

void Life::improve()
{
  cout << "Improve !!" << endl;
}

【问题讨论】:

  • ????????请在此处以纯文本形式发布代码、错误、示例数据或文本输出,而不是难以阅读、无法复制粘贴以帮助测试代码或在答案中使用的图像,并且对依赖的人构成障碍在屏幕阅读器上。您可以编辑问题以在问题正文中添加代码。为了便于格式化,请使用{} 按钮标记代码块,或者使用四个空格缩进以获得相同的效果。 屏幕截图的内容无法搜索、作为代码运行或复制和编辑以创建解决方案。

标签: c++ macos oop visual-studio-code clang++


【解决方案1】:

根据我从 VSCode 终端看到的情况,只有 main.cpp 正在编译。当您生成最终二进制文件时,life.cpp 的目标文件没有得到链接,这就是为什么它抱怨缺少 Life::tryAgain() 符号。

这取决于您是手动调用编译器还是使用 Makefiles 或让 VSCode 为您完成所有这些工作;不管编译命令应该是这样的:

g++ -o main life.cpp main.cpp

【讨论】:

  • 是否有自动化这个过程的组件或修改?
  • @Matrix 查看创建一个custom task 来编译/构建您的应用程序。您可以设置g++ args 来编译目录中的所有*.cpp 文件。
猜你喜欢
  • 2018-06-21
  • 2021-11-30
  • 2018-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 2018-05-19
相关资源
最近更新 更多