繁体   English   中英

需要针对getline函数错误的此编译错误的帮助:没有匹配的函数来调用'getline(std :: istream&,char&)'|

[英]need help with this compile error for getline function error: no matching function for call to 'getline(std::istream&, char&)'|

我有一个带有getline函数的源代码文件,我在编译该文件时会收到错误消息(下面的代码和错误)。 我的问题是我从一个已经编译且可以运行的程序(也包括在下面)中复制并粘贴了整个函数。 我在程序中的其他2个源代码文件中也都有getline函数,它们都可以很好地编译。 我对编程还很陌生,所以刚开始进行c ++编程(Java更好),因此请尝试使答案保持简单。 我在这里(和google)浏览了一些已经发布的问题和答案,但是所有发现问题的答案都说该函数的参数不正确。 但是我知道它们在这里是正确的,因为它可以在其他程序中正常工作。 您还可以在工作文件中看到,唯一包含的#include是iostream,它可以工作,这与Code :: Blocks中的g ++编译器兼容。 我确保也包括所有需要的变量/常量。

这是我得到错误的函数和文件的代码。 我所指的部分标有3 * (对不起,我能做的最好)。 错误也会在帖子底部重新打印。

#include <iostream>
#include <istream>
#include "candidates.h"

using namespace std;

int nCandidatesInPrimary;

int delegatesForThisState;

const int maxCandidates = 10;

int delegatesWon[maxCandidates];

int totalVotes;

int totalDelegates = 0;

int votesForCandidate[maxCandidates];

string candidate[maxCandidates];

int nCandidates;

string candidateNames;

void readCandidates ()
{
  cin >> nCandidates;
  string line;
  getline (cin, line);

  for (int i = 0; i < nCandidates; ++i)
    {
      ***getline (cin, candidateNames[i]);***
      delegatesWon[i] = 0;
    }
}

int findCandidate (std::string name)
{
  int result = nCandidates;
  for (int i = 0; i < nCandidates && result == nCandidates; ++i)
    if (candidateNames[i] == name)
      result = i;
  return result;
}

void printCandidateReport (int candidateNum)
{
  int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
  if (delegatesWon[candidateNum] >= requiredToWin)
    cout << "* ";
  else
    cout << "  ";
  cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum] << endl;
}

void assignDelegatesToCandidates ()
{
  int remainingDelegates = delegatesForThisState;
  for (int i = 0; i < nCandidatesInPrimary; ++i)
    {
      int candidateNum = findCandidate(candidate[i]);
      int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
      if (nDel > remainingDelegates)
    nDel = remainingDelegates;
      delegatesWon[candidateNum] += nDel;
      remainingDelegates -= nDel;
    }
}

这是已经运行的程序中的代码。

#include <iostream>

using namespace std;

// Max # of candidates permitted by this program
const int maxCandidates = 10;

// Names of the candidates participating in this state's primary
string candidate[maxCandidates];

// Names of all candidates participating in the national election
std::string candidateNames[maxCandidates];

// How many delegates are assigned to the state being processed
int delegatesForThisState;

// How many delgates have been won by each candidate
int delegatesWon[maxCandidates];

// How many candidates in the national election?
int nCandidates;

// How many candidates in the primary for the state being processed
int nCandidatesInPrimary;

// How many states participate in the election
int nStates;

// How many delegates in the election (over all states)
int totalDelegates = 0;

// How many votes were cast in the primary for this state
int totalVotes;

// How many votes wone by each candiate in this state's primary
int votesForCandidate[maxCandidates];


int findCandidate (std::string name);

/**
 * For the most recently read primary, determine how many delegates have
 * been won by each candidate.
 */
void assignDelegatesToCandidates ()
{
  int remainingDelegates = delegatesForThisState;
  for (int i = 0; i < nCandidatesInPrimary; ++i)
    {
      int candidateNum = findCandidate(candidate[i]);
      int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
      if (nDel > remainingDelegates)
    nDel = remainingDelegates;
      delegatesWon[candidateNum] += nDel;
      remainingDelegates -= nDel;
    }
}



/**
 * Find the candidate with the indicated name. Returns the array index
 * for the candidate if found, nCandidates if it cannot be found.
 */
int findCandidate (std::string name)
{
  int result = nCandidates;
  for (int i = 0; i < nCandidates && result == nCandidates; ++i)
    if (candidateNames[i] == name)
      result = i;
  return result;
}


/**
 * Print the report line for the indicated candidate
 */
void printCandidateReport (int candidateNum)
{
  int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
  if (delegatesWon[candidateNum] >= requiredToWin)
    cout << "* ";
  else
    cout << "  ";
  cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum] << endl;
}


/**
 * read the list of candidate names, initializing their delegate counts to 0.
 */
void readCandidates ()
{
  cin >> nCandidates;
  string line;
  getline (cin, line);

  for (int i = 0; i < nCandidates; ++i)
    {
      ***getline (cin, candidateNames[i]);***//already working
      delegatesWon[i] = 0;
    }
}


/**
 * read the info on one state's primaries
 */
void readState ()
{
  totalVotes = 0;
  cin >> nCandidatesInPrimary >> delegatesForThisState;
  totalDelegates += delegatesForThisState;  // "x += y" is a shorthand for "x = x + y"
  string word, line;
  getline (cin, line);
  for (int i = 0; i < nCandidatesInPrimary; ++i)
    {
      cin >> votesForCandidate[i];
      totalVotes = totalVotes + votesForCandidate[i];

      cin >> word;
      getline (cin, line);
      candidate[i] = word + line;
    }
}



/**
 * Generate the report on the national primary election.
 */
int main(int argc, char** argv)
{
  readCandidates();
  int nStates;
  cin >> nStates;
  for (int i = 0; i < nStates; ++i)
    {
      readState();
      assignDelegatesToCandidates();
    }
  for (int i = 0; i < nCandidates; ++i)
    {
      printCandidateReport(i);
    }
  return 0;
}

错误:没有匹配的函数可以调用'getline(std :: istream&,char&)'|

看起来您打算将候选人姓名声明为vector<string> candidateNames;

vector<string> candidateNames;

void readCandidates ()
{
  cin >> nCandidates;
  string line;
  getline (cin, line);
  candidateNames.resize(nCandidates);    
  for (int i = 0; i < nCandidates; ++i)
    {
       getline (cin, candidateNames[i]);

    }
}

candidateNames是单个字符串,而不是字符串的集合。 如何设置它们的向量?

std::vector<string> candidateNames;

然后在for循环之前:

candidateNames.resize(nCandidates);

好像你忘了

#include <string>

其他标头可能已经拉入了字符串类本身,但并非所有支持功能,例如接受std::string getline重载。

抱歉,发布此问题可能不公平。 我能够找到问题,它在我的头文件中,我弄乱了那里的变量。 对于候选人名称[]我忘记了头文件末尾的[]。 而且由于我没有在我的帖子中包含该文件,所以您看不到它。 谢谢您的帮助。

暂无
暂无

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

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