简体   繁体   中英

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

The class Bandiera does not meet the requirements of std::map<Key,T,Compare,Allocator>::operator[]

class Bandiera must be CopyConstructible and DefaultConstructible, ie 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 .

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 } );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM