在头文件中声明时未知的类型名

Unknown Typename when declared in header

本文关键字:未知 类型 声明 文件      更新时间:2023-10-16

由于一些奇怪的原因,我收到了一个位于头文件中的typedef void变量的未知类型名。自然地,我在网上搜索,虽然可以注意到我发现了类似的问题,但也应该注意到我没有使用IDE,只有Vim和Clang,没有预编译的头文件。在对ctrie_int头的单独测试中,一切都可以编译,但是当我扩展实现并将其头添加到另一个头的实现文件时,我得到了如下所示的奇怪错误。我相信这是一个简单的问题,但我不确定它是什么,有什么建议吗?

clang++ -Wall -Wextra -g -std=c++11 lzwtest.cpp -o lzwtest dict;
Project Compilation
.
.
.
.
.
Compiling CPP file lzwtest.cpp ...
In file included from lzwtest.cpp:2:
In file included from ./LZW.h:23:
In file included from ./ctrie_int.h:36:
./ctrie_int.ii:7:1: error: unknown type name 'Trie_Int'
Trie_Int * newTrie_Int(int defVal){return new Trie<int>(defVal);}
^
./ctrie_int.ii:7:43: error: cannot initialize return object of type 'int *' with an rvalue of type 'Trie<int> *'
Trie_Int * newTrie_Int(int defVal){return new Trie<int>(defVal);}
                                          ^~~~~~~~~~~~~~~~~~~~~
./ctrie_int.ii:9:21: error: unknown type name 'Trie_Int'
void deleteTrie_Int(Trie_Int * trie){delete ((Trie<int> *)trie);}
                    ^
./ctrie_int.ii:11:19: error: unknown type name 'Trie_Int'
int Trie_Int_size(Trie_Int * t){return ((Trie<int> *)t)->size();}
                  ^
./ctrie_int.ii:13:30: error: unknown type name 'Trie_Int'
int Trie_Int_getDefaultValue(Trie_Int * t){return ((Trie<int> *)t)->getDefaultValue();}
                             ^
./ctrie_int.ii:15:23: error: unknown type name 'Trie_Int'
int Trie_Int_contains(Trie_Int * t,const char * key){return ((Trie<int> *)t)->contains(key); }
                      ^
./ctrie_int.ii:17:18: error: unknown type name 'Trie_Int'
int Trie_Int_get(Trie_Int * t,char * key){return ((Trie<int> *)t)->get(key); }
                 ^
./ctrie_int.ii:19:19: error: unknown type name 'Trie_Int'
void Trie_Int_put(Trie_Int * t,char * s,int val){ ((Trie<int> *)t)->put(s,val);}
                  ^
./ctrie_int.ii:21:37: error: unknown type name 'Trie_Int'
const char * Trie_Int_longestPrefix(Trie_Int * t,char * s){return  ((Trie<int> *)t)->longestPrefix(s).c_str();}
                                    ^
./ctrie_int.ii:23:23: error: unknown type name 'Trie_Int'
int Trie_Int_compress(Trie_Int * t){return  ((Trie<int> *)t)->compress();}
                      ^
10 errors generated.

下面是要包含的文件的头文件

ctrie_int.h

#ifndef COM_WORDGAME_UTILITY_CTRIE_H
#define COM_WORDGAME_UTILITY_CTRIE_H
#ifdef __cplusplus
extern "C"{ //this is used to identify following code as C specific code which will enforce C style name mangling
#endif
//Declare a new void Type To Emulate C class
typedef void Trie_Int;
...Removed in attempt to shorten Question
#ifdef __cplusplus
}
#endif
#endif

这个文件使用前面但甚至简单的代码,或者只是包含最后一个头文件导致开始

中描述的错误。
#ifndef COM_WORDGAME_UTILITY_CTRIE_H
#define COM_WORDGAME_UTILITY_CTRIE_H
#ifdef __cplusplus
extern "C"{ //this is used to identify following code as C specific code which will enforce C style name mangling
#endif
void compress(char src[],char dst[]);
void decompress(char src[],char dst[]);
#ifdef __cplusplus
}
#endif
#endif
//BELOW CODE WILL BE ADDED IN ANOTHER FILE
#include <stdio.h>
#include <stdlib.h>
#include "ctrie_int.h" // This causes an issue cannot find Trie_Int the functions return an odd message describing the Trie_Int was defered to an actual pointer integer

以清晰地显示代码的前几行。它看起来像这样

//#include "ctrie_int.h" //Should not be included would create cycle since ctrie_int.h declares it at the final line 
#include "Trie.hpp" //C++ code
//THIS HAS TO BE COMPILED WITH C++ COMPILER TO BE COMPILED PROPERLY ANYWAY GAURDS UNEEDED
extern "C"{
//Create new Trie_Int Object
Trie_Int * newTrie_Int(int defVal){return new Trie<int>(defVal);}
....Removed In Attempt to shorten question
}

线索在错误消息的第一阶段:

Compiling CPP file lzwtest.cpp ...
In file included from lzwtest.cpp:2:
In file included from ./LZW.h:23:
In file included from ./ctrie_int.h:36:                     <<< Here
./ctrie_int.ii:7:1: error: unknown type name 'Trie_Int'

Trie_Int被定义之前,文件ctrie_int.ii似乎被包含在您的头中。