4

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:
.....
3
  • is there anything between the line containing "public:" and the 1st constructor you show in the question? Commented Dec 16, 2008 at 21:13
  • mh I cannot match the line numbers with the ones in your code. And would you mind posting the complete errors. Commented Dec 16, 2008 at 21:14
  • what happens when you remove some of the #includes? Commented Dec 16, 2008 at 21:15

8 Answers 8

19
#ifndef Weapon
#define Weapon

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

Sign up to request clarification or add additional context in comments.

7 Comments

@Paul Betts: I changed WEAPON_H to WEAPON_H in your answer. See stackoverflow.com/questions/228783/…
If your compiler supports #pragma once you might consider using that instead, as it's potentially faster, and definitely cleaner to read.
@mackenir, I feel like the #ifndef/#define/#endif thing has turned into an idiom by now, so it's not really a matter of being easier to read. I would look askance at any header that didn't have it because it's become so ubiquitous.
@mackenir: Herb Sutter and Andrei Alexandrescu write in their books that 'most compilers' nowadays recognize macro guards and offer the same performance advantage as using #pragma once
Standing back (and shaking off the habits of years) I think a single-line #pragma at the top of the file is cleaner than bracketing the entire header in a bunch of preprocessor boilerplate. I've screwed up the old style way of doing things in the past (wrong symbol copy-n-pasted to a new hdr).
|
7

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.

Comments

5

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.

Comments

4

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

1 Comment

The preprocessor wouldn't munge the WeaponsDB entries though since it goes by whole words. Otherwise if someone were to #define C they'd be in a whole world of strife.
2

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.

1 Comment

You can do this in gcc with the -E switch: gcc <all the switches you currently have> -E > output
0

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

Comments

0

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.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.