2014-02-16 39 views
1

我有5個文件,即:Dice.cpp,Dice.h,Pokemon.cpp,Pokemon.h和main.cpp。以下是我已經包括在每個文件:在C++項目中未定義的引用

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 

錯誤

當我嘗試使用geany構建應用程序,我得到了以下錯誤:

>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. 

端子輸出

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 

有人能指出我正確的方向,爲什麼我不能建立文件?我很確定我的makefile有問題,但我無法弄清楚。謝謝

+0

你*實現了*在這些標頭某處聲明的一切嗎?將它們包含在一個.cpp中是很好的,但是它們實際上是被聲明的成員實現的嗎? – WhozCraig

+0

我已編輯我的帖子。我實施了口袋妖怪::口袋妖怪以及所有其他成員。如果需要,我可以編輯和添加更多示例,但我只是不想做出大量的帖子。 – rubito

+0

你也可以發佈一個完整的'make'運行,也就是說, 'make clean'後輸出'make'?您的鏈接順序看起來是正確的(大多數取決於最不依賴的),因爲鏈接有時會很挑剔。謝謝。 – WhozCraig

回答

0

首先,嘗試make clean後跟make。如果一個頭文件被修改了,makefile就不會感覺到這個並且正確地重建類。

更可能的是,您尚未正確聲明class Pokemonclass Dice。也許顯示這些聲明。

Main.c引用錯誤消息中列出的符號。也許它們的命名方式不同(C++區分大小寫),或者類的作用域是隱藏的。

+0

這是「階級口袋妖怪」 「類{口袋妖怪 \t \t私人聲明: \t \t int healthPoints,hitPoints,attackL,defL; \t \t \t \t std :: string Pname; \t \t骰子d20; \t \t Dice d6; \t \t \t public: \t \t Pokemon(); \t \t布爾攻擊(寵物小精靈對手); \t \t void userBuild();「 – rubito