Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;假设错误C4430 int 我是C++中的新手,C++中只有一个小的头文件,结构简单。_C++_String_Compiler Errors_Int_Identifier - Fatal编程技术网

C++;假设错误C4430 int 我是C++中的新手,C++中只有一个小的头文件,结构简单。

C++;假设错误C4430 int 我是C++中的新手,C++中只有一个小的头文件,结构简单。,c++,string,compiler-errors,int,identifier,C++,String,Compiler Errors,Int,Identifier,PGNFinder.h: #ifndef PGNFINDER_H #define PGNFINDER_H struct Field { int Order; string Name; //more variables but doesn't matter for now }; #endif 这将导致下一个错误: error C2146: syntax error : missing ';' before identifier 'Name' error C443

PGNFinder.h:

#ifndef PGNFINDER_H
#define PGNFINDER_H

struct Field
{
    int Order;
    string Name;
   //more variables but doesn't matter for now
};

#endif
这将导致下一个错误:

error C2146: syntax error : missing ';' before identifier 'Name'    
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
当我将其更改为:

   struct Field
{
    int Order;
    std::string Name;
};
它在.exe文件和.obj文件中给出了一个错误

error LNK1120: 1 unresolved externals   (in the .exe file)
error LNK2019: unresolved external symbol "int __cdecl Convert::stringToInt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?stringToInt@Convert@@YAHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: void __thiscall CAN::calculateMessageLength(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?calculateMessageLength@CAN@@AAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
它给出了与开始时相同的错误。 那么为什么头文件不能识别int和string呢


谢谢你的帮助:)

因为我经常使用评论功能

您的问题是缺少一个include,当您包含string.h时,您仍然忘记了“string类”的std名称空间

所以要么使用
使用名称空间std
(对于初学者来说是最佳实践,因为大多数内容都很可能是std内容)
或者在结构中将字符串声明为std::string。

要将
string
用作变量类型,您需要

  • 包括声明它的标题(
    #include
  • 使用完全限定类型,例如
    std::string
    ,或者通过使用目录
    使用命名空间std但是,请注意,不建议在头文件中使用
如果你只尝试其中的一种,它不会起作用


但是,您的第二条错误消息似乎指向链接器问题。

将其更改为std::string显然可以修复编译器错误


然后出现一个链接器错误,该错误与该行代码无关。您似乎有一个“Convert”类,缺少“stringToInt”函数的实现。

hm也许您应该使用include PLUS std::string name(或
使用名称空间std;
)@Najzero post it as answerIs使用名称空间std;不推荐?并不是说我知道更好的方法:Pdutt是正确的,我没有看到它是一个头文件,这样设置名称空间应该在cpp/cxx onlytanks中完成!但奇怪的是,当另一个错误被修复时,链接器错误出现了。一点也不奇怪。如果源代码未能编译,则通常不会尝试链接它。因为它没有运行构建的那个步骤,所以您不会看到任何错误。
#include <string> 
string Name;