C++ namespace, and class help

Started by
15 comments, last by InterFiction 12 years, 5 months ago
error: 'OgreSwordStikes' has not been declared|

Spelling error. You wrote "OgreSwordStikes" instead of "OgreSwordStrikes".

error: 'string' has not been declared|[/quote]
You have two ways to solve it:
- replace string with std::string in the header file.
- import std::string into your own namespace by adding "using std::string;" inside of your namespace in your header file.
Advertisement
well, I got it all down to 1 error. It's the same one. I don't know if I changed something after fixing the typo, or what. I added half the functionality I wanted in the function, so I'll paste the while code, so you can get a closer look.

header
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H
#include<string>
// this namespace is for holding classes and functions dealing with weapon strikes.

namespace OgreWeaponStrikes
{


using std::string;

// Following class deals with swrod strikes
class OgreSwordStrikes
{

//The following method deals with random sword strikes based on OgreSwordStrikeID. a value of 1 is for light damage, and 2 for critical.
//Enemy string passes in the name of the enemy, and DamageAmount, and EnemyHealth passes in the amount of damage done, as well as enemies health.
//When the code is executed it will subtract damage amount from enemy health, and display a message stating what has been
//for damage, a description of the attack, as well as to whom the damage was done to.
public:
int OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName);
};

}

#endif // OGREWEAPONSTRIKES_


cpp
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>
using std::string;

using namespace std;

//This is the body of code that will be executed when OgreRanSwordStrikes is called.

int OgreSwordStrikes::OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName)
{

switch (OgreSwordStrikesID)
{
//random attack display will determine which text will be displayed for the attack message.



srand(time(0));

case 1:
int RandomAttackDisplay;
RandomAttackDisplay = (rand()%20)+1;
switch (RandomAttackDisplay)
{
case 1:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "A light slash doing minimal damage of " << DamageAmount << " to " << EnemyName <<"." <<endl;
break;
case 2:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "A light slash does minimal damage to " << EnemyName << DamageAmount <<"." << endl;
case 3:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "A light slash cuts " << EnemyName << "for " << DamageAmount <<"." << endl;
case 4:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword grazes " << EnemyName << "doing " << DamageAmount << " damage." << endl;
case 5:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword pokes " << EnemyName << "doing " << DamageAmount << " damage. " << endl;
case 6:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword lightly pokes " << EnemyName << "doing " << DamageAmount << " damage." << endl;
case 7:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Blade grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 8:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword jabs " << EnemyName << " doing" << DamageAmount << " damage." << endl;
case 9:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword lightly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 10:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword barely grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 11:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Blade hardly grazes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 12:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Blade lightly jabs " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 13:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 14:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword hardly pokes " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 15:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Sword barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 16:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Blade barely cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 17:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Blade hardly cuts " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 18:
EnemyHealth = EnemyHealth - DamageAmount;
cout << "Blade lightly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
case 19:
cout << "Sword lightly slides across" << EnemyName << " doing " << DamageAmount << " damage." << endl;
EnemyHealth = EnemyHealth - DamageAmount;
case 20:
cout << "Sword hardly slides across " << EnemyName << " doing " << DamageAmount << " damage." << endl;
EnemyHealth = EnemyHealth - DamageAmount;
default:
cout << "There has been an error. check to make sure" << endl << "your variables are correct and are" << endl;
cout << "passed into the function in the proper order." << endl;
}

}

return 0;
}

I finally got this working :) Thanks to everyone here for helping me, I really appreciate it.
Just a note, you don't want to call srand() twice. Call it once at the beginning of your program, then don't touch it. srand() just seeds the random number generator. Once it's been seeded, it's good to go. Reseeding it is unnecessary, and can even give less-random results.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
I can only find one place in the code I called it. That was in the switch. I may just be having a blind moment, lol, can ya point it out?

I can only find one place in the code I called it. That was in the switch. I may just be having a blind moment, lol, can ya point it out?

Yes you call it once in the function, but every single time you call the function it will call srand() again. If you call the OgreRanSwordStrikes function only once, its fine. But if you ever plan on calling that function more than once, it's an issue.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
ohh okay, that was new knowledge, thank you. I'll take it out of the function all together.

This topic is closed to new replies.

Advertisement