• Advertisement
Sign in to follow this  

help on my code

This topic is 4245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

could someone help me on my code my first header is
class human{
int two
int three
int four
void attack(int &amount, enemy &name);
void magic(int &type, enemy &name);
};


my second header is
class enemy{
int two
int three
int four
void attack(int &amount, human &name);
void magic(int &type, human &name);
};


the functions are in there own source file the problem is when i try to run it it says class enemy not declared in the first and class human in the second and if i declare them by #include "human.h" and #include "orc.h" the compilation doesn't end any help?

Share this post


Link to post
Share on other sites
Advertisement

#ifndef _HUMAN_H_
#define _HUMAN_H_

class enemy; // Forward declaration of the enemy class

class human
{
int two;
int three;
int four;

void attack(int &amount, enemy &name);
void magic(int &type, enemy &name);
};

#endif



#ifndef _ENEMY_H_
#define _ENEMY_H_

class human; // Forward declaration of the human class

class enemy
{
int two;
int three;
int four;

void attack(int &amount, human &name);
void magic(int &type, human &name);
};

#endif


Then, in enemy.cpp include human.h, and in human.cpp include enemy.h

Share this post


Link to post
Share on other sites
Hi you could use #pragma once at the top of your header files to stop it from compiling more than once when you include your opposite file. Otherwise say your human.h file just includes enemy.h which includes human.h so your compiler will get stuck in a loop.


////////////////////////////////////////
// HUMAN.H
////////////////////////////////////////
#pragma once

#include "enemy.h"

class human
{
int two
int three
int four
void attack(int &amount, enemy &name);
void magic(int &type, enemy &name);
};




////////////////////////////////////////
// enemy.H
////////////////////////////////////////
#pragma once

#include "human.h"

class enemy
{
int two
int three
int four
void attack(int &amount, human &name);
void magic(int &type, human &name);
};

Share this post


Link to post
Share on other sites
you might want to join the C++ workshop that is starting up here on gamedev right away.

Share this post


Link to post
Share on other sites
i will join thanks oh and another thing is there a way i could call a function that needs parameters from a function in a class for instance:
#pragma once
#include "enemy.h"
class enemy;
class human{
int one;
int two;
void damage(human &name, enemy &mon){
cout<<name.one<<mon.two<<endl;
}
void attack(int amount, human &name, enemy &mon){
int amount=name.one;
mon.two-=amount;
damage(human &name, enemy &mon);
}
};


because when i do it it doesnt work

Share this post


Link to post
Share on other sites
with the function parameters being pointers (&name) you may find that using name->one and name->two will work better.

Oh and when calling a function you can simply say damage(&name,&mon) to pass the updated variables to the next routines calculations.

Apart from that what happens when you compile or run the program with the code like that ?

Share this post


Link to post
Share on other sites
Quote:
Original post by Xrystal
with the function parameters being pointers (&name) you may find that using name->one and name->two will work better.
The arguments are references, not pointers, so the . syntax is correct.

@The OP: It looks like the problem may be access privilages; try making the member variables of 'enemy' public and see if that works.

Once you get it working, the next step is probably to revise the design to reflect OOP principals such as encapsulation (assuming that's what you're going for).

Share this post


Link to post
Share on other sites

void attack(int amount, human &name, enemy &mon){
int amount=name.one;
mon.two-=amount;
damage(human &name, enemy &mon);
}


When you *call* a function, *do not* include the names of the types. Just provide arguments that are of appropriate types.

A function definition says what to do with a given set of things (and includes names that will be used for those things within the function, for the sake of writing that definition). A call says "do the action with *these* things" (which could be differently named, or even be the result of some expression rather than just a variable.

Examples:


class Thing {
void doSomething() const {
// something wonderful happens here
}
};

int foo(const Thing& x, int bar) {
x.doSomething();
return bar * 3;
}

int quux() {
Thing wibble;
return foo(wibble, // notice, not named "x", and we don't put the types
10 + 3); // notice an expression, no variable
// 'foo' takes in the Thing, makes it doSomething(), and then returns
// 13 * 3, i.e. 39. Then quux() returns the result of the foo() call, i.e. 39.
}

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by Xrystal
with the function parameters being pointers (&name) you may find that using name->one and name->two will work better.
The arguments are references, not pointers, so the . syntax is correct.

@The OP: It looks like the problem may be access privilages; try making the member variables of 'enemy' public and see if that works.

Once you get it working, the next step is probably to revise the design to reflect OOP principals such as encapsulation (assuming that's what you're going for).



Thanks for that comment. To be honest I get the two items mixed up. I almost always use &varname when calling a function that has *varname in its declaration. I think I misread the OPs code in that light and apologise for the misunderstanding.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement