Jump to content
  • Advertisement
Sign in to follow this  
InterFiction

C++ namespace, and class help

This topic is 2588 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

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
My mistake.
It should have been:
int OgreSwordStrikes::OgreRanSwordStrikes(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName)


L. Spiro

Share this post


Link to post
Share on other sites
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/

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!