main.cpp(11):错误 C2059:语法错误:"字符串"
main.cpp(11) : error C2059: syntax error : 'string'
环境:Visual Studio 2008,C++ 我正在将条形码打印机[TSC210]与控制器[NXP A4]串行连接,查看了其他帖子,但不确定导致问题的原因,我是C++的初学者。任何人都可以建议如何解决此错误吗?
收到错误和警告情况,例如:
1] main.cpp(11) : error C2059: syntax error : 'string'
2] main.cpp(11) : warning C4091: '__declspec(dllimport)' : ignored on left of 'int' when no variable is declared
我有以下代码
#include <windows.h>
#define BUFFER_SIZE 32
#define Naked __declspec(naked)
#define DllImport __declspec( dllimport )
namespace TSCLIB
{
DllImport("TSCLIB.dll", EntryPoint = "about")
int about();
}
BOOL PortOpen(HANDLE *port, DWORD baudRate)
{
DCB portDCB; ///< COM port configuration structure.
BOOL returnValue = FALSE;
COMMTIMEOUTS comTimeOut;
/// Opens interface to reader.
/// COM Port Configuration.
/// Changes the DCB structure settings.
/// Configures the port according to the specifications of the DCB structure.
/// Gets communication time out values.
/// Sets communication time out values.
return TRUE;
}
BOOL PortClose(HANDLE *port)
{
if (*port == NULL)
{
return FALSE;
}
CloseHandle(*port);
*port = NULL;
return TRUE;
}
int wmain(void)
{
HANDLE portHandle;
while (TRUE)
{
// case WRITE:
}
// Closes the serial port.
}
}
好吧,问题出在这一行:
DllImport("TSCLIB.dll", EntryPoint = "about")]
这里实际上有两个问题。第一个是语句以"]"结尾。我猜这可能只是一个错字,你正确地使用了";"。然而,问题也是EntryPoint = "about"
部分。您不能在C++中调用这样的方法。我猜你想这样做:
DllImport("TSCLIB.dll", "about");
此外,如上面的注释中所述,在 DllImport 的定义之后有";"。最后,若要在 Windows 上加载 C++ 中的 DLL 并从该 DLL 使用特定函数,应使用 Win32 API 函数 LoadLibrary 加载 DLL 和 GetProcAddress 以获取指向要调用的函数的指针。网上有很多例子,例如,这个SO帖子
相关文章:
- main.cpp(11):错误 C2059:语法错误:"字符串"
- 函数错误 C2059:语法错误:'>'不起作用
- 错误 C2059:语法错误:从结构成员声明'constant'
- 错误 C2059:语法错误:'}' C++
- 我在C++文件中的简单代码块上有 2 个错误。错误 C2059:语法错误:"?"。第二智能感知:预期表达式。怎么了?
- VS2010 和 VS2012 以及 C++11 错误 C2059:语法错误:"..."怎么走?
- 错误代码 Visual Studio 错误:c2059 奇怪的语法错误
- 错误 C2059:尝试在已知大小的头文件中创建 Qt 容器时为"常量"
- 我收到错误 C2059:语法错误:'}'使用 Visual Studio 2013 进行边做循环练习
- 错误 C2059:VS10 中“= 删除”上的语法错误 C++ 代码
- 错误 C2059:语法错误:',' - 用于宏'getcwd'
- 错误 C2059:语法错误: 'constant'
- 错误 C2059:语法错误'constant'
- 需要解决方案:错误C2059:语法错误'constant'
- 有人可以看看我的代码吗?错误 C2059:语法错误:"公共"
- 错误C2059:语法错误:'字符串'
- Visual C++ 2013 和错误 C2065:"复杂":未声明的标识符和错误 C2059:语法错误:","
- 如何解决错误 C2059:语法错误:"字符串""
- CUDA 错误 C2059:语法错误:'<'
- 错误 C2059:将 C 源文件添加到项目时出现语法错误 :'}'