为什么我在使用 getline() 时遇到问题? "No instance of overloaded functions matches the argument list" 和 "data is ambiguous"

Why am I having trouble with getline()? "No instance of overloaded functions matches the argument list" and "data is ambiguous"

我以前使用过这个确切的功能,但只是将它复制并粘贴到另一个文件导致它停止工作。唯一的变化是我添加了 "using namespace std".

在我的 "ReadData()" 函数中,我在 getline(cin, data) 上收到错误,没有重载函数的实例与参数列表匹配。此外,我在通话中收到有关 "data" 的错误,显示 "data is ambiguous."

该函数应该从文本文件中读取数据并将文本行存储在数据中以供进一步处理。这个函数的工作原理是怎样的,所以我不确定。

#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++17 引入的 std::data 函数模板发生冲突。显然,您要包含的标准库之一 headers 将包含内容拉入 iterator.

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

这是您不应该使用using namespace std;的一个典型例子。您有名称冲突:string datastd::data.

冲突

如果这还不足以说服您,请查看 this std 命名空间中其他一些名称的列表。如果您使用 using namespace std; 这些名称中的任何一个 如果您碰巧包含正确的 headers.

可能会导致冲突