简体   繁体   中英

why do i get the error and …?

In my program i declared a function prototype like:

void callToPrint(LPTSTR, LPVOID, DWORD, string )

But i get the following error due to this statement: error C2061: syntax error: identifier 'string'

There are other errors also in the code which tell that the function does not take 4 arguments.

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

Why do i get this error? And how can i fix them?

My second question is:

  • I have declared a variable nameofPrinter of type LPTSTR but when i write the statement getline( cin, nameOfPrinter ) , the error displayed is no instance of overloaded function getline matches the argument list. Then how can i receive the nameOfPrinter from the user?

Answer to First Question:

error C2061: syntax error : identifier 'string'

You need to include the string header file in the header or source file where you are declaring the function, like:

 #include <string>

&

The namespace std ; should be included in your source file like:

using namespace std;

Or Alternatively, you should use:

std::string

Answer to Second Question:

istream::getline() is a function in the istream class with following overloaded versions:

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

Obviously, it does not understand your defined type of LPTSTR , so it tells you that there is no matching function call which takes LPTSTR as an argument.

How to resolve it?
In the comments @Cody Gray, explains you the real problem and the solution is ofcourse as suggested to convert LPTSTR in a format so that it cam match the istream::getline() parameter, which essentialy means convert the string you have in wchar_t* to a char* by using wcstombs()

Your file needs to contain the following line:

#include <string>

The header file string contains the definition for the string class. Because that class is within the std namespace the function prototype needs to be:

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

Since you're using LPTSTR in the prototype you must be using Visual C++. If your project is set to use Unicode charset instead of multi-byte char set you need to adapt your types accordingly.

For Unicode charset:

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

Or, you could declare the type of your strings as:

std::basic_string<TCHAR> nameOfPrinter;

Unfortunately, such a templated class does not exist for switching between cin and wcin . So you'll have to resort to the preprocessor.

#if defined(UNICODE) || defined(_UNICODE)
  #define _tcin wcin
#else
  #define _tcin cin
#endif

std::basic_string<TCHAR> nameOfPrinter;
std::getline( std::_tcin , nameOfPrinter );

But i get the following error due to this statement: error C2061: syntax error: identifier 'string'

You need to #include <string> and either use using namespace std; or declare string as std::string to use the string class.

the error displayed is no instance of overloaded function getline matches the argument list.

The second parameter of getline() is a reference to std::string . Apparently LPTSTR is not std::string . Use std::string instead.

Looks like you're developing GUI application. Why are you taking input using cin ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM