'Declaring a namespace causes hundreds of compiler errors in cpp, what am I doing wrong?
I am relatively new to cpp and my code is probably atrocious. However, I am currently trying to implement my own namespace into my program since I want to avoid a conflict between my Polygon class and the windows.h Polygon class. However upon watching some tutorials and reading other threads on how to implement namespaces I implemented my own and while I get no errors in visual studio, I get 303 errors upon compiling (I am using the visual studio local windows debugger), I am compiling in Debug mode for windows x64.
Here's my source/implementation Source.cpp file
#include <iostream>
#include <math.h>
#include <ctime> // Temp
#include <vector>
#include <stdio.h> // Temp
#include <windows.h>
#include "Vec2.h"
#include "Log.h"
#include "Time.h"
#include "Boundary.h"
#include "Ray.h"
#include "Particle.h"
#include "Polygon.h"
#include "Path.h"
#include "Circle.h"
// Renderer
SDL_Renderer* renderer;
SDL_Window* window;
bool isRunning;
bool fullscreen;
// Mouse
int mouseX;
int mouseY;
Uint32 mouse;
// Keyboard
GameEngine::Vec2 arrowInp;
// Collision Stack
std::vector<GameEngine::Boundary> collisionStack;
// Log
GameEngine::Log logger;
// Physics
GameEngine::Time deltaTime = GameEngine::Time(0.0f);
void handleEvents();
void start();
void update();
void render();
void createWindow();
void destroyWindow();
// Non Main
void collision();
using GameEngine::Particle;
using GameEngine::Boundary;
using GameEngine::Path;
using GameEngine::Circle;
using GameEngine::Vec2;
// Drawing Vars
Particle particle = Particle(300, 359.99f, Vec2(900, 700));
Boundary boundaries[] = { Boundary(Vec2(800, 0), Vec2(800, 1000)), Boundary(Vec2(300, 300), Vec2(400, 700)), Boundary(Vec2(300, 700), Vec2(800, 800)) };
GameEngine::Polygon square = GameEngine::Polygon(Vec2(800, 400), 45, Vec2(400, 200), 4);
GameEngine::Polygon poly1 = GameEngine::Polygon(Vec2(200, 400), Vec2(200, 200), 6);
GameEngine::Polygon ellipse = GameEngine::Polygon(Vec2(1200, 600), Vec2(200, 400), 64);
Path path = Path(Vec2(500, 100));
Circle circle = Circle(100, Vec2(800, 500));
GameEngine::Polygon player = GameEngine::Polygon(Vec2(700, 400), 0, Vec2(60, 60), 16);
float moveSpeed = 1000.0f;
float rotator = 0;
int main() { // Entry Point
clock_t m_LastClock = NULL; // Last Counter = 0
// Start Counter
start(); // For user
while (isRunning) {
clock_t m_CurrentClock;
m_CurrentClock = clock(); // Get Counter
deltaTime.SetTime((float)(m_CurrentClock - m_LastClock) / CLOCKS_PER_SEC);
handleEvents();
render();
m_LastClock = m_CurrentClock;
}
destroyWindow();
return 0;
}
// Initialise the window to draw to
void createWindow() {
// Set window size and type
fullscreen = true;
Uint32 flags = 0;
flags = SDL_WINDOW_RESIZABLE;
if (fullscreen) {
flags = flags | SDL_WINDOW_MAXIMIZED;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
logger.Message("Subsystems Initialised");
window = SDL_CreateWindow("2D Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 300, 300, flags);
if (window) {
logger.Message("Window Created");
// Minimum window size
SDL_SetWindowMinimumSize(window, 1000, 1000);
}
// Create Renderer for window
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
SDL_SetRenderDrawColor(renderer, 121, 121, 121, 255);
logger.Message("Renderer Created");
// Set how to blend alphas and colours
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
isRunning = true;
}
}
}
void destroyWindow() {
// Frees memory associated with renderer and window
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); //error here
SDL_Quit();
}
// Handles SDL events
void handleEvents() {
// Check for quit
SDL_Event event;
SDL_PollEvent(&event);
// Check we have latest inputs
SDL_PumpEvents();
// Reset inputs
arrowInp.x = 0; arrowInp.y = 0;
// If we get quit event, stop running and free up memory
switch (event.type) {
case SDL_QUIT:
isRunning = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_LEFT: arrowInp.x = -1; break;
case SDLK_RIGHT: arrowInp.x = 1; break;
}
switch (event.key.keysym.sym)
{
case SDLK_UP: arrowInp.y = -1; break;
case SDLK_DOWN: arrowInp.y = 1; break;
}
break;
default:
break;
}
}
// Render Function
void render() {
// Set back ground colour and clear renderer every frame
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
update();
// Draw to the screen
SDL_RenderPresent(renderer);
}
// Called when the program starts
void start() {
createWindow();
path.addPoints(std::vector<Vec2> { Vec2(200, 100), Vec2(200, 800), Vec2(350, 800), Vec2(700, 650), Vec2(400, 400), Vec2(200, 100)});
}
// Repeats every frame
void update() {
printf("%f secs \n", deltaTime.GetSeconds());
// Get the mouse' current state
mouse = SDL_GetMouseState(&mouseX, &mouseY);
particle.setPos(Vec2((float)mouseX, (float)mouseY));
// Draw Boundaries
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
rotator += 0.003f;
poly1.setRot(rotator);
// Player
Vec2 velocity = arrowInp * moveSpeed;
player.setPos(player.Pos += velocity * deltaTime.GetSeconds());
player.Show(renderer);
float f = deltaTime.GetSeconds();
square.Show(renderer);
poly1.Show(renderer);
ellipse.Show(renderer);
path.Show(renderer);
collision();
}
void collision() {
// Clears stack
collisionStack.clear();
// Adds colliders to stack
square.makeCollider(collisionStack);
poly1.makeCollider(collisionStack);
ellipse.makeCollider(collisionStack);
path.makeCollider(collisionStack);
player.makeCollider(collisionStack);
// Draw Particles
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 100);
particle.collide(renderer, collisionStack);
}
//// TODO
// deltaTime
// gravity
// collision
// code optimisation
Here's my Path.h header (as an example of how my headers are laid out):
#ifndef PATH_H
#define PATH_H
#include "Boundary.h"
#include "Vec2.h"
#include "SDL.h"
namespace GameEngine {
class Path
{
private:
// Properties
Vec2 pos;
std::vector<Vec2> points;
std::vector<Boundary> bounds;
public:
// Default Constructor
Path()
: pos{ 0, 0 }
{
}
// Specific Constructor takes position
Path(const Vec2 _pos)
: pos{ _pos }
{
}
// Adds vertices to path
void addPoint(const Vec2 point) {
points.push_back(point);
makeBounds();
}
void addPoints(const std::vector<Vec2> pts) {
for (Vec2 point : pts) {
points.push_back(point);
makeBounds();
}
}
// Shows the path
void Show(SDL_Renderer* renderer) {
for (Boundary& bound : bounds) {
bound.Show(renderer);
}
}
// Adds the path to the collision stack
void makeCollider(std::vector<Boundary>& collisionStack) {
for (Boundary& bound : bounds) {
collisionStack.push_back(bound);
}
}
private:
// Generates the Boundaries for the path
void makeBounds()
{
bounds.clear();
for (int i = 0; i < points.size() - 1; i++) {
Boundary bound(Vec2(points[i].x + pos.x, points[i].y + pos.y), Vec2(points[i + 1].x + pos.x, points[i + 1].y + pos.y));
bounds.push_back(bound);
}
}
};
}
#endif
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
