为什么我会得到错误和
why do i get the error and ...?
在我的程序中,我声明了一个函数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;
不幸的是,不存在这样一个用于在cin
和wcin
之间切换的模板化类。因此,您将不得不求助于预处理器。
#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
进行输入
- 我的字符计数代码计算错误.为什么
- C++ 错误 为什么这个矢量不输出?
- 当在函数调用中递增值时,程序正常工作,但是如果我们在单独的行中增加值而不是呼叫函数,则会出现错误.为什么
- 我正在将QPixmap传递给QAbstractButton::setIcon而不是QIcon,但我没有得到错误...为什么?
- 在 ZeroMQ 中绑定订阅者套接字并连接发布者套接字会在代码运行时给出错误.为什么
- 分段错误:为什么这里的数组索引越界了
- Box2D不寻常的错误.为什么 0阻止错误
- c++分段错误:为什么这个程序给出运行时错误(SIGSEGV)
- 链接列表错误 - 为什么头和尾引用指向相同地址
- VC++的一个大错误?为什么初始值设定项列表不对结构进行值初始化
- 分配大量内存会产生错误?为什么
- 得到一个段错误.为什么
- 变量名称与函数名称相同,导致编译器错误.为什么
- 访问在相应类的静态函数中创建的对象的私有成员变量不会导致错误.为什么?
- Qt5:此代码使用msvc2012编译正常,但使用MinGW编译时出现错误..为什么?
- 奇怪的错误-为什么编译器试图调用复制构造函数
- 我在使用 std::map 时收到此错误.为什么
- 我写了这个程序来反转链表中的元素,在编译这个程序后,在reverse()中显示错误.为什么
- Valgrind报告写入错误?为什么?
- 编译器错误 - 为什么我不能在Sublime Text 2中构建C++?