c++ - 无法在 C++ 中使用前向声明
问题描述
我正在学习C++,使用g++ (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0
我有以下代码:
- 主.cpp:
#include <iostream>
void log(std::string message);
int main() {
log("HI");
}
- 日志.cpp:
#include <iostream>
void log(std::string message)
{
std::cout << message << std::endl;
}
当我编译并运行时,我收到以下错误:
/tmp/ccYckYXk.o: In function `main':
main.cpp:(.text+0x43): undefined reference to `log(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: error: ld returned 1 exit status
请问我的代码有什么问题吗?
解决方案
推荐阅读
- sql - Oracle SQL - 创建选择语句,它将检索每个月的结束日期,但比较月末后的一两天的值
- firebase - Google Ads 中缺少一些通过 Firebase 报告的 Google Analytics 4 转化
- python - 需要以下格式的 Python 熊猫
- jestjs - 使用 Jest 测试 NestJS + TypeORM 时“'instanceof' 的右侧不是对象”
- docker - 如何通过 cli 检查 Docker Swarm 资源预留
- python - 为什么 ipywidgets 的小部件没有在 html 文件中定义?
- c++ - 使用 Playsound c++ 在后台运行音乐
- spring-boot - Spring boot oauth2 客户端凭据使用 webflux 动态配置
- python - 切片列表以提取给定索引的最后 k 和下 k 元素
- reactjs - 在 Docker 上运行 CRA 时出现错误(0,_schemaUtils.default)不是函数