Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Błąd linkera "undefined reference to"

Ostatnio zmodyfikowano 2019-01-06 20:38
Autor Wiadomość
Grzesiek11
Temat założony przez niniejszego użytkownika
Błąd linkera "undefined reference to"
» 2019-01-04 20:14:25
Cześć. Jak na razie poradziłem sobie z błędami kompilacji w Moim Projekcie Którego Nie Dokończę, ale teraz wywala mi błąd linkera...

$ cd "/home/grzesiek11/Kodzenie/przygoda-engine/" && g++ main.cpp -o main && "/home/grzesiek11/Kodzenie/przygoda-engine/"main
/tmp/cc3tPMUe.o: In function `Game::Game()':
main.cpp:(.text+0x4e0): undefined reference to `Game::locations'
main.cpp:(.text+0x55c): undefined reference to `Game::locations'
/tmp/cc3tPMUe.o: In function `Game::findLocation(int)':
main.cpp:(.text+0x9a6): undefined reference to `Game::locations'
collect2: error: ld returned 1 exit status
Oto kod, jak na razie krótki.
C/C++
#include <iostream>
#include <string>
#include <vector>

enum DIRECTION { N, E, S, W };

class Game {
public:
    Game();
    void start();
private:
    struct Object {
        std::string name;
        std::string description;
    };
   
    struct Exit {
        DIRECTION direction;
        int locationID;
        std::string str();
    };
   
    struct Location {
        int ID;
        std::string name;
        std::string description;
        std::vector < Object > objects;
        std::vector < Exit > exits;
        void describe();
    };
   
    struct Player {
        std::string name;
        int locationID;
        void go( std::string directionStr );
    };
   
    void takeInput();
    static Location findLocation( int ID );
    static std::vector < Location > locations;
    Player player;
};

std::string Game::Exit::str() {
    switch( direction ) {
    case N:
        return "north";
    case E:
        return "east";
    case S:
        return "south";
    case W:
        return "west";
    }
}

void Game::Location::describe() {
    std::cout << "You are in " << name << ".\n" << description << "\n" << "You can see ";
    for( auto i: objects ) {
        std::cout << i.name << " ";
    }
    std::cout << ".\n" << "You can go ";
    for( auto i: exits ) {
        std::cout << i.str() << " ";
    }
    std::cout << ".\n";
}

Game::Game() {
    player.name = "player";
    Location location;
    location.ID = 0;
    location.name = "room";
    location.description = "It seems small and smells bad.";
    Object object;
    object.name = "computer";
    object.description = "It looks like it's using Debian GNU/Linux.";
    location.objects.push_back( object );
    Exit exit;
    exit.direction = W;
    exit.locationID = 1;
    location.exits.push_back( exit );
    locations.push_back( location );
    location.ID = 1;
    location.name = "libray";
    location.description = "You can see books everywhere. It's quite abbadoned.";
    exit.direction = E;
    exit.locationID = 0;
    location.exits.push_back( exit );
    locations.push_back( location );
    player.locationID = 0;
}

void Game::takeInput() {
    std::string input;
    std::cout << "> ";
    getline( std::cin, input );
    std::vector < std::string > data;
    std::string actualString;
    for( int i = 0; i <= input.size(); i++ ) {
        if( input[ i ] == ' ' || i == input.size() ) {
            data.push_back( actualString );
            actualString = "";
            continue;
        }
        actualString += input[ i ];
    }
    if( data[ 0 ] == "go" ) {
        player.go( data[ 1 ] );
    }
}

void Game::Player::go( std::string directionStr ) {
    DIRECTION direction;
    if( directionStr == "north" ) {
        direction = N;
    } else if( directionStr == "east" ) {
        direction = E;
    } else if( directionStr == "south" ) {
        direction = S;
    } else if( directionStr == "west" ) {
        direction = W;
    }
    for( auto i: findLocation( locationID ).exits ) {
        if( i.direction == direction ) {
            locationID = i.locationID;
            return;
        }
    }
    std::cout << "You can't go there!\n";
}

Game::Location Game::findLocation( int ID ) {
    for( auto i: locations ) {
        if( i.ID == ID ) {
            return i;
        }
    }
    throw "Location not found.";
}

void Game::start() {
    while( 1 ) {
        findLocation( player.locationID ).describe();
        takeInput();
    }
}

int main() {
    Game game;
    game.start();
}
P-173421
michal11
» 2019-01-04 20:37:19
Nigdzie nie tworzysz zmiennej Game::locations tylko deklarujesz.
P-173422
Grzesiek11
Temat założony przez niniejszego użytkownika
» 2019-01-04 21:22:33
A to co?
C/C++
static std::vector < Location > locations;
Hmm...
P-173423
killjoy
» 2019-01-04 21:49:31
Na pewno, nie definicja. Czyli to o co chodziło @michal11
P-173424
pekfos
» 2019-01-04 21:52:23
P-173425
Grzesiek11
Temat założony przez niniejszego użytkownika
» 2019-01-06 20:38:59
Zamykam.
P-173442
« 1 »
  Strona 1 z 1