2 qq 33031101 qq_33031101 于 2016.04.24 19:55 提问

新手求助,C++的头文件引用问题
c++

为什么#include "container.h"报错说打不开source file啊,我完全不知道怎么回事,啊啊啊啊啊,求帮助

#ifndef _CONTAINER

#define _CONTAINER
#include
using namespace std;
class container
{
protected:
int numOfHeal;
int numOfMW;
public:
container();
void set(int heal_n, int mw_n);
int nOfHeal();
int nOfMW();
void display();
bool useHeal();
bool useMW();
};
container::container()
{
set(0, 0);
};

void container::set(int heal_n, int mw_n)
{
    numOfHeal = heal_n;
    numOfMW = mw_n;
};


int container::nOfHeal()
{
    return numOfHeal;
};


int container::nOfMW()
{
    return numOfMW;
};


void container::display()
{
    cout << "Your bag contains: " << endl;
    cout << "Heal(HP+100): " << numOfHeal << endl;
    cout << "Magic Water (MP+80): " << numOfMW << endl;
};


bool container::useHeal()
{
    numOfHeal--;
        return 1;       
};

//use magic water
bool container::useMW()
{
    numOfMW--;
    return 1;
};

#endif

#ifndef _PLAYER
#define _PLAYER

#include

#include

#include "container.h"
#include
using namespace std;
enum job { sw, ar, mg };

class player
{
friend void showinfo(player &p1, player &p2);
friend class swordsman;

protected:
int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;

string name;    
job role;       
container bag;  

public:
virtual bool attack(player &p) = 0;
virtual bool specialatt(player &p) = 0;
virtual void isLevelUp() = 0;

void reFill();      
bool death();       
void isDead();      
bool useHeal();     
bool useMW();       
void transfer(player &p);   
void showRole();    

private:
bool playerdeath;

};
void player::reFill()
{
HP = HPmax;

MP = MPmax;
}

bool player::death()
{
return playerdeath;
};

void player::isDead()
{
if (HP <= 0)

cout << name << " is Dead." << endl;
system("pause");
playerdeath = 1;

}
};

bool player::useHeal()
{
if (bag.nOfHeal()>0)
{
HP = HP + 100;
if (HP>HPmax)

HP = HPmax;

cout << name << " used Heal, HP increased by 100." << endl;
bag.useHeal();

system("pause");
return 1;

}
else

{
cout << "Sorry, you don't have heal to use." << endl;
system("pause");
return 0;

}
};

bool player::useMW()
{
if (bag.nOfMW()>0)
{
MP = MP + 100;
if (MP>MPmax)
MP = MPmax;
cout << name << " used Magic Water, MP increased by 100." << endl;
bag.useMW();
system("pause");
return 1;

}
else
{
cout << "Sorry, you don't have magic water to use." << endl;
system("pause");
return 0;
}
}

// possess opponent's items after victory
void player::transfer(player &p)
{
cout << name << " got" << p.bag.nOfHeal() << " Heal, and " << p.bag.nOfMW() << " Magic Water." << endl;
system("pause");
p.bag.set(p.bag.nOfHeal(), p.bag.nOfMW());
};
void player::showRole()
{
switch (role)
{
case sw:
cout << "Swordsman";
break;
case ar:
cout << "Archer";
break;
case mg:
cout << "Mage";
break;
default:
break;
}
};

// display character's job
void showRole()
{
system("cls");
cout << "##############################################################" << endl;
cout << "# Player" << setw(10) << p1.name << " LV. " << setw(3) << p1.LV
<< " # Opponent" << setw(10) << p2.name << " LV. " << setw(3) << p2.LV << " #" << endl;
cout << "# HP " << setw(3) << (p1.HP <= 999 ? p1.HP : 999) << '/' << setw(3) << (p1.HPmax <= 999 ? p1.HPmax : 999)
<< " | MP " << setw(3) << (p1.MP <= 999 ? p1.MP : 999) << '/' << setw(3) << (p1.MPmax <= 999 ? p1.MPmax : 999)
<< " # HP " << setw(3) << (p2.HP <= 999 ? p2.HP : 999) << '/' << setw(3) << (p2.HPmax <= 999 ? p2.HPmax : 999)
<< " | MP " << setw(3) << (p2.MP <= 999 ? p2.MP : 999) << '/' << setw(3) << (p2.MPmax <= 999 ? p2.MPmax : 999) << " #" << endl;
cout << "# AP " << setw(3) << (p1.AP <= 999 ? p1.AP : 999)
<< " | DP " << setw(3) << (p1.DP <= 999 ? p1.DP : 999)
<< " | speed " << setw(3) << (p1.speed <= 999 ? p1.speed : 999)
<< " # AP " << setw(3) << (p2.AP <= 999 ? p2.AP : 999)
<< " | DP " << setw(3) << (p2.DP <= 999 ? p2.DP : 999)
<< " | speed " << setw(3) << (p2.speed <= 999 ? p2.speed : 999) << " #" << endl;
cout << "# EXP" << setw(7) << p1.EXP << " Job: " << setw(7);
p1.showRole();
cout << " # EXP" << setw(7) << p2.EXP << " Job: " << setw(7);
p2.showRole();
cout << " #" << endl;
cout << "--------------------------------------------------------------" << endl;
p1.bag.display();
cout << "##############################################################" << endl;
};

#endif

5个回答

caozhy
caozhy   Ds   Rxr 2016.04.25 23:34

找到container.h,放在和程序相同的目录,然后就可以编译。

oyljerry
oyljerry   Ds   Rxr 2016.04.24 21:08

这个头文件路径是否正确 你编译器能否直接打开

beifengche
beifengche   2016.04.25 14:08

编译器没找到头文件,你先看看那个头文件在哪个路径下。

NK_test
NK_test   Rxr 2016.04.25 23:26

代码有点乱,与头文件对应的cpp是否正确存在?

CSDNXIAOD
CSDNXIAOD   2016.04.24 20:02

C/C++的头文件引用问题
C++代码段 -- 头文件相互引用问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!