Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 997
Everday Avatar asked Jul 02 '26 08:07

Everday


1 Answers

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.

like image 183
rici Avatar answered Jul 05 '26 00:07

rici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!