c ++项目中未定义的引用

时间:2014-02-16 22:08:08

标签: c++ makefile

我有5个文件,即:Dice.cpp,Dice.h,Pokemon.cpp,Pokemon.h和main.cpp。以下是我在每个文件中包含的内容:

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"

生成文件

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有问题,但我无法弄清楚是什么。感谢

1 个答案:

答案 0 :(得分:0)

首先,尝试make clean后跟make。如果修改了头文件,makefile将无法检测到并正确重建该类。

更有可能的是,您尚未正确宣布class Pokemonclass Dice。也许会显示这些声明。

Main.c引用错误消息中列出的符号。也许它们的命名方式不同(C ++区分大小写),或者类的范围是隐藏的。