C++ Cannot debug cause of errors, but no errors in code.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C++ Cannot debug cause of errors, but no errors in code.

  1. #1
    Join Date
    Oct 2017
    Posts
    1

    C++ Cannot debug cause of errors, but no errors in code.

    I have been getting a few errors which i cant seem to fix on my own. Could someone try to find the problems with reference to my code? In the error it points to my Invaders.cpp and .h file. I will provide them to you, because i cannot manage to fix the errors. I dont think you will need the other code or files, since the errors is only in the two mentioned files below. This is something i will need to fix so i can debug correctly to see the results. Can someone help?
    List of errors when debugging
    https://i.stack.imgur.com/yyh0y.png
    Invaders.cpp
    Code:
    #include "Invaders.h"
        #include <iostream>
        #include "Ship.h"
        #include "Entity.h"
        #include "Invader.h"
        #include <sstream>
        using namespace std;
        
        static const int FRAMES_PER_SECOND = 85;
        static const int EDGE_DELTA = 32;
        static const int SPAWN_DELTA = 16;
        static const double SPAWN_TIME = 1.0;
        static const VGCColor backgroundColor(255, 0, 0, 0);
        static const VGCColor fontColor(255, 255, 0, 0);
        static const string fontName("Times New Roman");
        static const int FONT_SIZE = 24;
        
        
        Invaders::Invaders() :
        	mSpawnTimer(VGCClock::openTimer(SPAWN_TIME)),
        	mFont(VGCDisplay::openFont(fontName, FONT_SIZE)),
        	mShip(new Ship(/*VGCVector(VGCDisplay::getWidth() / 2, VGCDisplay::getHeight() / 2)*/)),
        	mEntities(),
        	mScore(0),
        	mGameOver(false) {
        	mEntities.push_back(mShip);
        }
        Invaders::~Invaders() {
        	while (!mEntities.empty()) {
        		delete mEntities.back();
        		mEntities.pop_back();
        	} 
        	VGCDisplay::closeFont(mFont);
        	VGCClock::closeTimer(mSpawnTimer);
        }
        void Invaders::run() {
        	VGCTimer timer = VGCClock::openTimer(1.0 / FRAMES_PER_SECOND);
        	while (!mGameOver && VGCVirtualGameConsole::beginLoop()) {
        		VGCClock::reset(timer);
        		if (VGCDisplay::beginFrame()) {
        			VGCDisplay::clear(backgroundColor);
        			spawnInvader();
        			tick();
        			detectCollisions();
        			detectGameOver();
        			renderBackground();
        			renderForeground();
        			renderLife();
        			renderScore();
        			killDeadEntities();
        			killEdgeEntities();
        			VGCDisplay::endFrame();
        		}
        
        		VGCVirtualGameConsole::endLoop();
        		VGCKeyboard::clearBuffer();
        		VGCClock::wait(timer);
        	}
        	VGCClock::closeTimer(timer);
        }
        void Invaders::spawnInvader() {
        	if (VGCClock::isExpired(mSpawnTimer)) {
        		//...
        	}
        }
        void Invaders::tick() {
        	EntityVector entities(mEntities);
        	for (EntityVector::iterator i = entities.begin(); i != entities.end();
        		i++) {
        		Entity *entity = *i;
        		entity->tick(mEntities);
        	}
        }
        void Invaders::detectCollisions() {
        	EntityVector entities(mEntities);
        	for (EntityVector::size_type i = 0; i < entities.size(); i++) {
        		Entity *entity0 = entities[i];
        		for (EntityVector::size_type j = i + 1; j < entities.size(); j++) {
        			Entity *entity1 = entities[j];
        			if (isOverlap(entity0, entity1) && entity0->getCategory() !=
        				entity1->getCategory()) {
        				mScore += entity0->collide(entity1, mEntities);
        				mScore += entity1->collide(entity0, mEntities);
        			}
        		}
        	}
        }
        
        
        void Invaders::renderBackground() {
        	for (EntityVector::iterator i = mEntities.begin(); i != mEntities.end();
        		i++) {
        		Entity *entity = *i;
        		entity->render(Entity::BACKGROUND);
        	}
        }
        void Invaders::renderForeground() {
        	//...
        }
        
        void Invaders::renderLife() {
        	ostringstream output;
        	output << "Life: " << mShip->getLife();
        	const string text = output.str();
        	const VGCVector position(0, 0);
        	const VGCAdjustment adjustment(0.0, 0.0);
        	VGCDisplay::renderString(mFont, text, fontColor, position, adjustment);
        }
        
        void Invaders::renderScore() {
        	//...
        }
        void Invaders::killDeadEntities() {
        	EntityVector entities;
        	for (EntityVector::iterator i = mEntities.begin(); i != mEntities.end();
        		i++) {
        		Entity *entity = *i;
        		if (entity->isAlive()) {
        			entities.push_back(entity);
        		}
        		else {
        			delete entity;
        		}
        	}
        	mEntities = entities;
        }
        void Invaders::killEdgeEntities() {
        	//...
        }
        bool Invaders::isOverlap(Entity *entity0, Entity *entity1) {
        	const VGCVector position0 = entity0->getPosition();
        	const int X0 = position0.getX();
        	const int Y0 = position0.getY();
        	const int R0 = entity0->getRadius();
        	const VGCVector position1 = entity1->getPosition();
        	const int X1 = position1.getX();
        	const int Y1 = position1.getY();
        	const int R1 = entity1->getRadius();
        	const int DX = X0 - X1;
        	const int DY = Y0 - Y1;
        	return DX * DX + DY * DY < (R0 + R1) * (R0 + R1);
        }
    Invaders.h
    Code:
    #include "VGCVirtualGameConsole.h"
        #include <vector>
        #include "Entity.h"
        
        class Invaders { 
        public:
        	typedef std::vector<Entity*> EntityVector;
        	Invaders();
        	~Invaders();
        	void run();
        private:
        	void spawnInvader();
        	void tick();
        	void detectCollisions();
        	void detectGameOver();
        	void renderBackground();
        	void renderForeground();
        	void renderLife();
        	void renderScore();
        	void killDeadEntities();
        	void killEdgeEntities();
        	static bool isOverlap(Entity *entity0, Entity *entity1);
        	VGCTimer mSpawnTimer;
        	VGCFont mFont;
        	Ship *mShip;
        	EntityVector mEntities;
        	int mScore;
        	bool mGameOver;
        
        };

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,995

    Re: C++ Cannot debug cause of errors, but no errors in code.

    Please, copy/paste the errors in your post (in CODE block to format them properly)
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: C++ Cannot debug cause of errors, but no errors in code.

    Code:
    Ship *mShip;
    Invaders.h uses Ship, but at this point in the code Ship isn't defined. Try

    Code:
    #include "Ship.h"
    at the top of Invaders.h. If Invaders.h uses Ship then you have a circular reference so then another solution is to forward declare Ship in Invaders.h as only a pointer to Ship is used.

    Also, it doesn't seem that you are using code guards in the header to prevent multiple includes of the same include file.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)