Cannot compile project: error in locale.h file - c++

I'm trying to compile a project that has the following header:locale.h;
locale.h:
class LOG4CXX_EXPORT Locale
{
public:
...
protected:
Locale(const Locale&);
Locale& operator=(const Locale&);
const LogString language; <-- error
const LogString country; <-- error
const LogString variant; <-- error
}; // class Locale
Could anyone give me some suggestions ?
I'm getting this error. I am not sure
what is the problem.
/LOGGER/include/log4cxx/helpers/locale.h:42:41: error: field ‘language’ has incomplete type
const LogString language;
^
/LOGGER/include/log4cxx/helpers/locale.h:43:41: error: field ‘country’ has incomplete type
const LogString country;
^
/LOGGER/include/log4cxx/helpers/locale.h:44:41: error: field ‘variant’ has incomplete type

Consider the following code:
class MyClass;
int method1(const MyClass& param);
MyClass& method2();
const MyClass instance; // <- error here
The declaration of MyClass is a forward declaration. All the compiler know is that the class exists (it doesn't know its members, size...), that's why it is called an incomplete type. You can use references or pointers of that class, but that's it. See more info here When can I use a forward declaration?
So it seems that in your code, you only have a forward declaration of LogString type, and not a full declaration. Check your include files and include order so you get the full declaration of this class.

You are using std::basic_string, but there is no include for the appropriate header file:
#include <string>

What AnT wrote in a comment is the solution to the problem:
<clocale> is including your <locale.h> instead of the system one it ought to do; your locale is trying to include <string>, which again includes <clocale>.
So in the end, you get a circular include as I described in your other question https://stackoverflow.com/questions/32379927/header-file-does-not-compile-locale-h, just the chain being longer...
You need to break this inclusion circle. You can do this by removing the directory the file resides in from the inclusion directories you pass to gcc (I suppose this is -I"/LOGGER/include/log4cxx/helpers"). Instead, you can give a path to the parent directory (-I"/LOGGER/include/"). Instead of #include <locale.h> you would have to use #include <log4cxx/helpers/locale.h>.
Actually, I recommend keeping "/LOGGER/include" as the only directory you give gcc and have all other files you need included via the corresponding subpath - provided the rest of the log4cxx files allow that (which I would assume).
Apart from that, the only other way to solve the problem is indeed renaming your 'locale.h' file to something else (apart from changine the include path division, such as -I"/LOGGER/include/log4cxx" and #include <helpers/locale.h>; the one I chose, however, is the most natural one IMO).

Related

Using multiple structs through header files C++

Apologies if you have seen this question before however it has yet to be answered, essentially in my code I have two structs, defined in separate headers and used globally throughout the project. I simply wish to use both structs (which again, are defined in two separate headers) in other cpp files than just the ones that the header file belongs to.
Here is some sample code which I have tested:
class1.h
#include "class2.h"
#include <vector>
#include <string>
struct trans1{
string name;
};
class class1 {
private:
vector <trans2> t2;
public:
class1();
};
class2.h
#include "class1.h"
#include <vector>
#include <string>
struct trans2{
string type;
};
class class2{
private:
vector <trans1> t1;
public:
class2();
};
errorlog:
In file included from class1.h:3:0,
from class1.cpp:1:
class2.h:21:13: error: 'trans1' was not declared in this scope
vector <trans1> t1;
^
class2.h:21:19: error: template argument 1 is invalid
vector <trans1> t1;
^
class2.h:21:19: error: template argument 2 is invalid
I understand that this is ridiculous code in a real world application however this is the simplest way I could demonstrate.
It is worth noting that if I simply comment out the declaration of vector t1 or t2 under 'private:' the code compiles without fail. It is just the fact I am using a second struct.
Any help anyone? Thanks.
Simply forward-declare the classes that will be used. Put all implementation code into a cpp file, not inline in the header.
Make the vector private. This way no file that includes the header can force code generation against an incomplete class.
you can try to forward declare trans1 in class2.h and trans2 in class1.h like this:
class2.h :
// includes
struct trans1;
// rest of your code
the same thing (but with trans2) in class1.h
Don't forget to add Include guards in your code!
edit: and yes, you need to change your vectors to store pointers, otherwise it won't link
You need to put the "trans" structs in their own header file(s) and include them in your class header files.
You could forward declare them, but this would require changing your vector to use pointers. (In that case I would recommend std::vector<std::unique_ptr<trans>>). This could be appropriate if your structs are big and complex.
The main advantage of the forward-declaration approach is to reduce compile times. However if the structs are really so simple as in your example, I wouldn't bother with the extra overhead of using pointers here.
If You were to do this in single .cpp file, the solution would be trivial:
struct trans1 { ... };
struct trans2 { ... };
class class1 { ... };
class class2 { .... };
Now you just need to rearrange the code to get this result in every translation unit. (the order of classes/structs in the file is important)

enum header file not find in c++

I have a file called playback_type.h with only this code in it:
#include <iostream>
enum playback_type {
NOTE_PB, SONG_PB
};
Xcode lets me include the file fine, it even autocompletes the filename but when I try to build it I'm getting all sorts of errors.
#include <playback_type.h> // Error: `playback_type.h` file not found
class PlaybackHelper{
private:
// Singleton methods
PlaybackHelper();
PlaybackHelper(PlaybackHelper const&);
void operator=(PlaybackHelper const&);
playback_type type; // Error: 'playback_type' does not name a type
public:
void setPlaybackType(playback_type aType); // Error: 'playback_type' has not been defined
//singletong method
static PlaybackHelper &getInstance();
}
Any ideas why I'm getting those erros? The .h file is included correctly, xcode helps me autocomplete it so it should be there.
Angle brackets (<>) are used do indicate system headers, and quotes ("") to indicate local headers. Usually, the preprocessor will look for local headers in your project directory, but won't look for system headers there unless you specifically tell it to. So you should use quotes for your own headers:
#include "playback_type.h"
Just replace with #include "playback_type.h"

Syntax error missing ; before *

I have a header file like so:
#pragma once
#include "gamestate.h"
#include "ExitListener.h"
class InitialGameState : public GameState
{
public:
InitialGameState(Ogre::Camera *cam, Ogre::SceneManager *sceneMgr, OIS::Keyboard *keyboard, OIS::Mouse *mouse, Ogre::Root *root);
~InitialGameState(void);
virtual bool update(Ogre::Real time);
virtual void pause(void);
virtual void start(void);
void keyPressed(const OIS::KeyEvent &e);
void keyReleased(const OIS::KeyEvent &e);
//private:
ExitListener *mFrameListener;
};
The problem with this is that I get the following errors from VC 8:
InitialGameState.h(16) : error C2143: syntax error : missing ';' before '*'
InitialGameState.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
InitialGameState.h(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(they all refer to the last line)
I have a class ExitListener.h which is why I don't get the errors
Edit: ExitListener.h:
#pragma once
#include <Ogre.h>
#include <OIS/OIS.h>
#include <CEGUI/CEGUI.h>
#include <OgreCEGUIRenderer.h>
#include "Thing.h"
#include "InitialGameState.h"
using namespace Ogre;
class ExitListener : public FrameListener, public OIS::KeyListener, public OIS::MouseListener
{
public:
ExitListener(OIS::Keyboard *keyboard, OIS::Mouse *mouse, Camera *cam, std::vector<Thing*> &vec):
mKeyboard(keyboard), r(0.09), mContinue(true), mRunningAnimation(false),
mMouse(mouse), mYaw(0), mPitch(0), things(vec), mCamera(cam), mWDown(false), mSDown(false), mADown(false),
mDDown(false)
{
things = vec;
mKeyboard->setEventCallback(this);
mMouse->setEventCallback(this);
}
bool frameStarted(const FrameEvent& evt);
bool keyPressed(const OIS::KeyEvent &e);
bool keyReleased(const OIS::KeyEvent &e);
bool mouseMoved(const OIS::MouseEvent &e);
bool mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id);
bool mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id);
void setOwner(GameState *g);
private:
AnimationState *mSwim;
Radian r;
Radian mYaw;
Radian mPitch;
OIS::Keyboard *mKeyboard;
OIS::Mouse *mMouse;
Camera *mCamera;
bool mContinue;
bool mRunningAnimation;
std::vector<Thing*> &things;
bool mWDown;
bool mADown;
bool mDDown;
bool mSDown;
GameState *mOwner;
};
Edit 2:
It turned out that the problem could be solved by a forward declaration and then including the other header directly in my .cpp file.
Thanks.
My guess is that ExitListener.h is including InitialGameState.h header file either directly or indirectly. So there is a circular dependency between the header file and compiler is unable to find the declaration for ExitListener. If you just need to store the pointer of ExitListener in this class then there is no need to include the ExitListener.h header file. Instead you can just use the forward declaration as class ExitListener;
EDIT: You can use the forward declaration as suggested above, or remove the InitialGameState.h include from ExitListener.h . You need to include GameState.h (the base class header file) only. But I prefer to use the forward declarations in header file and include the header file only in cpp.
The errors don't refer to the last line, but to the line before the last line. (Please be precise. If people know the compiler well which emits this error message, their guesses might be a lot better if they know the exact line it is given for.)
"ExitListener.h" is not a class, but a header. (This isn't Java.) One would assume that there is a class ExitListener defined (or at least declared) inside that header, but there could just as well be some other class, none at all, or many classes.
Without this header, it's impossible to say exactly what's wrong, although either circular dependencies between these two headers or a missing ; at the end of the ExitListener class' definition is a very good guess that fits my experience with such errors. At the very least I'm sure the error means that the compiler doesn't know what ExitListener is.
As others have said, you do not need a class definition in order to declare a pointer to that class, so (assuming that "ExitListener.h" defines the ExitListener class) you don't need to include the header at all. A simple forward declaration class ExitListener; is sufficient enough to declare the ExitListener *mFrameListener member. (You will need to include the full class definition in order to implement the InitialGameState member functions that deal with ExitListener, though. If you implement these functions in the header where InitialGameState is defined, you will need to keep that "ExitListener.h" include.)
Apparently the problem is with ExitListener definition, it's not considered valid at that point.
The error is in the ExitListener.h file (or any of the file it includes). Often this problem is due to a missing ; at the end of the class.
If you add the code of this file I will be able to help you further.
The problem
ExitListener was incorrectly declared. This is the only solution for VS to say this.
Check that there was no error when compiling the ExitListener class. (and that you did not forget the trailing ";")
A side note
Here you are using a pointer to ExitListener. You do not need to know the size or internal layout of ExitListener if you just declare a pointer.
A forward declaration would be enough.
I suspect you are missing the Ogre includes somewhere on your include chain.
My assumption is based on the little knowledge I have about your other header files and VC alerting that it's missing a type specifier: missing type specifier.

Qt Creator: “XYZ does not name a type”

This is a very frustrating error message in Qt Creator: ’XYZ’ does not name a type. This usually means that there is an error in the class XYZ that prevents the compiler from generating the type, but there are no additional hints as to what went wrong.
Any suggestions?
I found this problem on qtcreator 3.4.1 and QT 5.4, when I replace such as
#include <QTextEdit>
with
class QTextEdit;
this problem gone.
i just had this problem, and like Arckaroph have said :
the problem is that when we include a header file in a source code file, and we use in it the directive #ifndef, we can't include it again in a header file to give a type of it's included class to a variable in source code file
example :
class1.h contains Class1
class2.h contains Class2
class2 have a private variable V with class1 type
if we include class1.h in class2.CPP we can't include it in class2.h to give V a class1 type.
so we put in class2.cpp class2.h before class1.h
or we delete class1.h from class2.cpp
In your abc.cpp make sure you include xyz.h before including abc.h.
No idea why swapping the two around would make a difference however it did for me.
Do you get the error from the compiler or the IDE (as a squiggly underline)? I've encountered this in Qt Creator 1.2.9 and I think it's a bug in the IDE.
I believe you are declaring something of type XYZ such as
XYZ foo;
The problem is XYZ is not yet defined.
The following is my problem and my conclusion. What do you think?
My problem is I have a class ABC and a class XYZ. Class ABC has a member that is declared as a XYZ type. Class XYZ has a member that is declared as a ABC type. The compiler doesn't know what the XYZ type is yet because it has not defined it yet. Therefore the error given is 'XYZ' does not name a type.
Example Code:
class ABC{
private:
XYZ *xyz; //XYZ is not defined yet
};
class XYZ{
private:
ABC *abc; //ABC is defined above
};
I found a solution for myself.
Say I have class A and class B.
"A.h" includes "B.h" and has instance of B as member.
"B.h" includes "A.h" and has instance of A as member.
Compiler gives me an error in "B.h" on line of code where member of class A is declared:
"A doesn't name type"
What I do is in "A.h" I remove #include "B.h" and place #include "B.h" to "A.cpp". And before A class declaration I write class B;
...
// #include "B.h"
class B;
class A
{
...
B m_b;
};
...
Worked for me, Good luck!
in a recent QT project, where I had just installed the most recent QT (3/2011), I cured the three of these that was stopping my build by adding this...
#include <sys/types.h>
...prior to the include of the header file that was throwing the errors. That did it.
I don't know why they would distribute something that had such problems, perhaps in other systems types.h is included with something else, but not in my case, anyway. Hope that helps someone.
Does #include'ing the corresponding header file help?
If you're using templates, then you need to precede the class name with "typename" so that the compiler can recognize it as a type...
template <typename t> //...
Two possibilities occur to me:
1. Perhaps you have SLOT instead of SIGNAL in a connect() call.
2. Sometimes it helps to make a gratuitous edit to the .PRO file (e.g. insert and delete a space), so that QMake gets run and the .moc files get generated.
In my case I wasn't using the namespace the class was defined in. Contents of the header were contained within the namespace, but the source file was missing the using namespace directive.
.h:
namespace mynamespace {
class MyClass : public QWidget
{
...
}
}
.cpp:
using namespace mynamespace
MyClass::MyClass(QWidget *parent) : QWidget(parent)
{
}

Does Not Name A Type in C++

in C++ when i get an error that says xxxxx does not name a type in yyy.h
What does that mean?
yyy.h has included the header that xxxx is in.
Example, I use:
typedef CP_M_ReferenceCounted FxRC;
and I have included CP_M_ReferenceCounted.h in yyy.h
I am missing some basic understanding, what is it?
That seems you need to refer to the namespace accordingly. For example, the following yyy.h and test.cpp have the same problem as yours:
//yyy.h
#ifndef YYY_H__
#define YYY_H__
namespace Yyy {
class CP_M_ReferenceCounted
{
};
}
#endif
//test.cpp
#include "yyy.h"
typedef CP_M_ReferenceCounted FxRC;
int main(int argc, char **argv)
{
return 0;
}
The error would be
...error: CP_M_ReferenceCounted does not name a type
But add a line "using namespace Yyy;" fixes the problem as below:
//test.cpp
#include "yyy.h"
// add this line
using namespace Yyy;
typedef CP_M_ReferenceCounted FxRC;
...
So please check the namespace scope in your .h headers.
The inclusion of the CP_M_ReferenceCounted type is probably lexically AFTER the typedef... can you link to the two files directly, or reproduce the problem in a simple sample?
Although possibly unrelated to OP's original question... this is an error I just had and shows how this error could occur.
When you define a type in a C++ class and you return it, you need to specify the class in which the type belongs.
For example:
class ClassName{
public:
typedef vector<int> TypeName;
TypeName GetData();
};
Then GetData() must be defined as:
ClassName::TypeName ClassName::GetData(){...}
not
TypeName ClassName::GetData(){...}
Otherwise the compiler will come back with the error:
error: 'TypeName' does not name a type
Yes, you should try to check the namespace first.
Be sure you didn't copy paste this yyy.h file from some other class header and keep the same "YYY_H__" in the new #ifndef as in the original file. I just did this and realized my mistake. Make sure to update your new YYY_H__ to represent the new class. Otherwise, obviously, YYY_H__ will already be defined from the original file and the important stuff in this header will be skipped.