Hello

Help with namespaces, and classes.

I'm trying to grasp the basics of namespaces, and classes within the namespaces. I was wondering if someone could take a look at my code, and give some pointers on what to do, and what not to do, also a bit on the errors to help mt improve. Thanks.

header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H


namespace OgreWeaponStrikes
{
class OgreSwordStrikes()
{


    public:
      void OgreSwordStrikes();
};
}

#endif // OGREWEAPONSTRIKES_H


cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include "OgreWeaponStrikes.h"
# include<cstdlib>
#include<string>
using namespace std;

 void OgreWeaponStrikes::OgreSwordStrikes::OgreSwordStrikes()
{
    int String;
    int RandomNumber;


}


|error: expected unqualified-id before ')' token|
error: return type specification for constructor invalid|
|error: invalid use of incomplete type 'struct OgreWeaponStrikes::OgreSwordStrikes'|
error: forward declaration of 'struct
: 4 errors, 0 warnings ===|
class OgreSwordStrikes() on line 7 of the header, remove ();
Header line 12, Constructors do not have a type. Functions should not share names with the constructors.

Line 7 in CPP remove type in function header.

I recommend getting the basics of classes down first, then think about namespaces later.
Thanks, I was able to clean up the code a bit more with that help, and a couple other things I noticed. It's till got errors. One I'm wondering about.

Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H


namespace OgreWeaponStrikes
{
class OgreSwordStrikes()
{


    public:
      OgreRanSwordStrikes();
};
}

#endif // OGREWEAPONSTRIKES_ 


CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
using namespace std;

OgreWeaponStrikes::OgreSwordStrikes::OgreRanSwordStrikes()
{
    int String;
    int RandomNumber;

return 0;
}


errors

error: expected unqualified-id before ')' token|
error: expected unqualified-id before '{' token|
|error: ISO C++ forbids declaration of 'OgreRanSwordStrikes' with no type|
error: invalid use of incomplete type 'struct OgreWeaponStrikes::OgreSwordStrikes'|
error: forward declaration of 'struct OgreWeaponStrikes::OgreSwordStrikes'|
||=== Build finished: 5 errors, 0 warnings ===|

I'm wondering about

forward declaration of 'struct OgreWeaponStrikes::OgreSwordStrikes'|


what exactly is a forward declaration?

If someone can show me how to get this one class working within my namespace, and let me know what's been done wrong, I think I can carry on with all other classes that I want to place within the name space :)
Last edited on
I was able to find one error myself. I had brackets after my class declaration. New sample of code is.

header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef OGREWEAPONSTRIKES_H
#define OGREWEAPONSTRIKES_H

// 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 EnemyString);
};


CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include <ctime>


using namespace std;

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

    switch(OgreSwordStrikesID)


that's only partial of the cpp..

errors are.

error: 'string' has not been declared|
error: ISO C++ forbids declaration of 'OgreRanSwordStrikes' with no type|
prototype for 'int OgreWeaponStrikes::OgreSwordStrikes::OgreRanSwordStrikes(int, int, int, std::string)' does not match any in class 'OgreWeaponStrikes::OgreSwordStrikes'|
error: candidate is: int OgreWeaponStrikes::OgreSwordStrikes::OgreRanSwordStrikes(int, int, int, int)|
||=== Build finished: 4 errors, 0 warnings ===|
I've cleaned up the code considerably with the help of some people from game dev, but I'm still getting two errors, that I don't know about...

header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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. 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.
    //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 OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName);
};

}

#endif // OGREWEAPONSTRIKES_ 


CPP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include "OgreTitanium.h"
# include<cstdlib>
#include<string>
#include<ctime>


using namespace std;

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

int OgreSwordStikes::OgreRanSwordStrikesBtl(int OgreSwordStrikesID, int DamageAmount, int EnemyHealth, string EnemyName)
{

return 0;
}

 


the errors are

error: 'OgreSwordStikes' has not been declared|
error: 'string' has not been declared|


I have no clue what's causing them.
bump...I can't give up on this, lol.
error: 'OgreSwordStikes' has not been declared|

You've misspelled OgreSwordStrikes here. Also, OgreSwordStrikes is in the OgreWeaponStrikes namespace, so you'll have to change this:

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

to this:

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

error: 'string' has not been declared|

This error is happening in the header. string is in the std namespace, so you should put std:: in front of string EnemyName in the function header.
Thank you!

now I have my first workin namespace, and class. I appreciate the help, I've been digging for this for days, had some run ins with some really helpful people like yourself, and a some not so helpful people. Thanks man :)
Topic archived. No new replies allowed.