rubito
rubito

Reputation: 327

Undefined references in c++ project

I have 5 files, namely : Dice.cpp, Dice.h, Pokemon.cpp, Pokemon.h, and main.cpp. The following is what I've included in each file:

Dice.cpp

#include "Dice.h"

Dice.h

#ifndef DICE_H
#define DICE_H
#include <cstdio> /** NULL */
#include <cstdlib> /** srand(), rand() */
#include <ctime> /** time() */

Pokemon.cpp

#include "Pokemon.h"
#include <string>
#include "Dice.h"

Pokemon::Pokemon() {
healthPoints = attackL = defL =0;
std::string Pname= "";
d20=Dice(20);
d6=Dice(6);
    }

Pokemon.h

#include <iostream>
#include <string>
#include "Dice.h"

main.cpp

#include <iostream>
#include <string>
#include "Pokemon.h"
#include "Dice.h"

makefile

all: Lab02

Lab02: main.o Pokemon.o Dice.o
    g++ main.o Pokemon.o Dice.o -o Lab02

main.o: main.cpp
    g++ -c main.cpp

Pokemon.o: Pokemon.cpp
    g++ -c Pokemon.cpp

Dice.o: Dice.cpp
    g++ -c Dice.cpp

clean:
    rm  *o Lab02 

Errors

When I try to build the application using geany I get the following errors:

>g++ -Wall -o "main" "main.cpp"
>/tmp/ccd3vy1j.o: In function `main':
>main.cpp:(.text+0x19): undefined reference to `Pokemon::Pokemon()'
>main.cpp:(.text+0x28): undefined reference to `Pokemon::Pokemon()'
>main.cpp:(.text+0x3c): undefined reference to `Dice::Dice(int)'
>main.cpp:(.text+0x83): undefined reference to `Pokemon::userBuild()'
>main.cpp:(.text+0xca): undefined reference to `Pokemon::userBuild()'
>main.cpp:(.text+0xd9): undefined reference to `Dice::roll()'
>main.cpp:(.text+0x1b0): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x1c9): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x259): undefined reference to `Pokemon::attack(Pokemon)'
>main.cpp:(.text+0x290): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x31b): undefined reference to `Pokemon::attack(Pokemon)'
>main.cpp:(.text+0x34f): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x452): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x46b): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x4f5): undefined reference to `Pokemon::attack(Pokemon)'
>main.cpp:(.text+0x529): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x5b4): undefined reference to `Pokemon::attack(Pokemon)'
>main.cpp:(.text+0x5e8): undefined reference to `Pokemon::getname()'
>main.cpp:(.text+0x655): undefined reference to `Pokemon::getHP()'
>main.cpp:(.text+0x668): undefined reference to `Pokemon::getHP()'
>collect2: error: ld returned 1 exit status
>Compilation failed.

Terminal Output

Lab02
g++ -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:35:5: error: expected ‘}’ before ‘else’
     else if (attackL1 <= 0) {
     ^
main.cpp:37:11: error: expected primary-expression before ‘>’ token
      cin>>>attackL1;
           ^
main.cpp: At global scope:
main.cpp:43:2: error: ‘cout’ does not name a type
  cout<<"Enter your defense level (1-30): ";
  ^
main.cpp:44:3: error: ‘cin’ does not name a type
   cin>>defPoints1;
   ^
main.cpp:46:4: error: expected unqualified-id before ‘if’
    if (defPoints1 > 49) {
    ^
make: *** [main.o] Error 1

Can someone point me in the right direction as to why I can't build the file? I'm pretty sure there's something wrong with my makefile but I can't figure out what. Thanks

Upvotes: 1

Views: 1310

Answers (1)

wallyk
wallyk

Reputation: 57804

First, try make clean followed by make. If a header file were revised, the makefile would not sense that and rebuild the class properly.

More likely, you haven't declared class Pokemon and class Dice properly. Maybe show the declarations for these.

Main.c refers the symbols listed in the error message. Maybe they are named differently (C++ is case sensitive), or the class's scope is hidden.

Upvotes: 0

Related Questions