首页 > 解决方案 > 无法在 C++ 中使用前向声明

问题描述

我正在学习C++,使用g++ (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0

我有以下代码:

#include <iostream>

void log(std::string message);

int main() {
    log("HI");
}
#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

请问我的代码有什么问题吗?

标签: c++g++

解决方案


推荐阅读