I get this error while compiling:
error: aggregate 'X x' has incomplete type and cannot be defined
I have three classes in 6 different files(header file + 3 implementation files). when I try to compile all these classes with a main, It gives me the above error. I am not including any header file in other header files, I am doing that in implementation files. So, I think its not a case of "cross reference". I am not sure what is the problem with my code can anyone help me in that?
Thanks
Somehow, that class isn't being defined when it needs to be.
Firstly, make sure the header has actually been included. Further, make sure you have include guards, and that you don't have circular includes and recursive definitions. Aside from that, without the code we cannot give specifics.
Related
This question is about something that after more than a year with C++ I can't solve or find any solution about it.
I got used to using separate files for headers and code in C, but I have a problem with it on C++: whenever I edit a header file and try to compile the code that uses it again, the compiler doesn't notice the change on the header.
What I do to solve this is "compiling" the header (.hpp) alone. Sometimes I just add it to the list of source files for g++ along with the rest of the code, but what happens then is that I have to execute the command twice (the first time it gives me errors, but not the second time). It also warns me that I'm using the "pragma once" option in a main file.
I know this is very wrong, so I've searched for a correct way to do this, without success. I have noticed that g++ generates ".gch" files but I don't really know what's their purpose, although they may be related.
I suspect that the problem is caused because of the code in the ".hpp". I know (I think) that the good way to do it is to define prototypes only inside the header and writing the body of the methods in a separate file, but sometimes (specially when using templates) this generates even more problems.
The .gch is a precompiled header and it is created if you explicitly compile a header file.
The compiler will then use that file instead of the actual header (the compiler does not care about modification timestamps).
Do rm *.gch and leave all headers out of the compilation command forever.
(And don't put template implementations in .cpp files.)
I try to include headers from a library in different files of my project and I get multiple definition errors on some functions of the library.
After reading the answer to this question I think the problem is that the functions are implemented directly in the header files of the library.
In particular I want to include the files codecfactory.h and deltautil.h from FastPFor. I don't know if it is relevant for my problem but I include it into my cmake project with this code in my CMakeLists.txt:
include_directories(../../FastPFor/headers)
add_library(FastPFor STATIC ../../FastPFor/src/bitpacking.cpp
../../FastPFor/src/bitpacking.cpp
../../FastPFor/src/bitpackingaligned.cpp
../../FastPFor/src/bitpackingunaligned.cpp
../../FastPFor/src/horizontalbitpacking.cpp
../../FastPFor/src/simdunalignedbitpacking.cpp
../../FastPFor/src/simdbitpacking.cpp
${HEADERS}
)
Everything works fine if I just include the files once. But as soon as I use them in two .cpp files I get these kinds of errors:
CMakeFiles/dbgen.bin.dir/queries/Query5.cpp.o: In function `vsencoding::BitsWriter::BitsWriter(unsigned int*)':
Query5.cpp:(.text+0x8420): multiple definition of `vsencoding::BitsWriter::BitsWriter(unsigned int*)'
CMakeFiles/dbgen.bin.dir/queries/Query13Naive.cpp.o:Query13Naive.cpp:(.text+0x7a50): first defined here
Is there any way I can fix this without having to change the FastPFor code but only my own?
The question you linked to says it all - there is no way to solve this without modifying the headers (or just include them in only one source file).
For instance this line defines a non-inline constructor in a header. Including it in more than one translation unit would result in a violation of the ODR rule.
One way to workaround this would be to change your project to header only style, i.e. moving your implementation to header files. In this way you can keep (more ore less) the structure of your project.
However, this is definitely not a nice solution... The whole project needs to be compiled after every small change to one of the headers...
I have the following header files:
https://gist.github.com/wemakeweb/5501443
and the compiler always reports "Unknown Type name Class". I have included Forward Declaration, to break circular including , where i think i have to. What did i forget?
Edit: i put it all in one header file, and the compiler still reports "expected ; after top level declarator"
https://gist.github.com/wemakeweb/5583500
Edit 2
Now im getting linker errors. "Undefined symbols for architecture x86_64"
Solved, Problems were
Circular Including
main.c instead of main.cpp
the actual code was in a static lib which was not linked properly
This error? error: unknown type name ‘class’
You're probably compiling it as C rather than C++.
Make sure your source file has a .cpp extension, and than any relevant compiler flags are set correctly. (It helps if you include the exact error message and line numbers. Don't try and retype, just cut+paste.)
You have at least one cyclic include dependency between Feld.h and Figur.h. The forward declarations have no effect if you also include the headers. Just remove the includes.
I have the following files:
listDriverTest.cpp
src/List.cpp
headers/List.h
The include in List.cpp is
#include "../headers/List.h"
The include in listDriverTest.cpp is
#include "headers/List.h"
When I compile with the following statement,
g++ listDriverTest.cpp "src/List.cpp"
I end up with a fair number of 'undefined reference' errors, e.g.
listDriverTest.cpp:(.text+0x81): undefined reference to `List<int>::List()'
listDriverTest.cpp:(.text+0x8f): undefined reference to `List<int>::add(int)'
listDriverTest.cpp:(.text+0x9d): undefined reference to `List<int>::add(int)'
...
How should I properly use includes and compile these three files in order for the compilation to work properly? I have gotten listDriverTest.cpp to compile and run properly with all the files in the same directory, but not when they're broken up like this.
See my answer in Must a child of a template class also be a template class?.
It is probably a different question, but the same answer applies.
It looks like the object file produced by compiling src/List.cpp already contains the specialization List, but it's in a different directory than the object file of listDriversTest.cpp. Hence, the linker cannot find it.
Of course, this depends on how you've organized your template code.
Your program compiled properly on my machine.
Just remove the double quotes around src/List.cpp
I think your problem is something else.
I added a function void list(void) in list.cpp which printed "list".
The same signature was added to list.h.
I'm compiling some code that relies on include guards to prevent multiple definitions of objects and functions, but Visual Studio 2008 is giving me link errors that there are multiple definitions. I don't understand why because I've used code very similar to this before and it hasn't caused problems. I must be doing something dumb but I have no idea what it is. I also tried to take out the include guards and use #pragma once, but I get the same link errors. What should I check for?
If they are linker errors, the most likely cause is probably non-inline functions defined in the header.
If you have a non-inline function in a header that is included in more than one source file, it will be defined in each of those source files ("translation units"), thus the function will be defined more than once, hence the multiple definitions error.
If you're getting linker errors... are you sure you're not 1) actually defining a function twice in code or 2) trying to do something silly like #include a source file (as opposed to a header file)?
This can also be caused by using different versions of the cstd lib from other library's linked in. Check under the c++/Code generation section and make sure all your projects are using the same settings.