为什么我遇到getline()? “没有重载函数的实例与参数列表匹配”和“数据不明确”

问题描述 投票:0回答:2

我之前使用过这个确切的函数,但只是复制并粘贴到另一个文件导致它停止工作。唯一的变化是我添加了“using namespace std”。

在我的“ReadData()”函数中,我在getline(cin,data)上遇到一个错误,即没有重载函数的实例与参数列表匹配。此外,我在调用中的“数据”中收到错误,称“数据不明确”。

该函数应该从文本文件中读取数据并将文本行存储在数据中以供进一步处理。这个功能确实如此,所以我只是不确定。

#include <iostream>
#include "NvraArray.h"
#include "NvraRecord.h"

#include <vector>
#include <string>
#include <ctime>
using namespace std;

// Globals
string data; // Stores the line that getline is at
vector<string> rows; // Stores the rows of data
vector<int> recordNumbersSeen; // Holds records numbers that have been checked
string strWords[24]; // Holds the individual columns of data for processing
int rowCounter = 0; // Counts the rows of data coming in

// Prototypes
// Reads the data from cin into the "rows" vector
void ReadData();
// Checks the current row against the list of records already seen
bool isDuplicate(int recordID);
// Checks the current row for invalid data
bool isValidData();
// Splits the row into an array to process
void SplitRowIntoArray(std::string row);

int main(){

    // For testing purposes
    srand(time(NULL));

    NvraArray array;
    NvraRecord record;



    system("pause");
    return 0;
}

void ReadData(){
    while(getline(cin,data)){

        // if on the first row, do nothing and skip to the next.
        if(rowCounter != 0){

            rows.push_back(data);
        }else{
            rowCounter++;   
        }
    }
    rowCounter = 0;
}
c++ cin getline ambiguous
2个回答
5
投票

这是为什么你不应该使用using namespace std;的一个主要例子。你有一个名字冲突:string datastd::data相冲突。

如果这还不足以说服你,请查看this命名空间中某些其他名称的std列表。如果使用using namespace std;,如果碰巧包含正确的标题,则这些名称中的任何一个都可能导致冲突。


5
投票

您遇到了与C ++ 17一起引入的std::data函数模板的冲突。显然,你所包含的标准库标题之一是包含在iterator中。

将全局变量重命名为data以外的其他变量应该可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.