简体   繁体   English

在 C++ 中将向量传递给构造函数时出错

[英]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?我是 C++ OOP 的新手。似乎在我的简单代码中我没有正确地将参数传递给 class 的构造函数。错误在哪里?

This is the class that i've defined这是我定义的 class

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.纠正我从 ifstream 读取数据的代码部分。 The error the is given bakc to me is this one: bakc 给我的错误是这个:

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.编辑 1 感谢所有的答案,它们几乎是完美的。 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只需将 Defualt 构造函数添加到我的 class,我的代码就可以正常工作。问题是我的代码应该很好,还可以添加像这样的复制构造函数

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不满足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 . class Bandiera必须是 CopyConstructible 和 DefaultConstructible,即定义一个副本和默认构造函数,或者你应该使用std::map<Key,T,Compare,Allocator>::insertstd::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 .如果 map 没有带有键nomeNazione的元素,那么将创建这样一个元素,调用映射的 object 的默认构造函数,该构造函数用于Bandiera类型的 object。 But this class does not have the default constructor.但是这个class是没有默认构造函数的。 So the compiler issues an error.所以编译器会报错。

Instead of the operator you could use for example the method insert like您可以使用例如方法插入而不是运算符

mappaColoriBandiere.insert( { nomeNazione, b } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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