【发布时间】:2019-05-22 19:07:29
【问题描述】:
字符串有些问题。我已经使用它们几个星期没有问题,但是在星期一的中途,我开始遇到奇怪的问题:
[Clang IntelliSense] Error: variable has incomplete type 'std::string'
每当我尝试制作字符串时。当我尝试wstring 和ostringstream 而不是stringstream 时会出现类似的错误。 initializing std::string* s 也可以正常工作,但s->append(...) 会导致"member has access into incomplete type 'std::string'"
这一切的发生是因为 Visual Studio (VS) 突然无法找到我已经使用了数周的#includes,包括一些标准库(不记得是哪个,因为...)。我通过让 VS 搜索正确的文件解决了这个问题,它找到了一些文件(不确定它们是否与以前相同或不同),所以它不再有 #include 语句的问题,但现在我有这个问题。
我已经使用<string.h> 好几个星期了。在研究这个问题时,我读到<string.h> 是错误的文件,所以我尝试了#include <string>,但没有区别。我已经尝试过<strings.h> 和<cstring>,但也没有区别。
我检查了我的外部依赖项,string.h 有几十个错误,我觉得这很奇怪,因为它是一个我没有接触过的标准文件。
我使用的是 VS2015、GNU make,我看到了 Clang IntelliSense,但我也看到了一些 g++,所以我不确定它实际使用的是什么编译器。也使用visualGDB 5.3。如果我忘记了什么,请告诉我。
#include <iostream>
#include <string>
#include <sstream>
int main(int argc, char *argv[])
{
std::string message = "uncomment";
...
message
是突出显示有问题的部分(类型不完整)
std::wstring wideMessage;
也是不完整的类型
- 预期结果:没有像过去几周那样的问题。我能够声明、初始化、追加()等等
- 实际结果:无法编译/调试
- 错误:
variable has incomplete type 'std::string' variable has incomplete type 'std::wstring' no matching constructor for initialization of 'std::ostringstream'
我在这里看到许多类似的问题,但所有问题都与前向声明有关,我看不出这与此有什么关系。我对类、结构或函数没有任何问题;甚至在main() 中声明一个变量也是有问题的
编辑:基于this answer,它可能使用VC++ 14.0 作为编译器,但我不知道如何确定
编辑2:错误信息:
1> VisualGDB: Sending 45 updated source files to build machine...
1> VisualGDB: Run "make CONFIG=Debug" in directory "/tmp/VisualGDB/c/Users/pthien/VS/Whatever/Whatever" on pthien@buildserver (SSH)
1> /opt/poky/1.8/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-g++ -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8 --sysroot=/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi -ggdb -ffunction-sections -O0 -std=c++11 -march=armv7-a -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8 --sysroot=/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi -ggdb -ffunction-sections -O0 -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include -I/opt/googletest-master/googletest/include -I/opt/boost_1_61_0 -I../../cereal/cereal-1.2.2/cereal-1.2.2/include -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/websocketpp-master -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include/websocketpp -I/opt/curl-7.54.0 -I/opt/boost_1_61_0/boost -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include/easywsclient-master -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include/uWebSockets-master -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include/c++/4.9.2/experimental -I/opt/boost_1_61_0/boost/tr1/tr1 -I/opt/boost_1_61_0/tools/build/src/engine -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include/c++/4.9.2 -I/opt/curl-7.54.0/include -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include/cereal -I/opt/poky/1.8/sysroots/x86_64-pokysdk-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.2/include-fixed -I/opt/boost_1_61_0/boost/compatibility/cpp_c_headers -I"../../../../../Program -IFiles -I(x86)/Microsoft -IVisual -IStudio -I10.0/VC/crt/src" -I"../../../../../Program -IFiles -I(x86)/Microsoft -IVisual -IStudio -I14.0/VC/include" -I/opt/poky/1.8/sysroots/cortexa8hf-vfp-neon-poky-linux-gnueabi/usr/include/c++/4.9.2/arm-poky-linux-gnueabi -DDEBUG -c Whatever.cpp -o Debug/Whatever.o -MD -MF Debug/Whatever.dep
1> C:\Users\pthien\AppData\Local\VisualGDB\RemoteSourceCache\buildserver\0008\include\wchar.h(459,20): note : 'wcstold'
1> extern long double wcstold (const wchar_t *__restrict __nptr,
1> ^
1> Whatever.cpp: In function 'int main(int, char**)':
1>C:\Users\pthien\VS\Whatever\Whatever\Whatever.cpp(138,47): error : 'strlen' was not declared in this scope
1> size_t request_len = strlen(webSocketRequest);
1> ^
1> Makefile:160: recipe for target 'Debug/Whatever.o' failed
1> make: *** [Debug/Whatever.o] Error 1
1> -------------------------------------------------------------
1> Command exited with code 2
1> Executable: make
1> Arguments: CONFIG=Debug
1> Directory: /tmp/VisualGDB/c/Users/pthien/VS/Whatever/Whatever
1>VisualGDB : error : Command-line action failed
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.MakeFile.Targets(37,5): error MSB3073: The command ""C:\Program Files (x86)\Sysprogs\VisualGDB\\VisualGDB.exe" /build "C:\Users\pthien\VS\Whatever\Whatever\Whatever.vcxproj" "/solution:C:\Users\pthien\VS\Whatever\Whatever.sln" "/config:Debug" "/platform:Win32"" exited with code 1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这大约是输出的 10%,包括开头和结尾。奇怪的是strlen() 的问题。它在 IDE 中没有下划线,所以我不知道它有问题。这是有道理的,因为我注释掉了 <string.h> 和 <cstring> 的 #include 和 strlen() 是其中之一。
【问题讨论】:
-
<string>是要使用的正确标头。如果您对是否应该编译某些东西有疑问,请尝试在像compiler explorer 这样的干净环境中编译有问题的代码。在这种情况下,您的设置似乎有问题。 -
为了确保您使用正确的包含和 lib 路径,请通过“开始”菜单打开 Visual Studio 命令提示符:开始/Visual Studio 2015/Developer 命令提示符。从那里,检查 INCLUDE 和 LIB 路径;它们应该由 VC 位正确地放在前面。在该命令提示符下,键入
devenv以启动 GUI,然后打开您的项目并重试。如果这不起作用,则项目本身可能具有覆盖以包含破坏您构建的路径,而上帝只知道您是如何管理的。 -
还值得仔细检查您的项目属性(属性/VC++ 目录/常规/包含目录)并确保它没有丢失其继承的包含目录(系统是
$(VC_Include_Path)或类似) -
@Mansoor 很酷,感谢您的链接。我也怀疑这与我的设置有关。这就是我标记visual-studio的原因。我无法复制整个文件,因为它位于另一台机器上,但它似乎在该站点上运行良好。
-
@WhozCraig cool 不知道 VS 有一个完整的命令提示符,谢谢。
inspect在命令提示符中无法识别,所以我不确定您所说的检查它们的路径或“VC 位”是什么意思。devenv有效,但项目仍有问题 @ChrisD 包含目录有$(VC_Include_Path);$(WindowsSDK_IncludePath);
标签: c++ visual-studio stdstring