1

I have been having issues with Visual Studio 2013. I can't explain why its happening. The compile issue is shown in the pictures below.
I have one class "PhysicsEngine.cpp/h" that use the class "RigidBody.cpp/h" just fine. Once I attempt to use the RigidBody class in my Character class header, it fails to see the RigidBody class identifier. I have included "RigidBody.h" in my file, and even tried prototyping the class. Interesting enough when I delete the "RigidBody.h" header inclusion, it spits out even more errors, leading me to believe it is reading it. Is there something I'm doing wrong?

Here is the output I received as an error:

1>------ Rebuild All started: Project: SideScroller, Configuration: Debug Win32 ------
1>  WorldChunkManager.cpp
1>  World.cpp
1>  Vector.cpp
1>e:(directory)\vector.cpp(41): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1>e:(directory)\vector.cpp(48): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1>  Tile.cpp
1>  RigidBody.cpp
1>e:(directory)\character.h(21): error C2146: syntax error : missing ';' before identifier 'pos'
1>e:(directory)\character.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:(directory)\character.h(21): error C2143: syntax error : missing ';' before '='
1>e:(directory)\character.h(21): error C2238: unexpected token(s) preceding ';'

Here is the code for the RigidBody.h

#ifndef RH
#define RH

#include "Vector.h"
#include "World.h"
#include <cmath>
#include <list>

class RigidBody
{
    private:
    int id;
    float gravityFactor;
    float airResistFactor;

    float mass;
    float elasticity;

    Vector pos = Vector(0, 0);
    int width;
    int height;

    Vector velocity = Vector(0, 0);

    std::list<Vector>* additiveVelocities;
    std::list<Vector>* forces;
public:
    RigidBody(int x, int y, int w, int h, float mass, float elasticity, float gravityFactor,         float airResistFactor);
    ~RigidBody();

    static int rigidBodyCount;

    //Get/Set
    int getID();
    double getX();
    double getY();
    float getGravFact();
    float getAirFact();
    float getMass();
    float getElast();
    Vector getPos();
    Vector getVelocity();
    std::list<Vector>* getadditiveVelocities();
    std::list<Vector>* getForces();

    void setX(double x);
    void setY(double y);
    void setVelocity(Vector& vec);
    void setPos(Vector& vec);

    //Interactino Functions
    void addForce(Vector force);
    void addVelocity(Vector velocity);
};


#endif

Here is the code that will not work (Character.h):

#ifndef CHARACTER_H
#define CHARACTER_H

#include "SDL.h"
#include "Camera.h"
#include "InputManager.h"
#include "Vector.h"
#include "RigidBody.h"

//TEMP DIM
const int CHAR_H = 64;
const int CHAR_W = 12;

class Character
{
private:
    double speed;

    //Vector pos = Vector(0, 0);

    RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);

    InputManager* inputPtr;
public:
    Character(int x, int y, InputManager* inputPtr);

    void getXY(int* dest);
    void getChXY(int* dest);
    void getCenterXY(int* dest);

    //Update
    void update(long double last);

    bool isFloor();

    //Draw
    void draw(SDL_Surface* screen, Camera* camPtr);
};

#endif

Let me know if there is anything additional you could need to find the problem!!! I don't want to overload the page with potentially irreverent information. Thank you!

Ashton Hunger
  • 33
  • 1
  • 3

1 Answers1

0

I think the problem with this line isn't that it's failing to find an identifier:

RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);

Rather, it's that you're trying to do an illegal initialization, in a C#-type way. You can't just assign a value to a class member as part of its declaration like that in C++. You need to assign the initial value in the constructor, or in an initializer attached to the constructor.

Dan Korn
  • 1,274
  • 9
  • 14
  • I attempted to change it, but its still gave the same error. Am I understanding why you're saying? I changed the header to say "RigidBody* pos;" and then in the Character.cpp constructor I put "pos = new RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);". – Ashton Hunger Dec 22 '14 at 22:04
  • Well, okay, if you're going to change it to a pointer type, then you could just use a forward declaration in the header, i.e. `class RigidBody;`, then #include the necessary header in the .cpp file. – Dan Korn Dec 22 '14 at 22:05
  • I would just preprocess the file and see what the compiler is actually looking at: http://stackoverflow.com/questions/8978997/how-can-i-see-the-output-of-the-visual-c-preprocessor – Dan Korn Dec 22 '14 at 22:09
  • That worked, thank you!! Just being curious. Do you know why putting the #include didn't work but forward declaration did? So I don't run into similar problems in the future. – Ashton Hunger Dec 22 '14 at 22:09
  • Use The Force, I mean the preprocessor. – Dan Korn Dec 22 '14 at 22:10