我有5个文件,分别是:Dice.cpp,Dice.h,Pokemon.cpp,Pokemon.h和main.cpp。以下是我在每个文件中包含的内容:
Dice.cpp
#include "Dice.h"
骰子
#ifndef DICE_H
#define DICE_H
#include <cstdio> /** NULL */
#include <cstdlib> /** srand(), rand() */
#include <ctime> /** time() */
宠物小精灵
#include "Pokemon.h"
#include <string>
#include "Dice.h"
Pokemon::Pokemon() {
healthPoints = attackL = defL =0;
std::string Pname= "";
d20=Dice(20);
d6=Dice(6);
}
宠物小精灵
#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出了点问题,但是我不知道是什么原因。谢谢
最佳答案
首先,尝试make clean
,然后尝试make
。如果头文件被修改,makefile将无法检测到该文件并正确地重建该类。
您很有可能没有正确声明class Pokemon
和class Dice
。也许显示这些声明。
Main.c引用错误消息中列出的符号。也许它们的名称不同(C ++区分大小写),或者类的作用域是隐藏的。