error is - "first defined here" in simple Hierarchy classes - c++

Im new in cpp, and tried to google the problame with no luck solving it, could use help.
Oh, and my files are all in the same folder.
the Rectangle.h file:
#ifndef RECTANGLE_H_
#define RECTANGLE_H_
#include "Polygon.h"
class Rectangle: public Polygon{
public:
Rectangle(int nColor);
Rectangle(Rectangle& r);
virtual string getType();
int getArea();
};
#endif /* RECTANGLE_H_ */
The errors are here, under each method signature it sais:
"first defined here"
the Rectangle.cpp file:
#include "Rectangle.h"
#include "Polygon.h"
//error is - "first defined here"
Rectangle::Rectangle(int color):Polygon(color, 3, "Rectangle"){
}
//error is - "first defined here"
Rectangle::Rectangle(Rectangle& r) :
Polygon(r) {
}
//error is - "first defined here"
string Rectangle::getType() {
return "Rectangle";
}
//error is - "first defined here"
int Rectangle::getArea() {
return 0;
}
I am also adding the father class "Polygon.h" but im pretty sure it's not relevant...
the only interesting thing about it is the method:
virtual string getType();
which is not abstract. just virtual
Polygon h:
#ifndef __POLYGON_H
#define __POLYGON_H
#include "Point.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Polygon {
public:
Polygon(int nColor , int nPoints , std::string type);
Polygon(const Polygon &polyOther);
virtual ~Polygon() = 0;
int getColor();
virtual string getType();
void addPoint(Point* p);
Point* getPoint(int index) const;
int getNumOfPoints() const;
Polygon& operator=(const Polygon &polyOther);
private:
std::vector<Point*> _points;
const int _color;
string _type;
};
#endif
thanks for the help....

Related

C++ Class inheritance in different files

I'm trying to learn Inheritance mechanism in C++, I have made a Bancnote(Bills) class, and I want to make a class Card inheriting all the functions and variables from Class Bancnote.
And I get this type of error :
include\Card.h|6|error: expected class-name before '{' token|
BANCNOTE.H
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include <iostream>
#include "Card.h"
using namespace std;
class Bancnote
{
public:
Bancnote();
Bancnote(string, int ,int ,int );
~Bancnote( );
int getsumacash( );
void setsumacash( int );
int getsumaplata( );
void setsumaplata( int );
int getrest( );
void setrest( int );
string getnume( );
void setnume( string );
void ToString();
protected:
private:
string nume;
int sumacash;
int rest;
static int sumaplata;
};
#endif // BANCNOTE_H
BANCNOTE.CPP
#include <iostream>
#include "Bancnote.h"
#include "Card.h"
using namespace std;
int Bancnote::sumaplata=0;
Bancnote::Bancnote(string _nume,int _sumacash,int _rest, int _sumaplata )
{
this->nume=_nume;
this->sumacash=_sumacash;
this->rest=_rest;
this->sumaplata=_sumaplata;
}
Bancnote::Bancnote()
{
this->nume="";
this->sumacash=0;
this->rest=0;
this->sumaplata=0;
}
Bancnote::~Bancnote()
{
cout<<"Obiectul"<<"->" <<this->nume<<"<-"<<"a fost sters cu succes";
}
string Bancnote::getnume()
{
return nume;
}
void Bancnote::setnume(string _nume)
{
this->nume=_nume;
}
int Bancnote::getsumacash()
{
return sumacash;
}
void Bancnote::setsumacash(int _sumacash)
{
this->sumacash=_sumacash;
}
int Bancnote::getsumaplata()
{
return sumaplata;
}
void Bancnote::setsumaplata(int _sumaplata)
{
this->sumaplata=_sumaplata;
}
int Bancnote::getrest()
{
return rest;
}
void Bancnote::setrest(int _rest)
{
this->rest=_rest;
}
void Bancnote::ToString()
{
cout<< "-----"<<getnume()<< "-----"<<endl;
cout<<"Suma Cash: "<<this->getsumacash()<<endl;
cout<<"Suma spre plata: "<<this->getsumaplata()<<endl;
cout<<"Restul:"<<this->getrest()<<endl;
}
CARD.H
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
class Card: public Bancnote
{
public:
Card();
virtual ~Card();
protected:
private:
};
#endif // CARD_H
You have messed up the includes. What you have is more or less this:
Bancnote.h:
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include "Card.h" // remove this
struct Bancnote {};
#endif
Card.h
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
struct Card : Bancnote {}; // Bancnote is not yet declared
// when compiler reaches here
#endif
When in main you include Bancnote.h then this header includes Card.h so you try to declare Card before Bancnote is declared. Actually Bancnote does not need the definition of Card, so simply removing the include should fix it.
PS: there are other issues (see comments below your question). Most importantly it is not clear why a Card is a Bancnote. Second, never put a using namespace std; inside a header! (see here why)

Debugging C++ compiler error

I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ‘)’ before ‘n’
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.

c++ Inheritance in different files error

For some reason I can't manage to figure out what is wrong with my inheritance in c++ within different files. The biggest error I get is 'no matching function for call to 'Enemy::Enemy (int&)'
Here is my Monster.cpp code
#include "Monster.h"
#include "Enemy.h"
#include <iostream>
Monster::Monster(int MonsterHealth,int MonsterMana,int Monstersize) // implementation
: Health(MonsterHealth), Mana(MonsterMana), Enemy(Monstersize)
{}
int Monster::dropxp(int enemydropxp){
}
Here is my Monster.h
#ifndef MONSTER_H
#define MONSTER_H
#include "Enemy.h"
class Monster : public Enemy
{
Monster();
Monster(int MonsterHealth, int MonsterMana,int Monstersize);
void TheenemyHealth()
{
int Enemyhealth = 100;
}
int EnemyDamage(int EnemyAttack){
int Attack = EnemyAttack;
Attack = 5;
}
int dropxp(int enemyxpdrop);
private:
int Health = 0;
int Mana = 0;
};
#endif // MONSTER_H
Here is my enemy.cpp
#include "Enemy.h"
Enemy::Enemy(int EnemyHealth,int EnemyMana)
{
Attackpower;
Strenght;
Enemyxp;
}
Enemy::~Enemy()
{
//dtor
}
and my enemy.h
#ifndef ENEMY_H
#define ENEMY_H
class Enemy
{
public:
Enemy(int EnemyHealth,int EnemyMana);
~Enemy();
virtual void TheenemyHealth(){}
virtual int EnemyDamage(int EnemyAttack){
int Attack = EnemyAttack;
}
virtual int dropxp(int enemyxpdrop);
private:
int Attackpower= 0;
int Strenght = 0;
int Enemyxp= 0;
};
#endif // ENEMY_H
Your enemy constructor is defined as Enemy(int EnemyHealth,int EnemyMana); but you are calling it with only one parameter in:
Monster::Monster(int MonsterHealth,int MonsterMana,int Monstersize) // implementation
: Health(MonsterHealth), Mana(MonsterMana), Enemy(Monstersize)
{}
Also your Enemy constructor does nothing:
Enemy::Enemy(int EnemyHealth,int EnemyMana)
{
Attackpower; // does nothing
Strenght; // does nothing
Enemyxp; // does nothing
}

data type inst converting correctly?

Ok so my .exe is giving me a zero so im guessing that the data type is not casting correctly. Sorry im new to c++ and came from c. Any time i had this problem in c something usually was truncated, but i cant find out what i did wrong.
//shape.h
#ifndef SHAPE_H
#define SHAPE_H
class shape
{
public:
shape();
virtual float area()=0;
};
#endif SHAPE_H
//shape.cpp
#include <iostream>
#include "shape.h"
using namespace std;
shape::shape()
{
}
//triangle.h
#include"shape.h"
class triangle: public shape
{
public:
triangle(float,float);
virtual float area();
protected:
float _height;
float _base;
};
//triangle.cpp
#include "triangle.h"
triangle::triangle(float base, float height)
{
base=_base;
height=_height;
}
float triangle::area()
{
return _base*_height*(1/2);
}
//main.cpp
#include <iostream>
#include "shape.h"
#include "triangle.h"
using namespace std;
int main()
{
triangle tri(4,2);
cout<<tri.area()<<endl;
return 0;
}
For some reason i am getting a zero in my exe when i should be getting a 4.
You assigned value in wrong way:
update:
triangle::triangle(float base, float height)
{
base=_base;
height=_height;
}
to:
triangle::triangle(float base, float height)
{
_base = base;
_height = height;
}
Edit:
Also as #WhozCraig mentions, should use float for 1/2, or just
_base * _height / 2.0

C++ - Error: expected unqualified-id before ‘using’

I have a C++ program and when I try to compile it it gives an error:
calor.h|6|error: expected unqualified-id before ‘using’|
Here's the header file for the calor class:
#ifndef _CALOR_
#define _CALOR_
#include "gradiente.h"
using namespace std;
class Calor : public Gradiente
{
public:
Calor();
Calor(int a);
~Calor();
int getTemp();
int getMinTemp();
void setTemp(int a);
void setMinTemp(int a);
void mostraSensor();
};
#endif
Why does this happen?
This class inherits from gradiente:
#ifndef _GRADIENTE_
#define _GRADIENTE_
#include "sensor.h"
using namespace std;
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
}
#endif
Which in turn inherits from sensor
#ifndef _SENSOR_
#define _SENSOR_
#include <iostream>
#include <fstream>
#include <string>
#include "definicoes.h"
using namespace std;
class Sensor
{
protected:
int tipo;
int IDsensor;
bool estadoAlerta;
bool estadoActivo;
static int numSensores;
public:
Sensor(/*PARAMETROS*/);
Sensor(ifstream &);
~Sensor();
int getIDsensor();
bool getEstadoAlerta();
bool getEstadoActivo();
void setEstadoAlerta(int a);
void setEstadoActivo(int a);
virtual void guardaSensor(ofstream &);
virtual void mostraSensor();
// FUNÇÃO COMUM
/* virtual int funcaoComum() = 0;
virtual int funcaoComum(){return 0;};*/
};
#endif
For completeness' sake, here's definicoes.h
#ifndef _DEFINICOES_
#define _DEFINICOES_
const unsigned int SENSOR_MOVIMENTO = 0;
const unsigned int SENSOR_SOM = 1;
const unsigned int SENSOR_PRESSAO = 2;
const unsigned int SENSOR_CALOR = 3;
const unsigned int SENSOR_CONTACTO = 4;
const unsigned int MIN_MOVIMENTO = 10;
const unsigned int MIN_SOM = 10;
const unsigned int MIN_PRESSAO = 10;
const unsigned int MIN_CALOR = 35;
#endif
What am I doing wrong?
There is a semicolon missing at the end of this class:
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
} // <-- semicolon needed after the right curly brace.
Also, the names of your include guards are illegal. Names that begin with an underscore and an uppercase letter are reserved for the C++ implementation (as are names containing a double underscore) - you are not allowed to create such names in your own code. And you should never use:
using namespace std;
in a header file. And lastly, the destructor in your Sensor base class should almost certainly be made virtual.
In gradiente.h you forgot the semicolon at the end of your class declaration.
You need this:
class Gradiente : public Sensor
{
protected:
int vActual, vMin;
public:
Gradiente();
~Gradiente();
};
See the added semicolon?
You forgot to leave the last semi colon on the closing brackets, };, on the gradiente class.