【问题标题】:C++: Syntax error: 'string' but still compiler successfuly in another project? [closed]C++:语法错误:'string'但仍然在另一个项目中成功编译? [关闭]
【发布时间】:2019-07-30 02:39:19
【问题描述】:

我已将一个代码文件从另一个成功编译的代码复制到我的代码中,但在我的代码中,它有很多错误,与其他代码一样。像这样:

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\pathcch.h(34): error C2059: syntax error: 'string'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\pathcch.h(34): error C2334: unexpected token(s) preceding '{'; skipping apparent function body

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(37): error C2059: syntax error: 'string'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(37): error C2238: unexpected token(s) preceding ';'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(49): error C2059: syntax error: 'string'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(49): error C2238: unexpected token(s) preceding ';'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(57): error C2059: syntax error: 'string'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(57): error C2238: unexpected token(s) preceding ';'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(65): error C2059: syntax error: 'string'

1>  c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\propkey.h(65): error C2238: unexpected token(s) preceding ';'

请帮帮我,谢谢!

对不起我的英语不好。

【问题讨论】:

  • 你在编译驱动吗?
  • @Nina:对不起,我不知道怎么编译驱动,你能告诉我吗?谢谢。
  • 你确定你正在编译 C++ 而不是 C?
  • 显示您的代码。
  • @AnhHuânLê 请edit 你的问题在 StackOverflow 上显示你的代码,而不是在其他网站上

标签: c++ string notifications syntax-error


【解决方案1】:

我安装了那个版本的 Windows SDK。

pathcch.h 第 34 行说

extern "C" {

propkey.h 在失败的行中包含这样的代码:

DEFINE_PROPERTYKEY(PKEY_Audio_ChannelCount, 0x64440490, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03, 7);

DEFINE_PROPERTYKEY宏在propkeydef.h中定义,扩展为

EXTERN_C const PROPERTYKEY DECLSPEC_SELECTANY name = ...

编译器错误的原因是extern "C"

一个可能的原因是,您使用 C 编译器而不是 C++ 编译器编译源代码。

C 语言中没有extern "C",只有 C++。

如果您使用的是 Visual C++,默认情况下,它会根据文件扩展名检测语言。修复的简单方法,将源文件扩展名重命名为 *.cpp*.cxx

Here’s more info.

另一个可能的原因是,您使用的是非 Microsoft 的编译器。如果是这样,请在问题中指定编译器+版本,可能有人会回答。

更新:我建议创建一个正确类型的新项目(可能是 C++/Windows 桌面/Windows 桌面应用程序),确保它编译并运行,然后开始添加您的代码。循序渐进地去做。 首先从主 .cpp 文件中删除所有内容,只留下 #include "stdafx.h"wWinMain 函数,只需从该函数返回 0。然后添加您的#include 语句,它们应该转到stdafx.h。然后添加#pragma comment部分,他们应该去一些.cpp文件,不管哪个。最后,将您的代码添加到主 .cpp 文件中。在每一步之间,确保项目构建。

【讨论】:

  • 感谢您的帮助,我还将文件(从 ".h" 更改为 ".cpp")并将错误更改为 stdafx.cpp 中的 '#include' statement specified with the /Ycstdafx.h command-line option was not found in the source file,它定义了 "include: precomp.h",我正在尝试修复它:D
  • @AnhHuânLê 错误不在您使用的代码中,而是在您的项目设置中。我认为你应该学习 C++,特别是 2 件事(1)头文件和源文件有什么区别(2,微软特定)什么是预编译头以及如何设置和使用它们。
  • 是的,我是 OOP C++ 的新手,我会研究它。非常感谢。
  • @AnhHuânLê 好吧,每个人都从某个地方开始。另请参阅更新。
  • 我按照你说的做,但我不知道如何添加#pragma comment,我可以使用#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")吗?期待很快收到您的来信。
【解决方案2】:

尝试将 '#include “string”' 添加到包含并添加 'using namespace std;'使用它,您可以确定它是否是缺少的包含/命名空间。

【讨论】:

  • Windows SDK不使用STL,OP的错误与std::string无关
  • 我也觉得和std::string无关
  • windows SDK是否定义了自己的字符串类型?
猜你喜欢
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 2013-02-09
  • 2018-04-24
相关资源
最近更新 更多