Upcasting in C++ : cannot convert child class pointer to parent class pointer - c++

I have a problem with my program. I made a Graph class in C++, and now I want to sort it topologically. The thing is, my topological sort accepts any DirectedGraph, but when I want to give it a child (AdjacencyListDirectedUnweightedGraph for example), it refuses to convert. Here is my .hpps:
TopoSort.hpp:
#ifndef TOPOSORT_HPP
#define TOPOSORT_HPP
#include "../Graph.hpp"
#include "../DirectedGraph/AdjListUWDG.hpp"
#include "../DirectedGraph/DirectedGraph.hpp"
#include "../UnDirectedGraph/AdjListWUDG.hpp"
class TopoSort
{
protected:
std::vector<int> _sortedList;
std::vector<int> _KahnTopNodes;
public:
TopoSort();
~TopoSort();
void KahnSort(DirectedGraph &list);
void KahnSortTopNodes(DirectedGraph &list);
};
#endif
DirectedGraph.hpp
#ifndef DIRECTEDGRAPH_HPP
#define DIRECTEDGRAPH_HPP
#include <iostream>
#include <string>
#include <vector>
#include "../Graph.hpp"
class DirectedGraph
: public Graph
{
protected:
std::vector<int> _inDegree;
std::vector<int> _outDegree;
public:
DirectedGraph(){};
virtual ~DirectedGraph(){};
int inDegree(int a){return (_inDegree[a]);}
int outDegree(int a){return (_outDegree[a]);}
bool rangeCheck(int a, int b)
{
if (a >= _vertices || b >= _vertices || a == b)
{
std::cout << "The edge " << a << " - " << b << " is invalid." << std::endl;
return (false);
}
return (true);
}
};
#endif
AdjListDG.hpp
#ifndef ADJListDG_HPP
#define ADJListDG_HPP
#include <string>
#include <vector>
#include "DirectedGraph.hpp"
class AdjListDG
: public virtual DirectedGraph
{
protected:
std::vector<std::vector<std::pair<int, int> > > _adjList;
public:
virtual ~AdjListDG();
bool existsEdge(Edge);
bool existsEdge(int, int);
void putEdge(Edge);
void removeEdge(Edge);
int adjacentVertices(int);
bool areAdjacent(int, int);
};
#endif
AdjListUWDG.hpp
#ifndef AdjListUWDG_HPP
#define AdjListUWDG_HPP
#include <string>
#include <vector>
#include "AdjListDG.hpp"
class AdjListUWDG
: public virtual AdjListDG
{
public:
AdjListUWDG(std::string);
virtual ~AdjListUWDG();
};
#endif
And, my main.
#include <iostream>
#include <string>
#include <fstream>
#include "UnDirectedGraph/AdjListWUDG.hpp"
#include "UnDirectedGraph/AdjListUWUDG.hpp"
#include "UnDirectedGraph/AdjMatWUDG.hpp"
#include "UnDirectedGraph/AdjMatUWUDG.hpp"
#include "Assgn3/TopoSort.hpp"
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage : ./graph FILENAME" << std::endl;
return(0);
}
std::string filename = argv[1];
AdjListWUDG gr(filename);
TopoSort tsort;
std::ofstream fichier("results.txt", std::ios::out | std::ios::trunc);
if(fichier)
{
if (gr.existsEdge(1, 2))
fichier << "1 - 2 exist" << std::endl;
fichier << "numedge == " << gr.numEdges() << std::endl;
fichier << "adjver 1 == " << gr.adjacentVertices(1) << std::endl;
fichier << "adj 1 2 == " << gr.areAdjacent(1,2) << std::endl;
fichier << "adj 1 0 == " << gr.areAdjacent(1,0) << std::endl;
fichier << "adj 0 2 == " << gr.areAdjacent(0,2) << std::endl;
}
DirectedGraph * gr2 = &gr;
tsort.KahnSort(*gr2);
}
That's it ! I'm sorry if it looks obvious or something, I just can't see what's the problem. Also tried dynamic and static casts, no success. thanks in advance !
EDIT :
I was stupid. tried to cast for a reference, instead of an object itself...
static_cast instead of static_cast
Sorry for that useless post !

Casting to a reference instead of the object itself, that was stupid, sorry !

Related

Why copy constructor changed previous object pointer member

#include <iostream>
#include <string.h>
#include "Date.h"
#include "Employee.h"
using std::cout;
using std::endl;
using std::to_string;
class TestOps {
public:
int sex = 1;
string toString() {
return " sex:" + to_string(sex) ;
}
};
class Test {
public:
TestOps* testOps;
Test(const Test& t) :Test{} {
this->testOps = new TestOps{ *(t.testOps) };
};
Test() {
TestOps ops;
//this->testOps = new TestOps{}; // it will be ok with this way
this->testOps = &ops;
}
};
int main() {
// code not understand
Test t1;
cout <<"first testOps:" << t1.testOps->toString() << endl; // sex: 1
Test t2{ t1 };
cout << "first testOps:" << t1.testOps->toString() << endl; // sex: -858893460 ???? why?
cout << "second testOps:" << t2.testOps->toString() << endl; // sex: -858893460 ???? why?
return 0;
}
As you can see, why the first log is as expected while the later logs are not?
Also, t1.testOps address is different from t2.testOps which is as expected.
I have done some research but didn't find the answer. Maybe because I'm pretty new to cpp.

Error occurs when I try to call an inherited function in C++

I have 3 classes, GameObject, Building which inherits from GameObject, and PokemonCenter which inherits from Building. When I try to call a Building::ShowStatus() function in PokemonCenter I keep get a "invalid operands to binary expression" error.
Building.h
#ifndef BUILDING_H
#define BUILDING_H
#include "Point2D.h"
#include "GameObject.h"
class Building : public GameObject
{
private:
unsigned int pokemon_count;
public:
Building();
Building(char,int, Point2D);
void AddOnePokemon();
void RemoveOnePokemon();
void ShowStatus();
bool ShouldBeVisible();
};
#endif
Building.cpp
#include "Building.h"
#include "GameObject.h"
#include <iostream>
using namespace std;
Building::Building()
{
display_code = 'B';
location;
id_num = ' ';
state = '0';
pokemon_count = 0;
cout << "Building default constructed";
}
Building::Building(char in_code,int in_id, Point2D in_loc)
{
id_num = in_id;
location = in_loc;
display_code = in_code;
state = '0';
pokemon_count = 0;
cout << "Building constructed";
}
void Building::ShowStatus()
{
cout << "\"(" << pokemon_count << "\"pokemon is/are in this building";
}
PokemonCenter.h
#ifndef POKEMONCENTER_H
#define POKEMONCENTER_H
#include "Point2D.h"
#include "Building.h"
class PokemonCenter: public Building
{
private:
unsigned int stamina_capacity;
unsigned int num_stamina_points_remaining;
double dollar_cost_per_stamina_point;
PokemonCenter();
PokemonCenter(int,double,unsigned int, Point2D);
public:
bool HasStaminaPoints();
unsigned int GetNumStaminaPointsRemaining();
bool CanAffordStaminaPoints(unsigned int, Point2D);
double GetDollarCost(unsigned int);
unsigned int DistributeStamina(unsigned int);
bool Update();
void ShowStatus();
};
enum PokemonCenterStates
{
STAMINA_POINTS_AVAILABLE = 0,
NO_STAMINA_POINTS_AVAILABLE = 1
};
#endif
PokemonCenter.cpp
#include "PokemonCenter.h"
#include "Point2D.h"
#include "Building.h"
#include "GameObject.h"
#include <iostream>
using namespace std;
PokemonCenter::PokemonCenter()
{
id_num = ' ';
location;
display_code = 'C';
stamina_capacity = 100;
num_stamina_points_remaining = stamina_capacity;
dollar_cost_per_stamina_point = 5;
state = STAMINA_POINTS_AVAILABLE;
cout << "PokemonCenter default constructed";
}
PokemonCenter::PokemonCenter(int in_id, double stamina_cost, unsigned int stamina_cap, Point2D in_loc)
{
id_num = in_id;
location = in_loc;
dollar_cost_per_stamina_point = stamina_cost;
stamina_capacity = stamina_cap;
num_stamina_points_remaining = stamina_capacity;
state = STAMINA_POINTS_AVAILABLE;
cout << "PokemonCenter constructed";
}
void PokemonCenter::ShowStatus()
{
cout << "Pokemon Center Status: " << Building::ShowStatus() << endl;
}
The problem is that the return type of Building::ShowStatus() is void. operator<< is not defined between std::ostream and void. Hence, you can't use
cout << "Pokemon Center Status: " << Building::ShowStatus() << endl;
Change that to:
cout << "Pokemon Center Status: ";
Building::ShowStatus();
cout << endl;

Requesting a function from another file in a class not working

I have 2 .cpp files called "FactoringProgram.cpp" and "FactoringProgram2nd.cpp", also 1 header file called "FactoringProgram.h". I've already tried searching for this problem on StackOverflow and a couple other sites and haven't found a solution that worked for me. When I try to run this command: "g++ FactoringProgram.cpp FactoringProgram2nd.cpp" I get this error:
FactoringProgram.cpp: In function ‘int main()’: FactoringProgram.cpp:8:11: error: request for member ‘InitialMessage’
in ‘Problem1’, which is of non-class type ‘Factoring()’
Problem1.InitialMessage();
The code for "FactoringProgram.h" is:
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
int InitialMessage();
};
#endif
FactoringProgram.cpp code:
#include <stdio.h>
#include <iostream>
#include "FactoringProgramH.h"
int main()
{
Factoring Problem1();
Problem1.InitialMessage();
return 0;
}
FactoringProgram2nd.cpp code:
#include "FactoringProgramH.h"
#include <stdio.h>
#include <iostream>
Factoring::Factoring(int FirstCoefficent=0, char FirstOperator='+',
int SecondCoefficent=1, char SecondOperator='+', int 3rdExpression=1)
: m_FirstCoefficent(FirstCoefficen), m_FirstOperator(FirstOperator),
m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),
m_3rdExpression(3rdExpression);
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< FirstCoefficent << "x^2 " << FirstOperator << " " << SecondCoefficent
<< " x" << SecondOperator << " " << 3rdExpression;
}
Your code has several small errors. Here is a version that works (you can compare and see what I had to change). A summary of the changes are:
default parameter values should be in the header file (*.h);
you cannot start variable/argument names with numbers;
int InitialMessage() doesn't return anything, so I changed it to void InitialMessage();
when you initialize fields in a constructor, the last field cannot end with a semi-colon;
fixed the most vexing parsing (per #RSahu comment) in main.cpp.
FactoringProgram.h
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring(int FirstCoefficent = 0, char FirstOperator = '+',
int SecondCoefficent = 1, char SecondOperator = '+', int thirdExpression = 1);
void InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
Factoring::Factoring(int firstCoefficent, char firstOperator, int SecondCoefficent, char SecondOperator, int thirdExpression) :
m_FirstCoefficent(firstCoefficent),
m_FirstOperator(firstOperator),
m_SecondCoefficent(SecondCoefficent),
m_SecondOperator(SecondOperator),
m_3rdExpression(thirdExpression)
{}
void Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
}
main.cpp
#include <stdio.h>
#include <iostream>
#include "FactoringProgram.h"
int main()
{
Factoring Problem1;
Problem1.InitialMessage();
return 0;
}
To this error substitute:
Factoring Problem1();
by:
Factoring Problem1 = Factoring();
The problem is that the compiler is interpreting this line as a function declaration rather than a variable declaration. AS mentioned in the comments, this is known as the most vexing parser problem.
Obs: The code you posted contain many more minor errors.
You should define Factoring() without params and I used headers in FactoringProgram.h
FactoringProgram.h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
Factoring(int,char,int,char,int);
int InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
int main()
{
Factoring Problem1 = Factoring();
Problem1.InitialMessage();
system("pause");
return 0;
}
FactoringProgram2nd.cpp
#include "FactoringProgram.h"
Factoring::Factoring()
{
*this = Factoring(0, '+', 1, '+', 1);
}
Factoring::Factoring(int FirstCoefficent = 0, char FirstOperator = '+',int SecondCoefficent = 1, char SecondOperator = '+', int _3rdExpression = 1) : m_FirstCoefficent(FirstCoefficent), m_FirstOperator(FirstOperator),m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),m_3rdExpression(_3rdExpression)
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
return 0;
}

c++ / scope of struct

In following code, I think that structure stSameNameButDifferent is local scope definition and so no problem for it. But I got error in run-time.
(error : process crash)
Can you explain what's wrong with that code?
test_function.h
#ifndef TEST_FUNC_H_
#define TEST_FUNC_H_
void test_a();
void test_b();
#endif
main.cpp
#include <iostream>
#include "test_function.h"
using namespace std;
int main(int argc, const char** argv)
{
cout << "testing for struct scope" << endl;
test_a();
test_b();
return 0;
}
test_a.cpp
#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"
struct stSameNameButDifferent
{
uint32_t nPlayCode;
uint32_t nGameID;
std::string sGameName;
};
void test_a()
{
std::list<stSameNameButDifferent> lstSt;
for(int i=0; i<10; ++i)
{
stSameNameButDifferent st;
st.nPlayCode = i;
st.nGameID = 100+i;
std::ostringstream osBuf;
osBuf << "Game_" << i;
st.sGameName = osBuf.str();
lstSt.push_back(st);
}
for(auto &st : lstSt)
{
std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
}
}
test_b.cpp
#include <iostream>
#include <sstream>
#include <cstdint>
#include <list>
#include "test_function.h"
struct stSameNameButDifferent
{
uint32_t nPlayCode;
uint32_t nGameID;
float fDiscountRate;
std::string sGameName;
};
void test_b()
{
std::list<stSameNameButDifferent> lstSt;
for(int i=0; i<10; ++i)
{
stSameNameButDifferent st;
st.nPlayCode = i;
st.nGameID = 1000+i;
st.fDiscountRate = (float)i/100;
std::ostringstream osBuf;
osBuf << "Game_" << i;
st.sGameName = osBuf.str();
lstSt.push_back(st);
}
for(auto &st : lstSt)
{
std::cout << st.nPlayCode << ", " << st.nGameID << ", " << st.sGameName << std::endl;
}
}
To avoid clashes of the same struct names in multiple translation units, you have to put them in an unnamed namespace like so:
namespace {
struct stSameNameButDifferent {
uint32_t nPlayCode;
uint32_t nGameID;
std::string sGameName;
};
}
This will make stSameNameButDifferent only seen privately in the corresponding translation unit (.cpp file).
Otherwise the linker will resolve the symbol with the first one found, hence the errors you see at runtime.
you have defined stSameNameButDifferent in global scope, so compiler cannot see and analyze both definitions to same struct and it only take the first one it meet and that's why you are getting an error.
You can use two different namespaces for test_a and test_b, so you will not get any error.

Run through a vector of member functions in boost for_each

I try to learn boost lambda expressions, and this is a thing that doesn't work out.
How can I run in the for_each a selected member of Holder?
#include <iostream>
#include <string>
#include <boost/assign.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
using namespace std;
using namespace boost::assign;
using namespace boost::lambda;
class Holder
{
public:
void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};
// --------------------
std::map< std::string, mem_fun_ref_t<void, Holder> > replacer;
insert(replacer)
("buh", std::mem_fun_ref(&Holder::vRun1))
("mar", std::mem_fun_ref(&Holder::vRun2))
("heu", std::mem_fun_ref(&Holder::vRun3));
Holder hol;
How do I call here the functions I have registered in the map<>?
for_each(replacer.begin(), replacer.end(), /* bind(_1, hol, it.first) */ );
The result should be
vRun1 buh
vRun2 mar
vRun3 heu
It looks as though you are overcomplicating it slightly. I'd use boost::function<> like so:
See it live on http://liveworkspace.org/code/b2c5a38d8c3499eefb6330a839a89d0a
#define BOOST_RESULT_OF_USE_DECLTYPE
#include <iostream>
#include <string>
#include <map>
#include <boost/function.hpp>
#include <boost/foreach.hpp>
#include <boost/phoenix.hpp>
using namespace std;
class Holder {
public:
void vRun1(std::string s){ cout << "vRun1 " << s << endl; }
void vRun2(std::string s){ cout << "vRun2 " << s << endl; }
void vRun3(std::string s){ cout << "vRun3 " << s << endl; }
};
typedef std::map< std::string, boost::function<void(Holder&, std::string)> > Replacer;
int main()
{
Replacer replacer;
replacer["a"] = &Holder::vRun1;
replacer["b"] = &Holder::vRun2;
replacer["c"] = &Holder::vRun3;
Holder hol;
for (Replacer::const_iterator it=replacer.begin(); it != replacer.end(); ++it)
{
(it->second)(hol, it->first);
}
Alternatively:
std::cout << "Using BOOST_FOREACH:\n";
BOOST_FOREACH(Replacer::value_type& pair, replacer)
{
(pair.second)(hol, pair.first);
}
Or even:
std::cout << "Using Boost Phoenix:\n";
namespace phx = boost::phoenix;
using namespace phx::arg_names;
std::for_each(replacer.begin(), replacer.end(),
phx::bind(
phx::bind(&Replacer::value_type::second, arg1),
phx::ref(hol),
phx::bind(&Replacer::value_type::first, arg1)));
}
Outputs
vRun1 a
vRun2 b
vRun3 c
Using BOOST_FOREACH:
vRun1 a
vRun2 b
vRun3 c
Using Boost Phoenix:
vRun1 a
vRun2 b
vRun3 c
This works for me:
#include <iostream>
#include <string>
#include <boost/assign.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/function.hpp>
using namespace boost::lambda;
class Holder
{
public:
void vRun1(std::string s){ std::cout << "vRun1 " << s << std::endl; }
void vRun2(std::string s){ std::cout << "vRun2 " << s << std::endl; }
void vRun3(std::string s){ std::cout << "vRun3 " << s << std::endl; }
};
// --------------------
typedef std::map <std::string,
boost::function<void(Holder&, std::string)> > Replacer_t;
Replacer_t replacer;
typedef Replacer_t::value_type ReplacerValue_t;
int main()
{
boost::assign::insert(replacer)
(std::string("buh"), &Holder::vRun1)
(std::string("mar"), &Holder::vRun2)
(std::string("heu"), &Holder::vRun3);
Holder hol;
for_each(replacer.begin(),
replacer.end(),
bind(protect(bind(bind(&ReplacerValue_t::second,_2),
_1,
bind(&ReplacerValue_t::first,_2))),
boost::ref(hol), _1));
}
This took about 2 hours of fighting with boost::lambda. In C++11 I would write it in 30 seconds, so update your compilers accordingly.