c++ - GPP 在链接阶段失败 : "undefined reference to [function]" (no makefiles or templates)

标签 c++ compiler-errors g++

<分区>

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 pointed out ,这已经在这里得到回答:https://stackoverflow.com/a/54296817/1362568

基本上,问题是我在我的 Screen.cpp 文件而不是我的 .h 文件中实现了我的 inline 函数。由于 inline 是“内联”扩展的,不是“真正的”函数,因此链接器无法找到“真正的”函数来定义我在 Screen.h 中声明的函数。

这是我的错误,因为我的导师告诉我要始终将定义放在单独的 .cpp 文件中,所以我没有严格按照教科书来操作。

我通过在我的 .h 文件中定义内联来解决这个问题。

关于c++ - GPP 在链接阶段失败 : "undefined reference to [function]" (no makefiles or templates),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54280001/

相关文章:

c++ - -D 选项从 g++ 命令行被错误地扩展

c++ - 在C程序中调用C++代码时,`extern“C”`会做什么?

C 不会编译包含 sdl.h

c++ - 为什么在 boost 程序 (boost::filesystem) 中输出 "Bus error: 10"?

Java:不编译就检查错误

mysql - 安装 RMysql RHEL 7.3 出错

c++ - g++ 字符串 remove_if 错误

c++ - 如何将用 linux 编写的 std::wstring 读入 windows

c++ - 初始化一个未知大小的数组

c++ - *& 在 void headInsert(DoubleLinkList*& head, int theData) 中是什么意思。它是否等同于 void headInsert(DoubleLinkList head, int theData)