I'm new with C OOP. Seems that in my simple code i'm not passing correctly an argument to the constructor of my class. Where's the error?
This is the class that i've defined
class Bandiera
{
private:
vector<string> colori;
public:
Bandiera(vector<string> c)
{
colori = c;
}
bool biancoPresente()
{
for (short i = 0; i < colori.size(); i )
if (colori[i] == "bianco")
return true;
return false;
}
bool rossoPresente()
{
for (short i = 0; i < colori.size(); i )
if (colori[i] == "rosso")
return true;
return false;
}
bool colorePresente(string nomeColore)
{
for (short i = 0; i < colori.size(); i )
if (colori[i] == nomeColore)
return true;
return false;
}
};
And this is my main:
int main()
{
map<string, Bandiera> mappaColoriBandiere;
ifstream f("bandiere.txt");
string buffer, nomeNazione, colore;
vector<string> bufferColori;
while (getline(f, buffer))
{
stringstream ss(buffer);
ss >> nomeNazione;
while (!ss.eof())
{
ss >> colore;
bufferColori.push_back(colore);
}
Bandiera b(bufferColori);
mappaColoriBandiere[nomeNazione] = b;
bufferColori.clear();
}
map<string, Bandiera>::iterator it;
for (it = mappaColoriBandiere.begin(); it != mappaColoriBandiere.end(); it )
if (it->second.biancoPresente() && it->second.rossoPresente())
cout << it->first << endl;
return 0;
}
Take for correct the part of the code where I read data from the ifstream. The error the is given bakc to me is this one:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c \tuple:1586:70: error: no matching function for call to 'Bandiera::Bandiera()'
second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
^
bandiere.cpp:14:5: note: candidate: Bandiera::Bandiera(std::vector<std::__cxx11::basic_string<char> >)
Bandiera(vector<string> c)
^~~~~~~~
bandiere.cpp:14:5: note: candidate expects 1 argument, 0 provided
bandiere.cpp:8:7: note: candidate: Bandiera::Bandiera(const Bandiera&)
class Bandiera
^~~~~~~~
bandiere.cpp:8:7: note: candidate expects 1 argument, 0 provided
bandiere.cpp:8:7: note: candidate: Bandiera::Bandiera(Bandiera&&)
bandiere.cpp:8:7: note: candidate expects 1 argument, 0 provided
CodePudding user response:
The class Bandiera
does not meet the requirements of std::map<Key,T,Compare,Allocator>::operator[]
- mapped_type must meet the requirements of CopyConstructible and DefaultConstructible.
class Bandiera
must be CopyConstructible and DefaultConstructible, i.e. define a copy and default constructors, or you should use std::map<Key,T,Compare,Allocator>::insert
or std::map<Key,T,Compare,Allocator>::emplace
.
CodePudding user response:
In this statement
mappaColoriBandiere[nomeNazione] = b;
if the map does not have an element with the key nomeNazione
then such an element is created calling the default constructor for the mapped object that is for an object of the type Bandiera
. But this class does not have the default constructor. So the compiler issues an error.
Instead of the operator you could use for example the method insert like
mappaColoriBandiere.insert( { nomeNazione, b } );