为什么我会得到错误和

why do i get the error and ...?

本文关键字:错误 为什么      更新时间:2023-10-16

在我的程序中,我声明了一个函数prototype,类似于:

void callToPrint(LPTSTR , LPVOID , DWORD , string )

但由于这句话,我得到了以下错误:error C2061: syntax error : identifier 'string'

代码中还有其他错误,表明function不采用4 arguments.

(error C2660: 'callToPrint' : function does not take 4 arguments)

为什么会出现此错误?我该如何修复它们

我的第二个问题是:

  • 我已经声明了LPTSTR类型的变量nameofPrinter,但当我编写语句getline( cin , nameOfPrinter )时,显示的错误是重载函数getline的实例与参数列表不匹配。那么我如何从用户那里接收nameOfPrinter

第一个问题的答案:

error C2061: syntax error : identifier 'string'

您需要在声明函数的头文件或源文件中包含string头文件,例如:

 #include <string>

&

namespace std;应该包含在源文件中类似:

using namespace std;

或者或者,您应该使用:

std::string

第二个问题的答案:

istream::getline()istream类中的一个函数,具有以下重载版本:

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

显然,它不理解您定义的LPTSTR类型,因此它告诉您,没有匹配的函数调用将LPTSTR作为参数。

如何解决
在评论@Cody Gray中,向您解释了真正的问题,解决方案当然是按照建议将LPTSTR转换为与istream::getline()参数匹配的格式,这本质上意味着使用wcstomb((

wchar_t*中的字符串转换为char*

您的文件需要包含以下行:

#include <string>

头文件string包含string类的定义。因为该类在std命名空间中,所以函数原型需要是:

void callToPrint(LPTSTR , LPVOID , DWORD , std::string );

既然您在原型中使用LPTSTR,那么您必须使用Visual C++。如果您的项目设置为使用Unicode字符集而不是多字节字符集,则需要相应地调整您的类型。

对于Unicode字符集:

std::wstring nameOfPrinter;
std::getline( std::wcin , nameOfPrinter );

或者,您可以将字符串的类型声明为:

std::basic_string<TCHAR> nameOfPrinter;

不幸的是,不存在这样一个用于在cinwcin之间切换的模板化类。因此,您将不得不求助于预处理器。

#if defined(UNICODE) || defined(_UNICODE)
  #define _tcin wcin
#else
  #define _tcin cin
#endif
std::basic_string<TCHAR> nameOfPrinter;
std::getline( std::_tcin , nameOfPrinter );

但由于此语句,我得到以下错误:错误C2061:语法错误:标识符"string">

您需要#include <string>并使用using namespace std;或将string声明为std::string才能使用string类。

显示的错误不是重载函数getline的实例与参数列表匹配。

CCD_ 33的第二个参数是对CCD_。显然LPTSTR不是std::string。请改用std::string

看起来您正在开发GUI应用程序。为什么使用cin进行输入