g ++在链接阶段无法编译以下文件,并出现多个undefined reference to [function]
错误。
我正在使用教科书学习C ++ 11。关于该主题的所有其他问题和答案都涉及makefile或模板,我在这里没有使用。
g ++版本为7.3.0(Ubuntu 7.3.0-27ubuntu1〜18.04)。
main.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;
}
Screen.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
Screen.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
答案 0 :(得分:1)
作为Mike Kinghan pointed out,这里已经回答:https://stackoverflow.com/a/54296817/1362568
基本上,问题是我在Screen.cpp文件而不是.h文件中实现了inline
函数。由于inline
是“内联”展开的,而不是“真实”函数,因此链接器找不到“真实”函数来定义我在Screen.h中声明的函数。
这是我的错误,是由于导师告诉我始终将定义放在单独的.cpp文件中,原因是我没有按照教科书的顺序去做。
我通过在.h文件中定义内联来解决此问题。