#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.
Related
I'm initiating to C++ and I'm struggling with a compiling problem
I have a source file "binomial.cpp" in which I define the methods of my classes :
#include "binomial.hpp"
using namespace std;
int Bernoulli::operator()(){
return (rand() < p*RAND_MAX) ? a : b;
};
int Binomial::operator()(){
int result(0);
for(int i(0);i<n;++i){
int a;
a = B();
result += a;
};
return result;
};
and a header file "binomial.hpp" where I declare all my classes :
#include <iostream>
#ifndef BINOMIAL
#define BINOMIAL
class RandVar {
virtual int operator()() =0;
};
struct Bernoulli : public RandVar {
Bernoulli(int a = -1,int b = 1, double p = 0.5) : a(a), b(b), p(p) {};
int operator()(){};
private:
int a,b;
double p;
};
class Binomial : public RandVar {
public:
Binomial(Bernoulli B, int n=2)
: B(B), n(n) {}
int operator()(){};
private:
Bernoulli B;
int n;
};
#endif
But when I try to compile that through g++ using the command : g++ -Wall binomial.cpp -o binomial those errors occur :
binomial.hpp: In member function ‘virtual int Bernoulli::operator()()’:
binomial.hpp:14:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.hpp: In member function ‘virtual int Binomial::operator()()’:
binomial.hpp:26:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.cpp: At global scope:
binomial.cpp:4:5: error: redefinition of ‘int Bernoulli::operator()()’
int Bernoulli::operator()(){
^~~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:14:6: note: ‘virtual int Bernoulli::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
binomial.cpp:8:5: error: redefinition of ‘int Binomial::operator()()’
int Binomial::operator()(){
^~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:26:6: note: ‘virtual int Binomial::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
I don't really know how to fix that so if someone can take some time to help a beginner it would be great !
You should replace both
int operator()(){};
with
int operator()();
in the header files. You meant to provide declarations, not definitions. To provide just a declaration (not provide the code right away), just drop the {}.
I have been attempting to make some code, but I am a bit new to c++ and need some help.
I cannot instantiate class Player as a pointer, because it's an "incomplete type" (or undefined type, vs says both). Below are some (simplified, albeit not very) versions of my code:
Entity.h
#pragma once
#include <vector>
class Entity
{
public:
static void init();
class EntityObject;
class Player;
static std::vector<EntityObject*> entities;
};
Entity.cpp
#include "Entity.h"
void Entity::init()
{
entities = std::vector<EntityObject*>();
}
class Entity::EntityObject
{
private:
float velX, velY, x, y;
public:
EntityObject(float xa, float ya) { x = xa; y = ya; }
float getVelX() { return velX; }
float getVelY() { return velY; }
float getX() { return x; }
float getY() { return y; }
};
class Entity::Player : EntityObject
{
public:
Player(float xa, float ya) : EntityObject(xa, ya)
{
printf("Player created");
}
};
Can anyone tell me why
#include "Entity.h"
int main(int argc, char* argv)
{
Entity::init();
Entity::EntityObject* player = new Entity::Player(10.0f, 10.0f);
Entity::entities.push_back(player);
}
gives an incomplete/undefined type?
Thanks.
Edit:
The errors are:
Both errors direct to this line: Entity::EntityObject* player = new Entity::Player(10.0f, 10.0f);
Error (active) E0070 incomplete type is not allowed
Error C2027 use of undefined type 'Entity::Player'
You defined the Entity::Player class in the .cpp file, not in the .h file. Therefore, even though the main() includes the .h file, it does not know about Entity::Player.
Entity::Player is forward declared in Entity.h.
When the compiler compiles your main.cpp module, it does not know anything about this class except that it exists, in particular it has no idea that this class as a constructor taking two float : Player(float xa, float ya)
=> Your issue is related to forward declaring, not nested class.
Read this thread to understand your problem
What are forward declarations in C++?
Read this one to understand what you can and what you can't do with forward declaration
When can I use a forward declaration?
I am a newbie in c++ and I am trying to modify a code and use gsl_integration library of c in a class called Cosmology. In order to assign member functions to to form a pointer for gsl, I used callback procedure I found by looking up in internet
Update: Cosmology.h
#include <cmath>
#include <gsl/gsl_integration.h>
struct CCallbackHolder
{
Cosmology* cls;
void* data;
};
class Cosmology {
private:
static const double c = 299792458.0, Mpc2Km = 3.08567758e+19, Yrs2Sec = 3.15569e7;
double H0 = 67.77, OmegaM = (0.022161+0.11889)/(H0*H0), OmegaL = 0.6914, OmegaG = 8.24e-5, OmegaK = 0.0009;
double Ez(double z);
double Hz(double z, void* params);
static double CCallback(double z,void* param)
{
CCallbackHolder* h = static_cast<CCallbackHolder*>(param);
return h->cls->Hz(h->data);
}
public:
double distH, timeH;
Cosmology();
Cosmology(double);
Cosmology(double , double );
Cosmology(double , double , double );
Cosmology(double , double , double , double );
Cosmology(double , double , double , double , double );
double distC(double);
} cosmo;
Cosmology.cpp
#include <cmath>
#include <gsl/gsl_integration.h>
#include "Cosmology.h"
#include<iostream>
using namespace std;
double Cosmology::Hz(double z, void* params) {
double result = 1.0/pow(OmegaL + pow(1.0+z,3.0)*OmegaM + pow(1.0+z,4.0)*OmegaG + pow(1.0+z,2.0)*OmegaK, 0.5);
return result;
}
double Cosmology::distC(double z) {
double lower_limit = 0.0, abs_error = 1.0e-8, rel_error = 1.0e-8, alpha = 0.0, result, error;
gsl_integration_workspace *work_ptr = gsl_integration_workspace_alloc(1000);
gsl_function Hz_function;
void* params_ptr = α
Hz_function.function = &Cosmology::CCallback;
Hz_function.params = params_ptr;
gsl_integration_qags(&Hz_function, lower_limit, z, abs_error, rel_error, 1000, work_ptr, &result, &error);
return distH*result;
}
using namespace std;
int main () {
Cosmology cosmo;
cout << "Comoving Distance: " << cosmo.distC (0.3);
return 0;
}
I am getting the following errors when I tried to compile the code:
Cosmology.h:10: error: ISO C++ forbids declaration of ‘Cosmology’ with no type
Cosmology.h:10: error: expected ‘;’ before ‘*’ token
Cosmology.h:16: error: ISO C++ forbids declaration of ‘constexpr’ with no type
Cosmology.h:16: error: expected ‘;’ before ‘double’
Cosmology.h:17: error: ISO C++ forbids declaration of ‘constexpr’ with no type
Cosmology.h:17: error: expected ‘;’ before ‘double’
In file included from Universe.cpp:3:
Cosmology.h: In static member function ‘static double Cosmology::CCallback(double, void*)’:
by compiling with this line : g++ -Wall -pedantic Cosmology.cpp -o Cosmology std=c++0x. How could I fix the code?
The main problem with your program is that you initialize non-static variables that are class members while declaring them, which is not a legal C++ operation. To be specific, the problem are these lines:
static constexpr double c = 299792458.0, Mpc2Km = 3.08567758e+19, Yrs2Sec = 3.15569e7;
double H0 = 67.77, OmegaM = (0.022161+0.11889)/(H0*H0), OmegaL = 0.6914, OmegaG = 8.24e-5, OmegaK = 0.0009;
c, Mpc2Km, Yrs2Sec, H0, OmegaM, OmegaL, OmegaG, OmegaK are non-static, non-const and double. If you want to initialize them (i.e. assign values to them), you have to do it within the Cosmology class constructor or move them outside of the class declaration and thus make them global, but note that you should avoid global variables unless you know what you do and there's no other way.
If you want to initialize them in the header file like you tried to do, they must be of integral type - won't work for doubles.
Shortly: the first line won't work because the type is double (only integral static const types can be initialized inside class declaration); the second won't work since the type is non-const, non-static and double.
The rest of the errors will probably vanish as soon as these lines are fixed.
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.
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)