'Error while passing a vector to a Constructor in C++
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
Edit 1 Thanks for all the answers, they were almost perfect. My code works perfectly fine just adding the Defualt constructor to my class. The thing is that my code should be good also adding a copy constructor like this one
Bandiera(const Bandiera &b)
{
colori = b.colori;
}
but it gives me the same error as the initial one. Hope someone can explain this to me. Thanks
Solution 1:[1]
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.
Solution 2:[2]
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 } );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 273K |
| Solution 2 | Vlad from Moscow |
