Search code examples
c++ctypename

Unknown Typename when declared in header


For some odd reason I recieve unknown type name for a typedef void variable located in a header file. Naturally i searched around the net and while it can be noted that I found similar issues it should also be noted that I am not using an IDE only Vim and Clang and don't have precompiled headers. In a separate test for the ctrie_int header, everything compiles but when I extend the implementation adding its header to the implementation file of another header I get the weird error seen below. I'm sure its a simple issue but i'm not sure what it is, any suggestions?

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.

Below is the header for the file being included

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

This file uses the previous but even simple code or just the inclusion of the header file of the last causes the error described in the beginning

#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

for clarity the first few lines of ctrie.ii looks like this

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

Solution

  • The clue is in the first stages of the error message:

    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'
    

    It appears that the file ctrie_int.ii is being included from your header before Trie_Int has been defined.