我是 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/