so many error when i add certain .cpp file - c++

i made small program which does some functions and i tested it and it worked as i wish.
Then i add its .h and .cpp files to another program to use it but when i add the .cpp file it gives me 100s of errors related to boostmultiindex which i use in the added .cpp file.
i do not know what is happening.
the errors include the following"this is sample of errors":
Error C2516 'boost::mpl::if_<C,F1,F2>::type': is not a legal base class
Error C2039 'type': is not a member of 'boost::iterators::iterator_category_to_traversal<int>'
Error C3203 'type': unspecialized class template can't be used as a template argument for template parameter 'U', expected a real type
Error C2653 'safe_mode': is not a class or namespace name
Error C2143 syntax error: missing ',' before '<'
i did not attach code or files because i do not know where to start.
i can update the post based on any guidance.
update:
i think the problem is related to certain header file which i put in it most includes for all my cpp files.
this might lead to cyclic dependency problem which might led to ignoring some includes.
because when i exclude this large include file the errors are much decreased.
i will try to reorder the includes and see if they make difference.
update:
now i commented two lines in the large include file and it removed the errors related to this problem.
the two lines are:
//??#include "modules/utils/utils.h"
//??#include "modules/utils_dst/utils_dst.h"
i do not why these two lines makes these errors
is it related to the fact that the two files are in subdirectories???

Make sure of the following:
When including the .h and .cpp files that you have the full path not just the name of the file.
There are no variables, functions, etc. having the same name/signature in both the files your including and the project file you are including to. If there is, then use namespace to differentiate between conflicts.
You have added all dependencies of the files you want to include to the file you want to include into.

Related

Confused on the locations of class declaration and implementation in c++

I have not been able to find the specific answer to this -- I do know that .h files contain class declarations and .cpp files hold the implementation.
I am confused as to how this is done without repetition.
If I have a class Animal it might be called in several areas of the program. So it would seem I would have to re-write the implementation several times (which no doubt means I am misunderstood).
It seems a bit goofy to include a .h declaration and .cpp implentation with every use of the given class (evidence I am misunderstanding something as well).
Where have I gone wrong in the subject of separating declaration and implementation?
I've been doing PHP and Python all of this time, so it might be prior habits confusing me.
.cpp files are compiled once into object (.o) files.
They are never included into other .cpp files (that would lead to multiple definitions, which are an error in the general case).
Declarations, which are generally retrieved by #includeing header files, are how one .cpp file can know what's defined in some other .cpp file and use it without actually having its definition.
Once all of the .cpp files have been compiled into object files, the linker comes along and links all the object files together to form the final executable or library.
If multiple definitions of the same thing are found, or if one is missing, that's a linker error.

Unknown type name class

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.

tracking down LNK2005: "already defined"

I have been working on a program in windows VC++ 2008. I started by having all of my code in .cpp files (just to get everything working), and now breaking things into .h, and .cpp files. when I compile I get a mountain of LNK2005 errors stating that:
Object.obj : error LNK2005: "__thiscall thing::thing(args)" already defined in otherObject.obj
while I was making the original program I kept getting errors of undeclared identifier, and so I gave a include directive to satisfy that. now when I am breaking everything up into separate .cpp, and .h files I get all of this. which place do I start looking (Object, otherObject, or thing), or somewhere else.
Basically you have definition for thing::thing(args) in two Translation Units(TU), which violates the One Definition Rule(ODR) and hence the error.
The linker exactly tells you which TU's are involved: otherObject.obj and Object.obj.
Start looking in to otherObject.cpp and Object.cpp and the headers which are included in these two cpp files. It is most likely that you have defined your constructor thing::thing(args) in header file and including that header file in both these cpp files results in multiple definitions.
Suggested Solution:
You cannot define the constructor in header file, You need to add it to your cpp file along with other member functions of the class. If you must add the definition of constructor to header you should mark it inline, given that You have not shown your code I don't see any reason to apply the second approach.
Given the information in your question, I bet that the method is defined in a header file but not marked inline. This then causes duplicate symbol linker errors. Try marking hte method inline or moving the definition to a source (.C) file.

Error compiling C++ program

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.

How to fix header file error?

So, my question is how to fix some error in header file, to run program normally? For example I use c++ builder 2010 and when winuser.h file is included, the program always get error like this
Checking project dependencies...
Compiling Project7.cbproj (Debug
configuration) [BCC32 Error]
winuser.h(47): E2257 , expected Full
parser context
File6.cpp(4): #include c:\program files (x86)\embarcadero\rad
studio\7.0\include\winuser.h [BCC32
Error] winuser.h(48): E2257 , expected
Full parser context
i try to replace that file with original from default installation, but that still get same error, how to fix that?
The error is almost certainly caused by whatever code appears before line 4 of File6.cpp. Most likely that is another header file, in which case it is likely that the code therein is malformed - a missing semicolon or brace for example.
The quickest way to verify that winuser.h is not the issue is to change the order of inclusion so that winuser.h is included first.
Another possibility is that something in winuser.h is dependent on some other header not previously included or directly included in winuser.h. Most Win32 API headers are included by windows.h, and it is generally advisable to include windows,h rather than either of its children.
The message is hard to read but the actual error is "E2257 , expected" (coma expected)
From the RAD studio documentation:
A comma was expected in a list of declarations, initializations, or parameters.
This problem is often caused by a missing syntax element earlier in the file
or one of its included headers.
The error message give you the line where it happened and you should probably look before that. There is probably some '}', ')' or ';' or other syntaxic closer missing in your code just before the error (likely before inclusion of the header file in your code). Full error message (you truncated it) or actual code would make it easier to spot.
It is also possible, even if unlikely, that the error is in one of the headers included in winuser.h.