Everday
Everday

Reputation: 33

C++ Vector Syntax Errors

I'm getting std::vector errors I've never heard of and unable to find anything about it.

ShootManager.h

#pragma once

#include "VGCVirtualGameConsole.h"
#include "Shot.h"
#include <vector>

using namespace std;

class ShootManager
{
public:
    ShootManager();
    ~ShootManager();

    void Destroy(int ShotNr);
    void Update();
    void Draw();
    void Fire(Shot* shot);

    vector<Shot*> shots;
};

Shot.h

#pragma once

#include "VGCVirtualGameConsole.h"
#include "ShootManager.h"

using namespace std;

class Shot
{
public:
    virtual ~Shot();
    virtual void Update() = 0;
    void Draw();
    void Move();

    enum Alignment
    {
        FRIEND, ENEMY
    };

protected:
    VGCVector position;
    VGCVector speed;
    Alignment alignment;
    bool Destroyed = false;
};

I get these errors

Error   3   error C2059: syntax error : '>' 
Error   7   error C2059: syntax error : '>' 
Error   1   error C2061: syntax error : identifier 'Shot'   
Error   5   error C2061: syntax error : identifier 'Shot'   
Error   2   error C2065: 'Shot' : undeclared identifier 
Error   6   error C2065: 'Shot' : undeclared identifier 
Error   4   error C2976: 'std::vector' : too few template arguments 
Error   8   error C2976: 'std::vector' : too few template arguments 

Identifier errors are for this line

void Fire(Shot* shot);

Rest for

vector<Shot*> shots;

These two lines were working perfectly for quite some time and I don't really know what caused it to suddenly start making these errors. I've yet to start trying to fill the vector and none of the functions are being called as of yet.

Upvotes: 3

Views: 6298

Answers (2)

rici
rici

Reputation: 241731

Your two header files reference each other. However, Shot.h is clearly necessary for ShootManager.h because Shot is referenced in ShootManager.

So it makes a difference whether a client program #includes Shot.h or ShootManager.h, and if it #includes both, in which order. If Shot.h is #included first, things will work. Otherwise they won't, because you cannot template a class using an undeclared identifier.

I'd remove #include "ShootManager.h" from Shot.h, and then fix whatever breaks as a result (probably a missing #include "ShootManager.h" in client code.)

As @kfsone points out in a comment, you could also remove #include "Shot.h" from ShootManager.h, replacing it with a forward-declaration class Shot;. Doing so will force client code to include both ShootManager.h and Shot.h if they use both classes, so it might require even more fixups, but it would certainly be the cleanest solution.

Upvotes: 3

Mahesh
Mahesh

Reputation: 34625

Error has nothing to do with std::vector. You have got circular dependency between these two header files. I recommend forward declaring Shot in ShootManager header file.

// ShootManager.h

#include "VGCVirtualGameConsole.h"
#include <vector>
class Shot;

Also, avoid bringing entire std namespace to the header. Instead write using std::vector; or prefix std where ever you use vector.

Upvotes: 2

Related Questions