So I've been working on a puzzle game using a sprite system I used fine previously and in my past projects have been fine when extending the class, however my code keeps throwing an error of
error C2511:
'Door:: Door(float,float,float,float,float,float,int,CGame *)' :
overloaded member function not found in 'Door'
and I have no idea why as i've checked all the classes in my previous project and how they interface with the rest of the program and it's IDENTICAL but this new class is still erroring out. and the code in my main to create the object throws "no overloaded function in Door takes 8 arguments" out.
Door Class:
Door.h
#pragma once
#include "sprite.h"
class CGame;
class Door :
public CSprite
{
public:
Door(void);
Door(float, float, float, float,float, float, float, int, CGame * p_Game);
void Update(float dt);
~Door(void);
private:
};
Door.CPP
#include "stdafx.h"
#include "Door.h"
#include "Game.h"
Door::Door(void)
{
}
//So this is where i try to make door class extend sprite. but it keeps saying "overloaded member function not found in Door"
//and the other error is "doesnt take 8 args" and to top it off. It says unexpected end of file.
//Uncomment this block and the code in the door bit of the map gen to see what it is doing wrong
Door::Door(float _x, float _y, float _w, float _h, float _vX, float _vY, int _texID, CGame * p_Game) : CSprite(_x, _y, _w, _h, _vX, _vY, _texID, p_Game)
{
m_iType = 4 //sets the type of sprite that this object is.
}
void Door::Update(float dt)
{
}
Door::~Door(void)
{
}
and this is the sprite class i am extending (just the relevant parts)
Sprite.h
#pragma once
class CGame;
class CSprite
{
public:
float m_fX; //the position of the centre of the sprite
float m_fY;
float m_fW; //width of the sprite in arbitrary units
float m_fH; //height of the sprite in arbitrary units
float m_fvX;
float m_fvY;
int m_iTextureID; //which of the preloaded textures to use
float m_fR; //red component between 0 and 1
float m_fG; //green component between 0 and 1
float m_fB; //blue component between 0 and 1
float m_fA; //alpha value 0-1
int m_iType;
CGame * m_pGame;
public:
CSprite();
CSprite(float x, float y, float w, float h,float vX,float vY, int textureID ,CGame * p_Game);
bool bIsCollidingWith( CSprite * othersprite_);
bool markedForDelete;
//This new constructor is added to the Csprite.h header file.
float getX() { return m_fX; }
float getY() { return m_fY; }
virtual bool TagForDeletion();
virtual int GetSpriteType();
virtual ~CSprite();
virtual void Render();
virtual void Update(float dt);
};
Sprite.cpp
#include "StdAfx.h"
#include <gl.h>
#include <glut.h>
#include <glaux.h>
#include "main.h"
#include "sprite.h"
#include "Game.h"
CSprite::CSprite( )
{
m_fX=0.0f;
m_fY=0.0f;
m_fW=1.0f;
m_fH=1.0f;
m_fvX=0.0f;
m_fvY=0.0f;
markedForDelete=false;
m_fR=m_fG=m_fB=m_fA=1.0;
m_iTextureID=0;
}
CSprite::CSprite(float x_, float y_, float w_, float h_,float vX_,float vY_, int textureID_, CGame * p_Game)
{
m_iType = 1;
m_fX=x_;
m_fY=y_;
m_fW=w_;
m_fH=h_;
m_fvX=vX_;
m_fvY=vY_;
m_fR=m_fG=m_fB=m_fA=1.0;
m_iTextureID=textureID_;
m_pGame = p_Game;
markedForDelete=false;
}
CSprite::~CSprite(void)
{
}
Implementation in the game class uses the parameters extended from Sprite to create the object
p_Door[m_iSpritesLoaded++]=new Door(uiRow,4.58 -uiCol,1,1,0,0,4,this);
You miscounted your floats.
Declaration
Door(float, float, float, float,float, float, float, int, CGame * p_Game);
// 1 2 3 4 5 6 7
Definition
Door::Door(float _x, float _y, float _w, float _h, float _vX, float _vY, int _texID, CGame * p_Game)
// 1 2 3 4 5 6
: CSprite(_x, _y, _w, _h, _vX, _vY, _texID, p_Game)
Usage
p_Door[m_iSpritesLoaded++]=new Door(uiRow,4.58 -uiCol,1,1,0,0,4,this);
// 1 2 3 4 5 6
(Did you really need Stack Overflow for this?!)
Looks like your Door constructor has one extra float parameter. The declaration has 7 floats whereas the definition in Door.cpp has 6.
In Door.h the constructor has 9 arguments
Door(float, float, float, float,float, float, float, int, CGame * p_Game);
while in the Door.cpp the definition of the constructor has only 8 elements, that is one float is missing.
Solution:
add one argument to the definition of the constructor
CSprite::CSprite(float x_, float y_, float w_, float h_,float vX_,float vY_, int textureID_, CGame * p_Game)
Related
I wanted to move some code that I wrote, on my laptop, to my other PC. I didn't use Github or any other type of source control, I just simply copied the c++ files on to a USB drive and put them on my other PC.
And now I'm getting an error, for some reason, when I try to run the code?
The code is simply a class that uses direct2d to draw on a window.
This is what it looks like:
#pragma once
#include <Windows.h>
#include <d2d1.h>
#include <iostream>
#include "LinkedList.h"
class Graphics
{
private:
ID2D1Factory* pFactory;
ID2D1HwndRenderTarget* pRenderTarget;
ID2D1SolidColorBrush* pBrush;
RECT bounds;
float lineWidth = 5.0f;
LinkedList pointList;
public:
Graphics();
~Graphics();
void BeginDraw() { pRenderTarget->BeginDraw(); };
void EndDraw() { pRenderTarget->EndDraw(); };
void SetBrushColor(float r, float g, float b, float a);
void SetBrushColor(float r, float g, float b);
void SetLineWidth(float width);
RECT GetBounds();
void ClearScreen(float r, float g, float b, float a);
void ClearScreen(float r, float g, float b);
void FillCircle(float x, float y, float radius);
void DrawCircle(float x, float y, float radius);
void FillRect(float x, float y, float w, float h);
void DrawRect(float x, float y, float w, float h);
void MoveTo(float x, float y);
void LineTo(float x, float y);
void tester();
bool Init(HWND* pWindowHandle);
};
And the error I'm getting is this:
Error LNK2019 unresolved external symbol _D2D1CreateFactory#16 referenced in function "long __cdecl D2D1CreateFactory(enum D2D1_FACTORY_TYPE,struct _GUID const &,void * *)" (?D2D1CreateFactory##YAJW4D2D1_FACTORY_TYPE##ABU_GUID##PAPAX#Z) ffff c:\Users\sharkgaming\documents\visual studio 2015\Projects\ffff\ffff\Graphics.obj 1
At first I thought it was because I forgot to link to the d2d1.lib but even after doing that I'm still getting the error?
So, does anyone know why I'm getting the error and how to fix it?
Okay,
after googling around, for an hour or two, I found out that basically adding this:
#pragma comment(lib,"d2d1.lib")
would fix my problem.
But I have absolutely no idea why, since I already went in to the linker settings and added d2d1.lib to the additional library files?
EDIT: OMG I just figured it out.
I added the library to the x64 build settings not the x86. -_-
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
It appears as though I stumbled across something odd while trying to write my own wrapper for the freeglut api. Basically, I am writing my own little library to make using freeglut easier. One of the first things that I am doing is attempting to implement my own Color class which will be fed into "glClearColor". I am also having it so that you can enter the colors in manually; this means that I will have multiple static methods with the same name but different parameters/arguments. I tried to compile this afterwards but received an error that makes me think that the compiler cant decide which method to use—which is odd considering the two methods in question are still different. One takes a Color3 class and the other a Color4.
Here is some source:
GL.H
#pragma once
#include "Vector3.h"
#include "Color3.h"
#include "Color4.h"
#include <string>
class GL
{
public:
static void initializeGL(int argc, char* argv);
static void initializeDisplayMode(unsigned int displayMode);
static void initializeWindowSize(int width, int height);
static void createWindow(std::string title);
static void mainLoop();
static void translate(const Vector3 &location);
static void translate(float x, float y, float z);
static void rotate(double rotation, float x, float y, float z);
static void rotate(double rotation, const Vector3& axis);
static void color3(const Color3 &color);
static void color4(const Color4 &color);
static void begin();
static void end();
static void pushMatrix();
static void popMatrix();
static void enable(int enableCap);
static void viewport();
static void polygonMode();
static void matrixMode();
static void clearColor(const Color3 &color);
static void clearColor(float red, float green, float blue);
static void clearColor(float red, float green, float blue, float alpha);
static void clearColor(const Color4 &color);
static void vertex3(const Vector3 &location);
static void vertex3(float x, float y, float z);
static void loadIdentity();
static void perspective();
static void depthFunction();
};
GL.cpp
#include "GL.h"
#include "freeglut.h"
void GL::clearColor(const Color3 &color)
{
glClearColor(color.getRed,color.getGreen,color.getBlue, 1.0f);
}
void GL::clearColor(float red, float green, float blue)
{
glClearColor(red, green, blue, 1.0f);
}
void GL::clearColor(float red, float green, float blue, float alpha)
{
}
void GL::clearColor(const Color4 &color)
{
}
And here is my compiler error:
1>------ Build started: Project: GameEngineToolkit, Configuration: Debug Win32 ------
1> Main.cpp
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\main.cpp(47): error C2665: 'GL::clearColor' : none of the 4 overloads could convert all the argument types
1> c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.h(610): could be 'void GL::clearColor(const Color4 &)'
1> c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.h(607): or 'void GL::clearColor(const Color3 &)'
1> while trying to match the argument list '(Color3 *)'
1> GL.cpp
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.cpp(8): error C3867: 'Color3::getRed': function call missing argument list; use '&Color3::getRed' to create a pointer to member
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.cpp(8): error C3867: 'Color3::getGreen': function call missing argument list; use '&Color3::getGreen' to create a pointer to member
1>c:\the capsule\c++\get\gameenginetoolkit\gameenginetoolkit\gl.cpp(8): error C3867: 'Color3::getBlue': function call missing argument list; use '&Color3::getBlue' to create a pointer to member
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
As you can see, it seems that the compiler cant decide between using the Color3 function or the color4 function; I don't understand why because it should be obvious which one to choose(the Color3 one is the one I am using in my main).
As per request, here is my Color3 class:
Color3.h
#pragma once
class Color3
{
public:
Color3();
Color3(float red, float green, float blue);
void setRed(float red);
void setGreen(float green);
void setBlue(float blue);
float getRed();
float getGreen();
float getBlue();
Color3 getColor();
~Color3();
private:
float red;
float green;
float blue;
};
Color3.cpp
#include "Color3.h"
Color3::Color3()
{
}
Color3::Color3(float red, float green, float blue)
{
setRed(red);
setGreen(green);
setBlue(blue);
}
Color3::~Color3()
{
}
float Color3::getRed()
{
return red;
}
float Color3::getGreen()
{
return green;
}
float Color3::getBlue()
{
return blue;
}
void Color3::setBlue(float blue)
{
this->blue = blue;
}
void Color3::setGreen(float green)
{
this->green = green;
}
void Color3::setRed(float red)
{
this->red = red;
}
Color3 Color3::getColor()
{
return *this;
}
The Solution:
Use pointers.
GL.cpp
#include "GL.h"
#include "freeglut.h"
void GL::clearColor(Color3* color)
{
glClearColor(color->getRed(),color->getGreen(),color->getBlue(), 1.0f);
}
void GL::clearColor(float red, float green, float blue)
{
glClearColor(red, green, blue, 1.0f);
}
void GL::clearColor(float red, float green, float blue, float alpha)
{
}
void GL::clearColor(Color4* color)
{
}
GL.H
#pragma once
#include "Vector3.h"
#include "Color3.h"
#include "Color4.h"
#include <string>
class GL
{
public:
static void initializeGL(int argc, char* argv);
static void initializeDisplayMode(unsigned int displayMode);
static void initializeWindowSize(int width, int height);
static void createWindow(std::string title);
static void mainLoop();
static void translate(const Vector3 &location);
static void translate(float x, float y, float z);
static void rotate(double rotation, float x, float y, float z);
static void rotate(double rotation, const Vector3& axis);
static void color3(const Color3 &color);
static void color4(const Color4 &color);
static void begin();
static void end();
static void pushMatrix();
static void popMatrix();
static void enable(int enableCap);
static void viewport();
static void polygonMode();
static void matrixMode();
static void clearColor(Color3* color); // Use pointers instead
static void clearColor(float red, float green, float blue);
static void clearColor(float red, float green, float blue, float alpha);
static void clearColor(Color4* color); // Same thing; no error. =P
static void vertex3(const Vector3 &location);
static void vertex3(float x, float y, float z);
static void loadIdentity();
static void perspective();
static void depthFunction();
};
Well first of all, you're passing a Color3 pointer into an overloaded function that takes two different references.
You have a range of options:
Don't pass a pointer (you have Color3 color in main(), don't pass &color, pass color)
De-reference the pointer to pass a reference (having a Color3* color in main(), pass *color not color)
Change the method or add one to accept a Color3 pointer. This is dumb and I don't advise it. But you can!
Also I know it's not a part of the question but it appears getRed, getGreen, and getBlue are methods that you should append () to.
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.
#include <iostream>
#include <cmath>
using namespace std;
class Tcirculo{
float radio;
float diametro;
float area;
public:
void carea(float r){radio= r; area=(M_PI*((r*r)));}
float cdiam(float r) {diametro = 2*r; return diametro;}
float getr(){return radio;}
float getd(){return diametro;}
float geta(){return area;}
};
class Trectangulo : public Tcirculo{
float altura;
public:
float calca(float h, float r){altura =h;
float arearec = getd() * h; return arearec;}
};
class Tcilindro : public Tcirculo ,Trectangulo{
float xx,bb;
public:
Tcilindro(float a, float b) {xx=a;bb=b;}
float area_total();
};
float Tcilindro::area_total(){
int area;
area = ((2*((getd())))+calca(bb,xx));
return area;
}
int main(int argc, char *argv[]) {
return 0;
}
but the problem is :
warning: direct base 'Tcirculo' inaccessible in 'Tcilindro' due to ambiguity
In member function 'float Tcilindro::area_total()':
error: reference to 'geta' is ambiguous
error: candidates are: float Tcirculo::geta()
error: float Tcirculo::geta()
error: reference to 'geta' is ambiguous
error: candidates are: float Tcirculo::geta()
error: float Tcirculo::geta()
There is no need to derive Tcilindro from Tcirculo, it is sufficient if you derive it from Trectangulo.
These problems because of multiply inheritance with same Base Class. In you example class Tcilindro inherits from Trectangulo and Tcirculo but Trectangulo already derived from Tcirculo and Tcilindro have double definition of same functions. You just need to omit Tcirculo class here to remove ambiguity of inherited functions.
I'm still kind of new to C++ and don't know why I'm getting these linker errors while trying trying to call these functions in another class.
The errors are:
error LNK2019: unresolved external symbol "public: float __thiscall Star::getMass(void)" (?getMass#Star##QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update#Projectile##QAEXQAVStar##H#Z)
error LNK2019: unresolved external symbol "public: float __thiscall Star::getX(void)" (?getX#Star##QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update#Projectile##QAEXQAVStar##H#Z)
error LNK2019: unresolved external symbol "public: float __thiscall Star::getY(void)" (?getY#Star##QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update#Projectile##QAEXQAVStar##H#Z)
Projectile.cpp:
#include <hge.h>
#include "Projectile.h"
#include "Physics.h"
#include "Star.h"
#include <math.h>
Projectile::Projectile(float xV, float yV, float x, float y, float m, HTEXTURE tex)
{
xVel = xV;
yVel = yV;
xPos = x;
yPos = y;
mass = m;
quad.tex = tex;
}
void Projectile::Update(Star stars[], int length)
{
for(int i = 0; i<length; ++i)
{
float force = Physics::calcGravityForce(mass, stars[i].getMass(), Physics::calcDist(xPos, yPos, stars[i].getX(), stars[i].getY()));
Accelerate(force, stars[i].getX() - xPos, stars[i].getY() - yPos);
}
}
void Projectile::Accelerate(float force, float x, float y)
{
float c = sqrt((x * x) + (y * y));
xVel += x/c;
yVel += y/c;
}
Star is defined in Star.h here:
#ifndef STAR_H
#define STAR_H
#include <hge.h>
class Star
{
private:
float mass, radius, x, y;
hgeQuad quad;
public:
Star(float m, float r, float X, float Y, HTEXTURE);
float getMass();
float getRadius();
float getX();
float getY();
Star() {}
};
#endif
You have several functions declared in the Star class:
Star(float m, float r, float X, float Y, HTEXTURE);
float getMass();
float getRadius();
float getX();
float getY();
And you are trying to use some of them without providing a definition, that is to say, the body of the function, which is why you're getting those linker errors.
Add a new .cpp file to your project named Star.cpp (the name doesn't matter though) and add the definitions of the functions for the Star class, like you've done for the Projectile class. (You could just add them to any .cpp file in your project, like Projectile.cpp, but if you have a separate header file, it's good to have a seperate .cpp file too.)
Or if you don't want to have another cpp file in your project, you can put the bodies of the functions inside the class itself:
class Star
{
private:
float mass, radius, x, y;
hgeQuad quad;
public:
Star(float m, float r, float X, float Y, HTEXTURE);
float getMass() { return mass; }
float getRadius() { return radius; }
float getX() { return x; }
float getY() { return y; }
Star() {}
};
That style is common for small "getter" functions like getMass, getRadius, etc. which just return a member variable.
Though it's not directly related to your question, I should point out a few things:
Make all your "getter" functions (like getMass etc) const (so that they can be used on const Star objects) by putting the word const after the parameters (the () in this case) like this: float getMass() const { return mass; }
Because you have member variables in the Star class, you should set them to some sensible default value in the constructor which takes no parameters,
like this:
Star() : mass(0), radius(0), x(0), y(0) {}
Which will set mass, radius, x and y to 0. (This unusual syntax is called an initialiser list. You can read about them here.)
You can even do this without a seperate constructor by using default arguments:
Star(float m = 0, float r = 0, float X = 0, float Y = 0, HTEXTURE = 0);