Chad
Chad

Reputation: 2465

Problem with a constructor c++

So I have this code for these Constructors of the Weapon class:

Weapon(const WeaponsDB * wepDB);
Weapon(const WeaponsDB * wepDB_, int * weaponlist);
~Weapon(void);  

And I keep getting an error:

1>c:\users\owner\desktop\bosconian\code\bosconian\weapon.h(20) : error C2062: type 'int' unexpected

and ensuing errors (more than listed):

1>c:\users\owner\desktop\bosconian\code\bosconian\weapon.h(21) : error C2059: syntax error : '('
1>c:\users\owner\desktop\bosconian\code\bosconian\weapon.h(21) : error C2238: unexpected token(s) preceding ';'
1>c:\users\owner\desktop\bosconian\code\bosconian\weapon.h(33) : error C2327: '<unnamed-tag>::maxWeapons' : is not a type name, static, or enumerator
1>c:\users\owner\desktop\bosconian\code\bosconian\weapon.h(33) : error C2065: 'maxWeapons' : undeclared identifier
1>c:\users\owner\desktop\bosconian\code\bosconian\weapon.h(38) : warning C4094: untagged 'class' declared no symbols

I'm a semi-newbie and I haven't been able to figure it out.

Line 21 is the second constructor, the first one doesn't cause an error. Also, if I comment out this constructor I still get all the errors listed after that constructors. Any idea what the problem might be?

Here is the preceding code for reference:

#ifndef Weapon
#define Weapon
#include <allegro.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class WeaponsDB;
class MenuDriver;
class Ammo;

class Weapon
{
public:
.....

Upvotes: 4

Views: 2018

Answers (8)

Calyth
Calyth

Reputation: 1673

Like other answers already made available, I also suspect the preprocessor directive.

To confirm, say on GCC, you can request it to only run the preprocessor and save that output somewhere. There's probably similar features for the compiler that you use.

Upvotes: 2

Marc
Marc

Reputation: 1356

Just a quick note: in C++, unlike C, when a function (or destructor, in this case) doesn't have any parameter, you don't need to use (void), you just use ().

Upvotes: -1

Ana Betts
Ana Betts

Reputation: 74692

#ifndef Weapon
#define Weapon

This is almost certainly going to cause weirdness; call the constant WEAPON_H instead.

Upvotes: 19

0x00bc
0x00bc

Reputation:

GCC also supports "#pragma once" but it's not standard and code will be more portable if you use the traditional include guard #ifndef _MYFILE_H_ or some variant.

Upvotes: 0

isekaijin
isekaijin

Reputation: 19772

I don't know if this is Microsoft-specific (I have only used VS2005 recently), but this works. I start all my header files with:

#pragma once

Upvotes: 0

Stephen C. Steel
Stephen C. Steel

Reputation: 4430

To anplify Tim's answer. You see the code like this:

#ifndef Weapon
#define Weapon
#include <allegro.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class WeaponsDB;
class MenuDriver;
class Ammo;

class Weapon
{
public:
   Weapon(const WeaponsDB * wepDB);
   Weapon(const WeaponsDB * wepDB_, int * weaponlist);
   ~Weapon(void);
}

But you've defined the preprocessor macro Weapon as an empty string, so the compiler sees this:

#ifndef Weapon
#define Weapon
#include <allegro.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class sDB;
class MenuDriver;
class Ammo;

class 
{
public:
   (const sDB * wepDB);
   (const sDB * wepDB_, int * weaponlist);
   ~(void);
}

Just change the include guard to use a string that doesn't occur as a name (e.g. WEAPON_H_INCLUDED).

Upvotes: 4

Tim
Tim

Reputation: 20360

So you named your class the same as a preprocessor directive? That is something I would avoid.

Try changing your preprocessor Weapon or making a different class name. I think it will work better.

Upvotes: 7

Mark Ransom
Mark Ransom

Reputation: 308530

I think the problem is in the #define Weapon - any occurence of "Weapon" later on in the code will be removed or replaced by something you didn't intend.

Upvotes: 5

Related Questions