C++ 错误 C4430 int 假定

标签 c++ string compiler-errors int identifier

我是 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呢?

感谢您的帮助:)

最佳答案

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

  • 包含声明它的 header ( #include <string> )
  • 使用完全限定类型,例如 std::string或通过使用目录 using namespace std;但是请注意,using不建议在头文件中使用(参见 "using namespace" in c++ headers)

如果你只尝试其中的一种,它是行不通的。

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

关于C++ 错误 C4430 int 假定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13875625/

相关文章:

c++ - Windows 触发了 c 中的断点?

c++ - 尝试在 C++ 中减少设计模式

javascript - 从带有 Node 的字符串中提取城市名称

java - 如果 Java 打印 NUL 字符怎么办?

c - 我该如何解决这个错误? (未定义对 'display_menu' 的引用)

java - 为什么我的构造函数出现非静态变量错误?

c++ - 哪种方法更适合为函数提供编译时间常量?函数参数与模板参数

c++ - 混合编程 - 包括 C++ 头文件到 Fortran

Java - 全名的正则表达式

ubuntu - 鲁本图/bin/sh : 2: xtensa-lx106-elf-gcc: not found