make_pair(字符串、类):错误:标记之前的预期主表达式

make_pair(string, class): error: expected primary-expression before â)â token

本文关键字:表达式 错误 pair 字符串 make      更新时间:2023-10-16

错误:在标记之前需要主表达式

我不完全确定这里发生了什么,因为我的朋友们也在做这个项目,似乎无法判断出哪里出了问题。如有任何关于此错误的帮助,我们将不胜感激。错误所指的行上有一条注释指出了这一点。我正试图通过下面的代码将一对插入到地图中。

theCandidatesmap<string, class>,在这种情况下,该类被称为Candidate。

void TallyVotes::initialize(Scanner& inStream)
    {   
        numberOfLosers = 0;
        numberOfVotes = boost::lexical_cast<int>(inStream.next());
        numberOfCandidates = boost::lexical_cast<int>(inStream.next());
        for(int i = 0; i < numberOfVotes ; i++)
        {
            for(int j = 0; j < numberOfCandidates ; i++)
            {
                theVotes[i][j] = inStream.next();
                cand = theVotes[i][j];
                if(i == 0)
                {   
                    theCandidates.insert(make_pair(cand, Candidate));//ERROR ON THIS LINE       
                }
            }
        }
    } // void TallyVotes::initialize(Scanner& inStream)

make_pair函数采用两个作为参数,而不是一个值和一个类型。

尝试例如

make_pair(cand, Candidate())
//      Note parentheses ^^

表达式Candidate()创建一个临时对象,然后将其复制到std::map中。