C++ namespace, and class help

Started by
15 comments, last by InterFiction 12 years, 5 months ago
Hello,

glad to join the forums. I'm apart of cplusplus.com but I haven't been getting much help there with this issue, so I was hoping maybe things would be different here. I'm a bit new to c++, but I'm getting alright with the basics. I've decided to take on a rather large project of creating a library for console rpg creation. I'm having a bit of trouble with my code though. Being new to c++ I'm having a bit of trouble in creating namespaces with classes in them properly.

Header file sample

namespace OgreWeaponStrikes
{
// Following class deals with swrod strikes
class OgreSwordStrikes()
{

//The following method deals with random sword strikes based on OgreSwordStrikeID 1 for light damage, 2 for critical.
//Enemy string passes in the name of the enemy, and DamageAmount passes in the amount of damage done.
public:
OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, string EnemyString);
};


CPP file sample

using namespace std;

OgreWeaponStrikes::OgreSwordStrikes::OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, string EnemyString)
{

switch(OgreSwordStrikesID)
{
// The first ID deals with 1 imput for light sword strikes.
case 1:
srand(time(0);
int RandomIndexID = (rand()%20)+1;


errors in header file

expected unqualified-id before ')' token|
forward declaration of 'struct OgreWeaponStrikes::OgreSwordStrikes'|[/quote]

errors in CPP file

error: ISO C++ forbids declaration of 'OgreRanSwordStrikes' with no type|
error: invalid use of incomplete type 'struct OgreWeaponStrikes::OgreSwordStrikes'|[/quote]

could someone point out what's causing them, and give a brief description?

thanks,
JB.
Advertisement
Header:
class OgreSwordStrikes // NO () !

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

the namespace the constructor and the switch inside have no closing }. Might just be an error in copying or the formatting is messed up when posting.

Hello,

glad to join the forums. I'm apart of cplusplus.com but I haven't been getting much help there with this issue, so I was hoping maybe things would be different here. I'm a bit new to c++, but I'm getting alright with the basics. I've decided to take on a rather large project of creating a library for console rpg creation. I'm having a bit of trouble with my code though. Being new to c++ I'm having a bit of trouble in creating namespaces with classes in them properly.

Header file sample

namespace OgreWeaponStrikes
{
// Following class deals with swrod strikes
class OgreSwordStrikes()
{

//The following method deals with random sword strikes based on OgreSwordStrikeID 1 for light damage, 2 for critical.
//Enemy string passes in the name of the enemy, and DamageAmount passes in the amount of damage done.
public:
OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, string EnemyString);
};


CPP file sample

using namespace std;

OgreWeaponStrikes::OgreSwordStrikes::OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, string EnemyString)
{

switch(OgreSwordStrikesID)
{
// The first ID deals with 1 imput for light sword strikes.
case 1:
srand(time(0);
int RandomIndexID = (rand()%20)+1;


OgreRanSwordStrikes should have a return type also...
Change:
class OgreSwordStrikes()
to:
class OgreSwordStrikes

and:
OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, string EnemyString);
to:
void OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, string EnemyString);


Note that I just chose void arbitrarily. You should think about what the function should return and return that, if not void.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks guys, getting much better help here, and I appreciate it :) Before I was told I picked a bad hobby, and should choose an easier language :angry: way to encourage a newb eh, haha.

Header.

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

namespace OgreWeaponStrikes
{
// Following class deals with swrod strikes
class OgreSwordStrikes()
{

//The following method deals with random sword strikes based on OgreSwordStrikeID 1 for light damage, 2 for critical.
//Enemy string passes in the name of the enemy, and DamageAmount passes in the amount of damage done.
public:
int OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName);
};
}


cpp

#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>


using namespace std;

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


I'm down to two errors now.

class OgreSwordStrikes()


error

expected unqualified-id before ')' token|[/quote]

and

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

error: expected unqualified-id before 'int'|[/quote]
I just gave you the fix for the first one.

Second:
Change:

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


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

ops, sorry about that. Just a quick question how come

int OgreWeaponStrikes::OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName)

doesn't have to include the class?

I'm still getting two errors though, sorry if I missed it :)

header

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

namespace OgreWeaponStrikes
{
// Following class deals with swrod strikes
class OgreSwordStrikes
{

//The following method deals with random sword strikes based on OgreSwordStrikeID 1 for light damage, 2 for critical.
//Enemy string passes in the name of the enemy, and DamageAmount passes in the amount of damage done.
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 namespace std;

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

return 0;
}


errors

error: 'string' has not been declared|
error: 'int OgreWeaponStrikes::OgreRanSwordStrikes(int, int, int, std::string)' should have been declared inside 'OgreWeaponStrikes'|[/quote]
My mistake.
It should have been:
int OgreSwordStrikes::OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName)


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

no problem, I appreciate the help :)

I replaced the line..

int OgreSwordStikes::OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName)

but I'm still getting error.

error: 'OgreSwordStikes' has not been declared|[/quote]

and also the error

error: 'string' has not been declared|[/quote]

refuses to vanquish, lol/

This topic is closed to new replies.

Advertisement