C++未定义对的引用

C++ undefined reference to

本文关键字:引用 未定义 C++      更新时间:2023-10-16

我得到了很多未定义的引用。我不知道我做错了什么。

我得到以下错误:

undefined reference to 'LetteroidField::start()'
undefined reference to 'LetteroidField::setTitle(std::string)'
undefined reference to 'Letteroid::setletter(char)'
undefined reference to 'Letteroid::setLetter()'
undefined reference to 'Letteroid::setCoords()'
undefined reference to 'Letteroid::erase()'

以及其他letteroid参考文献。

我还没有完成其他课程,但我不知道为什么会出现这些错误。我是否没有正确使用#include"?

这是我教授的样例代码。我联系了他,但他没有回答(这是网课)。


#include "letteroidfield.h"
#include "letteroid.h"
#include "blinkingletteroid.h"
#include "jumpingletteroid.h"
#include "movingletteroid.h"
#include <stdlib.h>     /* srand, rand */
#include <time.h>
/// include your derived classes here

int main()
{

    LetteroidField screen;
    screen.start();
    screen.setTitle("Ken's example for the class, press 'x' to quit");

    BlinkingLetteroid one;
    BlinkingLetteroid two;
    BlinkingLetteroid three;
    one.setLetter('!'); /// character
    one.setCoords(5, 10); /// row, col
    two.setLetter('h');
    two.setCoords(7, 9);
    three.setLetter('@');
    three.setCoords(15, 57);
    JumpingLetteroid four;
    four.setLetter('j');
    four.setCoords(rand() % 21, rand() % 21);
    MovingLetteroid five;
    five.setLetter('m');
    int x = 20;
    int y = 20;
    while (x >= 1)
    {
        --x;
    }
    while (y >= 1)
    {
        --y;
    }
    if (x == 1)
    {
        x = 20;
    }
    if (y == 1)
    {
        x = 20;
    }
    five.setCoords(x,y);

    /// create and initialize your letteroids here
    while ( screen.waitForKeyPress() ) /// keep going until 'x' is pressed
    {
        one.blink();
        two.blink();
        three.blink();
        /// call the function that draws your letteroids here
    }

    screen.end();
    return 0;
}

#ifndef _LETTEROIDFIELD_H_
#define _LETTEROIDFIELD_H_
#include <string>
class LetteroidField
{
public:
    void start();            /// start up the screen for letteroids
    bool waitForKeyPress();  /// wait for any key to be pressed (return 
    void end();              /// shut down the screen and return it to     
    void setTitle(std::string);   /// diplay the title
};
#endif

#ifndef _LETTEROID_H_
#define _LETTEROID_H_
class Letteroid
{
 public:
  void setCoords(int, int);// set the position(down, across)
  void setLetter(char);    // set the character
  int getX();              // get the position down
  int getY();              // get the position across
  void erase();            // erase the letteroid from the screen
  void draw();             // draw the letteroid to the screen
 private:
  int myX;
  int myY;
  char myLetter;
};
#endif

您需要问自己的问题是:这些类在哪里定义?

如果答案是:"在标题旁边提供的共享库(文件扩展名".so")中",那么您需要通过在编译命令中至少添加以下内容来链接它:

g++ main.cpp -L</path/to/library> -l<library_name>

如果答案是:"在标题旁边提供的静态库(文件扩展名为".a",又名归档)中",那么您需要通过在编译命令中至少添加以下内容将其包含在二进制文件中:

 g++ main.cpp <library_name.a>

如果答案是:"在标题旁边提供的一堆源文件中",那么你需要通过在编译命令中至少添加以下内容来将它们包括在二进制文件中:

g++ main.cpp <source_file1.cpp> <source_file2.cpp> ...