C++假定错误 C4430 int

C++ error C4430 int assumed

本文关键字:C4430 int 错误 C++      更新时间:2023-10-16

我是C++的新手,C++只有一个小的头文件,里面有一个简单的结构。

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)

当我添加时

#include <string> 

并改回

string Name;

它给出了与开始时相同的错误。那么为什么头文件无法识别 int 和字符串呢?

感谢您的帮助:)

为了使用 string 作为变量的类型,您需要

  • 包括声明它的标头 ( #include <string>
  • 使用完全限定类型(如 std::string)或通过 using 目录using namespace std; 但请注意,不建议在头文件中使用 using(请参阅 C++ 头文件中的"使用命名空间")

如果您只尝试其中之一,它将不起作用。

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

因为我倾向于经常使用注释功能。

您的问题是缺少包含,当您包含 string.h 时,您仍然忘记了"字符串类"的 std 命名空间。

所以要么使用using namespace std(对于初学者的最佳实践,因为大多数东西很可能是 std 的东西)或者在结构中将字符串声明为 std::string。

将其

更改为std::string可以清楚地修复编译器错误。

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

相关文章: