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!
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.
Related
I'm trying to code a simple DirectX11 engine but I keep getting this strange error and I can't find the problem: I define a Terrain class and a Mesh class and #include the Mesh class in the Terrain class:
the Terrain class definition:
// Terrain.h
#pragma once
#include "Noise.h"
#include "Mesh.h"
class Terrain
{
public:
Terrain(float width, float depth, int numVerticesW, int numVerticesD);
~Terrain();
float GetHeight(float x, float z);
void Draw();
private:
Mesh mMesh; // I get the error on this line
Noise mNoiseGenerator;
std::vector<float> mHeights;
void CreateTerrain(float width, float depth, int numVerticesW, int numVerticesD);
float ComputeHeight(float x, float z, float startFrequency, float startAmplitude, float persistence, int octaves);
};
and the Mesh class definition:
// Mesh.h
#pragma once
#include <d3d11.h>
#include <vector>
#include "Game.h"
class Mesh
{
public:
Mesh();
~Mesh();
template <typename T, unsigned int N>
void LoadVertexBuffer(T data[][N], unsigned int size, bool dynamic = false);
void LoadIndexBuffer(std::vector<unsigned int> indices);
void SetVertexCount(unsigned int vertexCount);
void Bind();
void Draw();
private:
std::vector<ID3D11Buffer*> mVertexBuffers;
std::vector<unsigned int> mStrides;
ID3D11Buffer *mIndexBuffer;
unsigned int mVertexCount;
};
template <typename T, unsigned int N>
void Mesh::LoadVertexBuffer(T data[][N], unsigned int size, bool dynamic)
{
D3D11_BUFFER_DESC bufferDesc = {};
bufferDesc.Usage = dynamic ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_IMMUTABLE;
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.ByteWidth = sizeof(T[N]) * size;
bufferDesc.CPUAccessFlags = dynamic ? D3D11_CPU_ACCESS_WRITE : 0;
bufferDesc.MiscFlags = 0;
bufferDesc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA bufferData = {};
bufferData.pSysMem = data;
ID3D11Buffer *buffer;
Game::GetInstance()->GetDevice()->CreateBuffer(&bufferDesc, &bufferData, &buffer);
mVertexBuffers.push_back(buffer);
mStrides.push_back(sizeof(T[N]));
}
When I compile the code I get:
Severity Code Description Project File Line Suppression State
Error C3646 'mMesh': unknown override specifier DirectX11 engine 0.3 c:\users\luca\desktop\programming\code\c++\source\visual studio\directx11 engine 0.3\terrain.h 14
Severity Code Description Project File Line Suppression State
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int DirectX11 engine 0.3 c:\users\luca\desktop\programming\code\c++\source\visual studio\directx11 engine 0.3\terrain.h 14
I searched the web but most results show missing semicolons or circular inclusion issues but I can't find any.
EDIT
I found the issue but I can't explain why my solution works:
following the inclusion tree:
Terrain.h --> Mesh.h --> Game.h --> Renderer.h --> Terrain.h
eliminating #include "Terrain.h" (since I just declare Terrain * pointers inside the class) and adding it to Terrain.cpp seems to solve the issue.
So it must be a matter of circular inclusion, but shouldn't I be guarded against that by using header/include guards?
Your problem is that #pragma once only prevents against double inclusion. I.e. it makes the following safe (simplified to make it obvious) :
// Terrain.cpp
#include "Terrain.h"
#include "Terrain.h"
It does not solve circular inclusion, which is far harder to solve automatically. With double inclusion, it's clear which one is first. But a circle has no begin.
Mage/Interface/Context.h
#pragma once
#include <Mage/Interface/Element.h>
#include <Mage/Renderer/RenderingContext.h>
#include <Mage/Renderer/VertexBuffer.h>
#include <glm/glm.hpp>
namespace Mage {
namespace Interface {
class Context {
protected:
RenderingContext* ctx;
VertexBuffer* vbo;
glm::mat4 projection;
Mage::Interface::Frame* uiParent;
public:
Context(RenderingContext* ctx);
~Context();
void render();
Mage::Interface::Frame* createFrame();
};
}
}
Mage/Interface/Element.h
#pragma once
#include <vector>
#include <Mage/Interface/Context.h>
#include <glm/glm.hpp>
namespace Mage {
namespace Interface {
class Element {
protected:
Mage::Interface::Context* ctx;
std::vector<Element*> children;
glm::vec3 position;
float scale;
public:
virtual void draw();
void attach(Element* child) {
this->children.push_back(child);
}
inline glm::vec3 getPosition() {
return this->position;
}
float getScale() {
return this->scale;
}
};
// Frame is an untextured, single colour quad. Frame may contain other
// Elements.
class Frame : public Element {
public:
Frame();
Frame(glm::vec3 pos);
Frame(float width, float height);
Frame(glm::vec3 pos, float width, float height);
};
}
}
This gives me the following errors:
Error C2039 'Context': is not a member of 'Mage::Interface' Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
Error C2238 unexpected token(s) preceding ';' Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
Error C2143 syntax error: missing ';' before '*' Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Mage2D c:\users\jesse\documents\visual studio 2015\projects\mage2d\include\mage\interface\element.h 14
When I take out Mage::Interface::Context* ctx, the code compiles fine. I figured I must have missed a semi colon, but I can't see it - it all seems to check out just fine to me.
You have a circular dependency. Element.h includes Context.h and Context.h includes Element.h, that's not going to work.
The way to solve that is to forward-declare types instead of including their headers whenever you can, it'll also reduce compile times.
I am trying to create a vector of custom objects defined in a header file and then initialize them in the actual cpp file. I'm getting the following errors in Visual Studio:
error C2976: 'std::vector' : too few template arguments
error C2065: 'Particle' : undeclared identifier
error C2059: syntax error : '>'
In the code below, the vector is defined in Explosion.h.
Particle.h:
#pragma once
class Particle : public sf::CircleShape {
public:
float speed;
bool alive;
float vx;
float vy;
Particle(float x, float y, float vx, float vy, sf::Color color);
~Particle();
};
Particle.cpp:
#include <SFML/Graphics.hpp>
#include "Particle.h"
Particle::Particle(float x, float y, float vx, float vy, sf::Color color) {
// Inherited
this->setPosition(x, y);
this->setRadius(5);
this->setFillColor(color);
// Player Defined Variables
this->speed = (float).05;
this->alive = true;
this->vx = vx;
this->vy = vy;
}
Particle::~Particle() {
}
Explosion.h:
static const int NUM_PARTICLES = 6;
#pragma once
class Explosion {
public:
std::vector<Particle*> particles;
bool alive;
Explosion();
~Explosion();
};
Explosion.cpp:
#include <SFML/Graphics.hpp>
#include "Particle.h"
#include "Explosion.h"
Explosion::Explosion() {
this->alive = true;
// Add Particles to vector
for (int i = 0; i < NUM_PARTICLES; i++) {
this->particles.push_back(new Particle(0, 0, 0, 0, sf::Color::Red));
}
}
Explosion::~Explosion() {
}
I'm sure there is something fundamentally wrong here since C++ is fairly new to me.
You need to tell Explosion.h what a Particle is.
In this case, Explosion.h is using Particle*, so a forward declartion will suffice.
Explosion.h
class Particle; // forward declaration of Particle
class Explosion {
// ...
};
You could also simply #include "Particle.h, however as your projects increase using forward declarations (instead of direct includes) can significantly reduce your build times.
Currently I have a tic tac toe board "tttBoard" with a constructor
tttBoard::tttBoard() {
isX = true;
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
gBoard[x][y]=sEmp;
}
}
}
That should create a new board and fill it with the enum sEmp. isX is a boolean which marks that the first player moves first. Despite having #include "tttBoard.h" and (I believe) having the constructor in that header file (below), I've run across the same errors over and over:
error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
tttBoard.h
#ifndef tttBoard
#define tttBoard
class tttBoard {
public:
tttBoard();
void Draw();
void Move(int x, int y);
char* getValue(int x, int y);
private:
enum sVal {
sEmp,
sX,
sO
};
sVal gBoard[3][3];
bool isX;
}
#endif
#ifndef tttBoard
#define tttBoard
class tttBoard {
That's not a proper include guard. You're defining tttBoard as an empty symbol and then using the same name for the class.
#ifndef TTT_BOARD_H
#define TTT_BOARD_H
class tttBoard {
// stuff
};
#endif
I am getting a very confusing compiler error when building the following test code.
f:\data\sdks\smctc-1.00\examples\ik_pf\msvc\MarkerSampler.inl(16): error C2143: syntax error : missing ';' before 'MarkerSampler<MarkerT>::sample2'
f:\data\sdks\smctc-1.00\examples\ik_pf\msvc\MarkerSampler.inl(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Note sample builds without error, but sample2 is causing the problem. The only difference being one explicitly details the return type without the use of a typedef and the other uses the typdef'ed version.
.h file
#pragma once
#ifndef __MARKERSAMPLER_H_INCLUDED__
#define __MARKERSAMPLER_H_INCLUDED__
#include <vector>
#include <string>
#include <qthread.h>
#include <Wm5Vector3.h>
#include "UtilityFunctions.h"
#include "Marker.h"
#include "MarkerSet.h"
template <class MarkerT>
class MarkerSampler : public QThread
{
typedef std::vector<MarkerT> MarkerVector ;
typedef MarkerSet<MarkerT> MarkerSetT ;
typedef std::vector<MarkerSetT> MarkerSetVector ;
public :
MarkerSampler(const std::string inputDataDirectory, const unsigned int nSamples, const unsigned int startFrame, const unsigned int nFramesToUse) :
inputDataDirectory_(inputDataDirectory), nSamples_(nSamples), startFrame_(startFrame), nFramesToUse_(nFramesToUse) {seed = -time(NULL) ;}
~MarkerSampler() {}
std::vector<MarkerSet<MarkerT> > sample(const double scaleFactor = 1.0, const double noiseSD = 0.0) ;
MarkerSetVector sample2(const double scaleFactor = 1.0, const double noiseSD = 0.0) ;
protected:
void run();
private:
int seed ;
const std::string inputDataDirectory_ ;
const unsigned int nSamples_ ;
const unsigned int startFrame_ ;
const unsigned int nFramesToUse_ ;
} ;
#include "MarkerSampler.inl"
#endif
.inl file
template <class MarkerT>
std::vector<MarkerSet<MarkerT> > MarkerSampler<MarkerT>::sample(const double scaleFactor, const double noiseSD)
{
....
}
template <class MarkerT>
MarkerSetVector MarkerSampler<MarkerT>::sample2(const double scaleFactor, const double noiseSD)
{
....
}
The missing ; part of that error is slightly misleading, the compiler actually just doesn't recognize the MarkerSetVector type. I think this is because it's typedef'd inside the MarkerSampler class.
If you explicitly state the scope of the typedef it should fix the error. Though (depending on your compiler at least) you may also need to add in the typename keyword, since MarkerSetVector is a dependant type. Here's a fixed example:
template <class MarkerT>
typename MarkerSampler< MarkerT >::MarkerSetVector MarkerSampler<MarkerT>::sample2(const double scaleFactor, const double noiseSD)
{
....
}