2015-12-11 2 views
-1

Я получаю много неопределенных ссылок. Я не знаю, что я делаю неправильно.C++ undefined ссылка на

Я получаю следующие ошибки:

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 

+0

Можете ли вы показать включает (или весь класс) в .cpp из ваш класс LETTEROID? Я думаю, проблема в том, что вы не включаете заголовки класса – dasjkdj

ответ

2

Вопрос, который вы должны спросить себя: где определены эти классы?

Если ответ: «в общей библиотеке (расширение файла» .so «) при условии рядом с заголовком», то вам нужно связать против него, добавляя по крайней мере, следующее к команде компиляции:

Если ответ: «в статической библиотеке (расширение файла« .a », архив AKA), предоставленное рядом с заголовком», то вам нужно будет включить его в свой двоичный файл, добавив по крайней мере следующее к вашему команда компиляции:

g++ main.cpp <library_name.a> 

Если ответ: «в кучке исходных файлов при условии, наряду с заголовком», то вам необходимо включить их в двоичном виде, добавляя по крайней мере, следующее к команде компиляции:

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