You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++调用getline()遇‘no matching function...’错误求助

Fixing "no matching function for call to getline()" Error in Your C++ File Reading Code

Let's break down why you're hitting this error and fix it step by step:

1. Immediate Syntax Error in the Final getline Call

Look at your last line of code:

getline(fpd,st.stbno,...

The ... here is not a valid delimiter argument for std::getline. The third parameter of getline expects a single char (like the '|' you used for the other fields). This invalid input is the direct cause of the "no matching function" error. Replace ... with the actual delimiter from your file (most likely '|' if it's consistent with the rest of your records, or '\n' if it's the end of a line).

2. Check the Data Types of st's Members

std::getline requires its second argument to be a non-const reference to std::string. If st.usn, st.name, st.stbno, etc., are declared as C-style char arrays (e.g., char usn[20];) instead of std::string, the compiler won't find a matching getline overload. Double-check your struct/class definition to ensure all these members are std::string types.

3. Verify Required Header Files Are Included

You need these headers at the top of your code to use the standard library functions you're calling:

#include <iostream>   // For cout
#include <fstream>    // For ifstream
#include <string>     // Critical for std::string and std::getline
#include <iomanip>    // For setw
// Note: getch() is non-standard; if you use it, add <conio.h> (but prefer std::cin.get() for portability)

Forgetting <string> is a common mistake that makes the compiler fail to recognize std::string, leading to mismatched getline arguments.

4. Ensure Proper Namespace Usage

If you haven't added using namespace std; (or choose not to use it), you must qualify all standard library identifiers with std:::

std::ifstream fpd;
std::getline(fpd, st.usn, '|');
std::cout << "\tUSN" << std::setw(10) << "\tName" << std::setw(20) << "Book Issued\n";

Without this, the compiler won't associate getline with the standard library version, triggering the same error.

Bonus: Fix Your File Reading Loop

Your current while(fpd) condition can cause issues (it may read the last record twice if the file ends without a proper delimiter). A more reliable approach is to check if the first getline succeeds before processing the record:

// Move the header outside the loop so it doesn't print every iteration!
std::cout << "\tUSN" << std::setw(10) << "\tName" << std::setw(20) << "Book Issued\n";
while (std::getline(fpd, st.usn, '|')) {
    std::getline(fpd, st.name, '|');
    std::getline(fpd, st.email, '|');
    std::getline(fpd, st.phone, '|');
    std::getline(fpd, st.stbno, '|'); // Fixed delimiter here
    // Print the parsed record data
    std::cout << "\t" << st.usn << std::setw(10) << "\t" << st.name << std::setw(20) << st.stbno << "\n";
}

Also, getch() is non-standard. For portable C++, replace it with std::cin.get() to pause the program.


内容的提问来源于stack exchange,提问作者Obaid Rehman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:35:10