在标头中声明时的未知类型名称

时间:2014-06-23 01:46:17

标签: c++ c typename

由于某些奇怪的原因,我收到了位于头文件中的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

为了清楚起见,ctrie.ii的前几行看起来像这样

//#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
}

1 个答案:

答案 0 :(得分:1)

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

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'

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