I've been trying to write a header to implement logical connectives in c++. The most recent attempt ended as follows:
This is not a header but just an attempt to implement it in a program
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Model
{
vector< pair<string,bool> > model;
public:
bool find(string name);
};
bool Model::find(string name)
{
//pair<string, bool> x;
for(auto &x: model)
{
if(x == make_pair(name,true) || x== make_pair(name,false))
return true;
}
return false;
}
void balanced(string s);
class Sentence
{ public:
bool evaluate(Model model){ throw ("Nothing to evaluate"); return false;}
string formula(){return "";}
set<string> symbols(){set<string> symbols; return symbols;}
string parenthesize(string s);
};
class Symbol:public Sentence
{
string name;
public:
Symbol(string name) { this->name = name;}
bool evaluate(Model model);
string formula(){return this->name;}
set<string> symbols(){ set<string> s {name};
return s;}
};
bool Symbol::evaluate(Model model)
{
if(model.find(this->name))
return true;
return false;
}
class Not:public Sentence
{
Sentence operand;
public:
Not(Sentence operand) { this-> operand = operand;}
bool evaluate(Model model){return !this->operand.evaluate(model);}
string formula() {return "¬" + this->parenthesize(this->operand.formula());}
set<string> symbols(){return this->operand.symbols();}
};
class And:public Sentence
{
vector<Sentence> conjuncts;
public:
And(){ }
And(vector<Sentence> conjuncts) {this->conjuncts = conjuncts;}
bool evaluate(Model model);
void add(vector<Sentence> conjuncts){this->conjuncts.insert(this->conjuncts.end(),conjuncts.begin(),conjuncts.end());}
string formula();
set<string> symbols();
};
bool And::evaluate(Model model)
{
vector <bool> v;
for(auto conjunct : conjuncts)
v.push_back(conjunct.evaluate(model));
return all_of(v.begin(),v.end(),[](bool b){return b;});
}
string And::formula()
{
if(this->conjuncts.size() == 1)
return this->conjuncts[0].formula();
string s = " ∧ ";
string temp = "";
for (auto conjunct : this->conjuncts)
temp += this->parenthesize(conjunct.formula()) + s;
return temp;
}
set<string> And::symbols()
{
set<string> temp(this->conjuncts.begin(),this->conjuncts.end());
return temp;
}
int main()
{
auto rain = Symbol("rain");
cout<<rain.formula();
return 0;
}
after compilation the errors that popup are:
In file included from C:/fakepath/include/c++/set:60, from logic.cpp:2:
C:/fakepath/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::M_insert_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Arg&&, _NodeGen&) [with _Arg = Sentence&; _NodeGen = std::_Rb_treestd::__cxx11::basic_string<char, std::__cxx11::basic_string, std::_Identitystd::__cxx11::basic_string<char >, std::lessstd::__cxx11::basic_string<char >, std::allocatorstd::__cxx11::basic_string<char > >::_Alloc_node; _Key = std::__cxx11::basic_string; _Val = std::__cxx11::basic_string; _KeyOfValue = std::_Identitystd::__cxx11::basic_string<char >; _Compare = std::lessstd::__cxx11::basic_string<char >; _Alloc = std::allocatorstd::__cxx11::basic_string<char >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iteratorstd::__cxx11::basic_string<char >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iteratorstd::__cxx11::basic_string<char >]':
C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:2468:4: required from 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = __gnu_cxx::__normal_iterator<Sentence*, std::vector >; _Key = std::__cxx11::basic_string; _Val = std::__cxx11::basic_string; _KeyOfValue = std::_Identitystd::__cxx11::basic_string<char >; _Compare = std::lessstd::__cxx11::basic_string<char >; _Alloc = std::allocatorstd::__cxx11::basic_string<char >]'
C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_set.h:193:4: required from 'std::set<_Key, _Compare, _Alloc>::set(_InputIterator, _InputIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<Sentence*, std::vector >; _Key = std::__cxx11::basic_string; _Compare = std::lessstd::__cxx11::basic_string<char >; _Alloc = std::allocatorstd::__cxx11::basic_string<char >]' logic.cpp:93:64: required from here
C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_tree.h:2217:59: error: no match for call to '(std::_Identitystd::__cxx11::basic_string<char >) (Sentence&)' = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v)); ~~~~~~~~~~~~~^~~~~ In file included from C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/string:48, from C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/locale_classes.h:40, from C:fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/ios_base.h:41, from C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/ios:42, from C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/ostream:38, from C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/iostream:39, from logic.cpp:1:
C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_function.h:1111:7: note: candidate: '_Tp& std::_Identity<_Tp>::operator()(_Tp&) const [with _Tp = std::__cxx11::basic_string]' operator()(_Tp& __x) const ^~~~~~~~ C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_function.h:1111:7: note: no known conversion for argument 1 from 'Sentence' to 'std::__cxx11::basic_string&'
C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_function.h:1115:7: note: candidate: 'const _Tp& std::_Identity<_Tp>::operator()(const _Tp&) const [with _Tp = std::__cxx11::basic_string]' operator()(const _Tp& __x) const ^~~~~~~~ C:/fakepath/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_function.h:1115:7: note: no known conversion for argument 1 from 'Sentence' to 'const std::__cxx11::basic_string&'
P.S: I know it is very tedious to go through the entire list of errors; but I can't figure out the sources/cause of these errors; so any help in this will be of great help
Aucun commentaire:
Enregistrer un commentaire