c++ - VS Code C++ OOP 不工作 Mac OS High Sierra
问题描述
大家好,我是 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)
主.cpp:
#include <iostream>
#include "life.h"
using namespace std;
int main()
{
Life life;
life.tryAgain();
return 0;
}
生活.h:
#include <iostream>
using namespace std;
class Life
{
public:
bool sucess;
void tryAgain();
void improve();
};
生活.cpp:
#include "life.h"
void Life::tryAgain()
{
cout << "Trying again!!!" << endl;
}
void Life::improve()
{
cout << "Improve !!" << endl;
}
解决方案
从我从 VSCode 终端看到的,只有main.cpp
正在编译。当您生成最终二进制文件时,目标文件life.cpp
没有被链接,这就是为什么它抱怨Life::tryAgain()
符号丢失的原因。
这取决于您是手动调用编译器还是使用 Makefiles 或让 VSCode 为您完成所有这些工作;不管编译命令应该是这样的:
g++ -o main life.cpp main.cpp
推荐阅读
- awk - 如何在 sed 或 awk 中搜索点和数字并在数字前加上前导零?
- groovy - 以特定格式在soapui中传递今天的日期
- node.js - 不能使用变量作为键,mongoose .find() 的值
- css - 如何让我的脚本在 next.js 中的组件上运行?
- c# - 使用 linq 使用多个条件搜索和更新 XML
- javascript - 完成多个 Promise 中的任何一个后继续执行 (JavaScript)
- javascript - 按下按钮时服务器端 CURL POST 请求
- reactjs - 使用
内部路由停止 Auth0 登录工作 - javascript - 如何避免在javascript中重复代码?
- python - 通过迭代将 ne 列添加到 db