请求分号时出现令人困惑的错误(错误 C2143)

Confusing error requesting semicolon (error C2143)

本文关键字:错误 C2143 请求      更新时间:2023-10-16

我正在使用VS Professional 2013,我正在构建一个控制台应用程序。我的一种方法是根据用户输入确定字符串长度。我在此方法的第 5 行不断收到一个令人困惑的错误:缺少分号(错误 C2143)。据我所知,行内的任何函数都不需要额外的分号。另外,我故意不调用命名空间。下面复制了 #include 函数,并存储在头文件中。

#include <stdio.h> 
#include <tchar.h> //Part of VS' implementation for applications. Can effectively be ignored. 
#include <iostream>
#include <string> 
int main() {
    std::string s;
    std::cout << "Enter your string: " << std::flush;
    std::string.getline(std::cin, s);
    const int size = s.length();
    std::cout << "The total number of characters entered is: " << size << std::endl;
}

std::string没有成员getline,所以std::string.getline(std::cin, s);是非法的。

你想要

std::getline(std::cin, s);