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.
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.
I'm getting an error I typically see if I forget to add on the std:: to an object like string. In this case, its my own namespace called x. The specific errors are as follow:
Error 59 error C2143: syntax error : missing ';' before '*' e:\cppworkspace\xengine\xengine\code\include\x\graphics\graphics.h 32 1 XEngine
Error 60 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int e:\cppworkspace\xengine\xengine\code\include\x\graphics\graphics.h 32 1 XEngine
The graphics header file:
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include "x/x.h"
#include "x/graphics/d3dwrapper.h"
namespace x
{
class Graphics
{
public:
Graphics() {}
~Graphics(){}
bool Init(int32 width, int32 height, bool fullscreen, HWND hwnd);
void Destroy();
void Update();
void Render();
protected:
private:
Graphics(const Graphics& other) {}
D3DWrapper* m_d3d; // ERROR IS HERE
};
}
#endif // GRAPHICS_H
The D3DWrapper header file:
#ifndef D3DWRAPPER_H
#define D3DWRAPPER_H
#include "x/x.h"
#include "x/system/system.h"
namespace x
{
class D3DWrapper
{
public:
D3DWrapper();
~D3DWrapper() {}
bool Init(int32 width, int32 height, bool fullscreen, HWND hwnd);
void Release();
bool InitScene();
void BeginScene(Color color);
void EndScene();
inline ID3D11Device* GetDevice() { return m_device; }
inline ID3D11DeviceContext* GetDeviceContext() { return m_deviceContext; }
protected:
private:
D3DWrapper(const D3DWrapper& other) {}
ID3D11Device* m_device;
ID3D11DeviceContext* m_deviceContext;
IDXGISwapChain* m_swapChain;
ID3D11RenderTargetView* m_renderTargetView;
};
}
#endif // D3DWRAPPER_H
I cannot seem to figure out what could possibly be wrong with my code to get this error so any help is appreciated.
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.
I have a class "GameOverState" which has a private member
static const std::string s_gameOverID;
In GameOverState.cpp I am initialising as :
const std::string GameOverState::s_gameOverID = "GAMEOVER";
I am getting the following errors:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2440: 'initializing' : cannot convert from 'const char [9]' to 'int'
error C2377: 'std::string' : redefinition; typedef cannot be overloaded with any other symbol
error C2373: 's_gameOverID' : redefinition; different type modifiers
error C2143: syntax error : missing ';' before 'GameOverState::s_gameOverID'
I have a PlayState class/PauseState class which have the same implementation which are working fine. How do I fix this bug??
GameOverState.h
#pragma once
#include "GameState.h"
#include "PlayState.h"
#include "MenuState.h"
#include "PauseState.h"
#include "AnimatedGraphic.h"
#include <string>
class GameObject;
class GameOverState : public GameState
{
public:
virtual void update();
virtual void render();
virtual bool onEnter();
virtual bool onExit();
virtual std::string getStateID() const { return s_gameOverID; }
private:
static void s_gameOverToMain();
static void s_restartPlay();
static const std::string s_gameOverID;
std::vector<GameObject*> m_gameObjects;
}
GameOverState.cpp
#include "GameOverState.h"
const std::string GameOverState::s_gameOverID = "GAMEOVER";
void GameOverState::s_gameOverToMain()
{
TheGame::Instance()->getStateMachine()->changeState(new MenuState());
}
void GameOverState::s_restartPlay()
{
TheGame::Instance()->getStateMachine()->changeState(new PlayState());
}
bool GameOverState::onEnter()
{
if (!TheTextureManager::Instance()->load("assets/gameover.png", "gameovertext", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/main.png", "mainbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
if (!TheTextureManager::Instance()->load("assets/restart.png", "restartbutton", TheGame::Instance()->getRenderer()))
{
return false;
}
GameObject* gameOverText = new AnimatedGraphic(new LoaderParams(200, 100, 190, 30, "gameovertext"), 2);
GameObject* button1 = new MenuButton(new LoaderParams(200, 200, 200, 80, "mainbutton"), s_gameOverToMain);
GameObject* button2 = new MenuButton(new LoaderParams(200, 300, 200, 80, "restartbutton"), s_restartPlay);
m_gameObjects.push_back(gameOverText);
m_gameObjects.push_back(button1);
m_gameObjects.push_back(button2);
std::cout << "entering PauseState\n";
return true;
}
You're missing the semicolon after the definition of GameOverState.
The preprocessor runs before compilation and basically just copy pastes the content of the header in the source file, altough we can't see that. An error resulting from a broken header can thus be pretty misleading.
It's legal to have class definitions inside a variable definition and the position of specifiers (like static) is not limited to the beginning of a declaration, either (for example, int const static x = 0; is fine).
So, your code looks like this to the compiler:
class GameOverState {} static const std::string GameOverState::s_gameOverID = "GAMEOVER";
Hopefully the errors make more sense now.
As everyone else has said, you're probably missing the #include line in your header if your other two classes are working fine. It's presuming and expecting an int, so that seems the case.
Make sure #include<string> is in your header
I've got an outline of a HashTable class I'm trying to make. I'm getting 3 errors output from Visual Studio, but I can't see the problem here. I'm fairly new to OO in C++ so it's probably something i've missed. It claims there is a problem with my array of vectors. The errors are:
error C2143: syntax error : missing ';' before '<' line 10
error C2238: unexpected token(s) preceding ';' line 10
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int line 10
Here's my complete class, it's pretty empty right now:
#include <iostream>
#include <vector>
#include "stdafx.h"
using namespace std;
class HashTable
{
private:
const static int buckets = 100;
vector<int> hashTable[buckets]; //Internal storage
int hash(int toHash); //Performs hash function
public:
HashTable(); //Constructor
HashTable(int s); //Constructor
~HashTable(); //Destructor
void add(int toAdd); //Adds an element to the HashTable
void remove(int toDelete); //Deletes an element from the HashTable
bool search(int toSearch); //Returns true if element in HashTable, false otherwise
int getSize(); //Returns size of HashTable
void print(); //Prints current state of the hashtable
//TODO more methods...?
};
//Definitions...
HashTable::HashTable()
{
}
HashTable::~HashTable()
{
//cout << "Destroyed" << endl;
}
void HashTable::add(int toAdd)
{
//elements[hash(toAdd)] = toAdd;
}
void HashTable::remove(int toDelete)
{
}
bool HashTable::search(int toSearch)
{
}
int HashTable::getSize()
{
//return size;
}
void HashTable::print()
{
}
int main()
{
return 0;
}
The C++ here is valid (once you fill in the empty functions). The problem is with how Visual C++ uses precompiled headers. When you use precompiled headers (the default setting), the Visual C++ compiler expects the first line of each implementation file to be #include "stdafx.h", and doesn't compile anything that appears before that.
This means the the include of <vector> in your code is ignored, and thus compiling vector<int> causes an error.
If you move the line #include "stdafx.h" to the top this should compile. Or you can disable precompiled headers in the project settings.