C++ 未定义引用

C++ undefined reference to

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

我收到以下错误:

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 参考。

我还没有完成其他 classes,但我不知道为什么会收到这些错误。我没有正确使用#include "" 吗?

这是我教授的示例代码。我联系了他,但他没有接听(在线 class)。


#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

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

如果答案是:"in a shared library (file extension ".so") 与 header" 一起提供,那么您需要 link 通过至少添加以下内容来反对它编译命令:

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

如果答案是:"in a static library (file extension ".a", AKA archive) 与 header" 一起提供,那么您需要将其包含在您的二进制文件中,方法是至少将以下内容添加到您的编译命令:

 g++ main.cpp <library_name.a>

如果答案是:"in a bunch of source files provided alongside the header",那么您需要将它们包含在您的二进制文件中,方法是至少将以下内容添加到您的编译命令中:

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