c++ - GPP 在链接阶段失败:“未定义对 [function] 的引用”(没有生成文件或模板)
问题描述
g++ 在链接阶段无法编译以下文件并出现多个undefined reference to [function]
错误。
我正在使用教科书学习 C++11。关于这个主题的所有其他问题和答案都涉及我在这里没有使用的 makefile 或模板。
g++ 版本为 7.3.0(Ubuntu 7.3.0-27ubuntu1~18.04)。
主.cpp:
#include "Screen.h"
// #include <string>
using std::string;
// #include <iostream>
using std::cout;
using std::endl;
int main()
{
Screen myScreen(5, 5, 'X');
myScreen.move(4,0).set('#').display(cout);
cout << endl;
myScreen.display(cout);
cout << endl;
return 0;
}
屏幕.h:
#ifndef SCREEN_H
#define SCREEN_H
#include <string>
#include <iostream>
class Screen
{
public:
// typedefs
using pos = std::string::size_type;
// constructors
Screen()
= default;
Screen(pos ht, pos wd):
height (ht),
width (wd),
contents(ht * wd, ' ')
{}
Screen(pos ht, pos wd, char c):
height (ht),
width (wd),
contents(ht * wd, c)
{}
// public member functions
char get () const;
inline char get (pos, pos) const;
Screen& move (pos, pos);
Screen& set (char);
Screen& set (pos, pos, char);
Screen& display(std::ostream&);
const Screen& display(std::ostream&) const;
private:
// class variables
pos cursor = 0,
height = 0,
width = 0;
std::string contents ;
// private member functions
void do_display(std::ostream&) const;
};
#endif
屏幕.cpp:
#include "Screen.h"
// #include <string>
using std::string;
// #include <iostream>
/*
#### # # #### # ### ###
# # # # # # # # # #
#### # # #### # # #
# # # # # # # # #
# ### #### ##### ### ###
# # ##### # # #### ##### #### ####
## ## # ## ## # # # # # #
# ## # #### # ## # #### #### #### ###
# # # # # # # # # # #
# # ##### # # #### ##### # # ####
*/
char Screen::get() const
{return contents[cursor];}
/*inline*/ char Screen::get (pos r, pos c) const
{
pos row = r * width;
return contents[row + c];
};
inline Screen& Screen::move(pos r, pos c)
{
pos row = r * width;
cursor = row + c;
return *this;
}
inline Screen& Screen::set(char c)
{
contents[cursor] = c;
return *this;
}
inline Screen& Screen::set(pos row, pos col, char ch)
{
contents[(row * width) + col] = ch;
return *this;
}
Screen& Screen::display(std::ostream& os)
{
do_display(os);
return *this;
}
const Screen& Screen::display(std::ostream& os) const
{
do_display(os);
return *this;
}
/*
#### #### ### # # ### ##### #####
# # # # # # # # # # #
#### #### # # # ##### # ####
# # # # # # # # # #
# # # ### # # # # #####
# # ##### # # #### ##### #### ####
## ## # ## ## # # # # # #
# ## # #### # ## # #### #### #### ###
# # # # # # # # # # #
# # ##### # # #### ##### # # ####
*/
inline void Screen::do_display(std::ostream& os) const
{os << contents;}
我遵循了书中的所有内容,所以我希望 g++ 编译文件时没有错误。但是我在控制台中遇到了这些错误:
$ g++ -o program main.cpp Screen.cpp -std=c++11
/tmp/ccBELGiY.o: In function `main':
main.cpp:(.text+0x45): undefined reference to `Screen::move(unsigned long, unsigned long)'
main.cpp:(.text+0x52): undefined reference to `Screen::set(char)'
collect2: error: ld returned 1 exit status
解决方案
正如Mike Kinghan 指出的那样,这已经在这里得到了回答:https ://stackoverflow.com/a/54296817/1362568
基本上,问题是我inline
在我的 Screen.cpp 文件而不是我的 .h 文件中实现了我的功能。由于inline
s 是“内联”扩展的并且不是“真实”函数,因此链接器找不到“真实”函数来定义我在 Screen.h 中声明的函数。
这是我的错误,因为我的导师告诉我总是把定义放在一个单独的 .cpp 文件中,所以没有按照我的教科书来做。
我通过在我的 .h 文件中定义我的内联来解决这个问题。
推荐阅读
- javascript - 使用 Hooks 时无限重新渲染
- git - 重命名时,Git 正在考虑将签入的文件视为未跟踪
- apache-nifi - Apache Nifi:PutParquet 创建了一个无效文件
- python-3.x - Flask-SQLALCHEMY:psycopg2 操作错误 - 服务器在一段时间后意外关闭连接
- python - 如何使用 django 的 orm 获取额外数据?
- nginx - 开发模式下的 create-react-app:设置 PUBLIC_URL 环境变量会导致错误
- salesforce-lightning - 如何代表其他人发送 salesforce 销售云快速发送消息
- python - Python:Plotly 3D 曲面图
- json - 通过 API 抓取网页 - Python - 请求库
- javascript - 如何运行用户输入 Ace 编辑器的代码