gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:26:05 27 4
gpt4 key购买 nike

<分区>

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com