cannot convert 'ArithProgression*' to 'Progression*' in assignment - c++

I had defined a class "Progression" and saved it as "Progression.h" and then i made another class "ArithProgression" which extends Progression class and saved it as "ArithProgression.h".
File: Progression.h
#ifndef PROGRESSION_H
#define PROGRESSION_H
#include <iostream>
using namespace std;
class Progression
{
public:
Progression()
{
cur=first=0;
}
Progression(long f)
{
cur=first = f;
}
void printProgression(int n)
{
cout<<firstValue();
for(int i=0;i<=n; i++)
{
cout<<' '<<nextValue();
}
}
virtual ~Progression() {}
protected:
long first;
long cur;
virtual long firstValue()
{
cur= first;
return cur;
}
virtual long nextValue()
{
return cur++;
}
};
#endif // PROGRESSION_H
FILE: ArithProgression.h
#ifndef ARITHPROGRESSION _H
#define ARITHPROGRESSION _H
#include "Progression.h"
class ArithProgression :public Progression
{
public:
ArithProgression(long i=1)
:Progression()
{
inc=i;
}
virtual ~ArithProgression () {}
protected:
long inc;
virtual long nextValue()
{
cur+=inc;
return cur;
}
private:
};
#endif // ARITHPROGRESSION _H
FILE: main.cpp
#include <iostream>
#include "Progression.h"
#include "ArithProgression.h"
using namespace std;
int main()
{
Progression* p;
p= new ArithProgression();
p->printProgression(10);
delete p;
}
I am getting an error: "cannot convert 'ArithProgression*' to 'Progression*' in assignment" in code blocks 12.11
Please Help

Your code, as you posted it, works just fine. Since ArithProgression is indeed a subclass of Progression, the above code cannot trigger that error.

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)

unable to seperate header classes. I get "does not name a type"

I made a class with a header and a cpp and a main class. When doing so all is good! When separating a class that I will have 2 classes (header+cpp) and one main Class A (Board) does not recognize class B (IllegalCoordinateException) even though I inserted the include. This is probably a newbies question and I might lose some points, but I am stuck in finding my problem.
Here is my working code(stripped down to only the important parts):
main.cpp
#include "Board.h"
#include <iostream>
using namespace std;
int main() {
Board board1{4}; // Initializes a 4x4 board
try {
board1[{3,4}]='O'; // This should raise an exception
} catch (const IllegalCoordinateException& ex) {
cout << "Illegal coordinate" << ex.theCoordinate() << endl; // prints "Illegal coordinate: 3,4"
}
return 0;
}
Board.h
#ifndef CIRC_H
#define CIRC_H
#include <iostream>
#include <string>
using namespace std;
struct coord {
int x;
int y;
};
class IllegalCoordinateException{
coord _coord;
public:
IllegalCoordinateException(coord c){
_coord = c;
}
string theCoordinate() const{
return to_string(_coord.x)+","+to_string(_coord.y);
}
};
class xo{
char x;
public:
char getChar() const{return x;}
char& operator= (const char c){x = c;}
xo& operator= (const xo _xo){
x = _xo.getChar();
return *this;
}
void clear(){
x = '.';
}
operator char() const{
return x;
}
};
class Board{
private:
coord _coord;
xo** board;
int size;
public:
Board();
Board(int v);
~Board();
xo& operator[](coord c); // here is where I use "IllegalCoordinateException"
};
#endif
Board.cpp
#include <iostream>
#include <vector>
#include "Board.h"
using namespace std;
void freeBoard(xo** board,int size){
for(int i = 0 ; i < size ; i++){
delete[] board[i];
}
}
Board::Board()
{
size = 0;
board = new xo* [size];
}
Board::Board(int v)
{
size = v;
board = new xo* [size];
for (int i=0; i<size; i++)
{
board[i] = new xo[size];
for(int j = 0 ; j < size ; j++){
board[i][j].clear();
}
}
}
Board::~Board(){
freeBoard(board,size);
delete[] board;
}
xo& Board::operator[](coord c)
{
if(c.x < size && c.y < size)
{
return board[c.x][c.y];
}
else
{
throw IllegalCoordinateException(c);
}
}
After seperation:
main.cpp
No diff
Board.h also cpp no diff
#ifndef CIRC_H
#define CIRC_H
#include "IllegalCoordinateException.h"
#include <iostream>
#include <string>
using namespace std;
struct coord {
int x;
int y;
};
class xo{
char x;
public:
char getChar() const{return x;}
char& operator= (const char c){x = c;}
xo& operator= (const xo _xo){
x = _xo.getChar();
return *this;
}
void clear(){
x = '.';
}
operator char() const{
return x;
}
};
class Board{
private:
coord _coord;
xo** board;
int size;
public:
Board();
Board(int v);
~Board();
xo& operator[](coord c);
};
#endif
IllegalCoordinateException.h // I also seperated in my code to .h and .cpp (but ofcourse there is no real diff.
#ifndef CIRC_H
#define CIRC_H
#include <iostream>
#include "Board.h"
using namespace std;
class IllegalCoordinateException{
coord _coord;
public:
IllegalCoordinateException(coord c){ _coord = c;}
string theCoordinate() const{return to_string(_coord.x)+","+to_string(_coord.y);}
};
#endif
When doing
$ g++ -g -Og -std=c++0x main.cpp Board.cpp IllegalCoordinateException.cpp
I get:
Board.cpp: In member function ‘xo& Board::operator’:
Board.cpp:60:43: error: ‘IllegalCoordinateException’ was not declared
in this scope
throw IllegalCoordinateException(c);
How can this be? I mean I am including it in the Board.h so Board.cpp is supposed to recognize it!? I tried also to include it in Board.cpp and also make a forward declaration in Board.cpp but both were frugal.
Both of your header files have #ifndef CIRC_H / #define CIRC_H.
So when the first one is included (no matter which order), it defines CIRC_H, and when the second one is included, it gets ignored because the whole file is within #ifndef CIRC_H.
Solution: use a different macro name for each header file.

Calling a method from one class in another class

I have this class:
boer.h
#pragma once
#include <functional>
#include <iostream>
class boer
{
private:
std::function<void(int id_)> someFun;
public:
boer();
~boer();
void setSomeFun(std::function<void(int id_)> someFun_);
void getSomeFun();
};
boer.cpp
#include "boer.h"
boer::boer() { }
boer::~boer() { }
void boer::setSomeFun(std::function<void(int id_)> someFun_)
{
someFun = someFun_;
}
void boer::getSomeFun()
{
someFun(12345);
}
And this class:
aircraft.h
#pragma once
#include <functional>
#include <iostream>
#include "boer.h"
class aircraft
{
private:
boer Boer;
public:
aircraft();
~aircraft();
void source_forSomeFun(int id_);
};
aircraft.cpp
#include "aircraft.h"
aircraft::aircraft() { }
aircraft::~aircraft() { }
void aircraft::source_forSomeFun(int lol_)
{
std::cout << "AMAZING!!!" << std::endl;
}
And I need to connect void source_forSomeFun(int id_); in aicraft with std::function<void(int id_)> someFun; in boer. How can I do this? Maybe there is another way, but i think this method is the most preferable.
int main()
{
aircraft Aircraft;
boer Boer;
Boer.setSomeFun(???); // here
Boer.getSomeFun();
int i;
std::cin >> i;
return 0;
}
Boer.setSomeFun([&](int v){aircraft.source_forSomeFun(v);});
Use a lambda.

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
}

How exactly serialization works in C++?

Here are my code files:
main.cpp
#include <iostream>
#include <fstream>
#include "Car.h"
#include "Engine.h"
using namespace std;
int main() {
Car* car = new Car(1984);
/* do something here */
delete car;
return 0;
}
Car.h
#ifndef CAR_H
#define CAR_H
#include <iostream>
using namespace std;
#include "Engine.h"
class Car {
public:
Car(int);
virtual ~Car();
void serialize(ostream& s) {
engine.serialize(s);
s << ' ' << yearModel;
}
void unserialize(istream& s) {
engine.unserialize(s);
s >> yearModel;
}
private:
Engine engine;
int yearModel;
};
#endif /* CAR_H */
Car.cpp
#include "Car.h"
Car::Car(int year) {
yearModel = year;
}
Car::~Car() {
}
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
#include <iostream>
using namespace std;
class Engine {
public:
Engine();
virtual ~Engine();
void serialize(ostream& s) {
s << ' ' << engineType;
}
void unserialize(istream& s) {
s >> engineType;
}
private:
int engineType;
};
#endif /* ENGINE_H */
Engine.cpp
#include "Engine.h"
Engine::Engine() {
engineType = 1;
}
Engine::~Engine() {
}
What I want to do in the main.cpp is to save the created Car object to file.txt and later read it from there. How does that exactly work? For example: how do I call the serialization function in Car class?
I'm sorry if I sound like a noob, but this whole serialization thing is pretty new to me.
Edit: Code compiles now when I added 'void' in front of all serialize- and unserialize-functions.
This has nothing to do with serialization: a function needs a return type, even if it is void. So this is wrong:
serialize(ostream& s) // look, no return type.
You probably need to either return void,
void serialize(ostream& s) { /* code as before */ }
or return the stream by reference to allow for chaining:
ostream& serialize(ostream& s) {
return s << ' ' << engineType;
}