enum header file not find in c++ - 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"

Related

Cannot compile project: error in locale.h file

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).

Namespace has not been declared

I have a namespace like:
HW.h
#include <select.h>
namespace Hw
{
void setInput(uint8_t type, uint8_t input, ESelect select);
void setParam(uint8_t param, ESelect select);
}
select.h
enum class ESelect
{
Select0,
Select1,
Select2
}
Both of the above are in the same static library. I try to call this from another static library, like this.
Test.cpp
#include<HW.h>
#include<select.h>
Hw::setInput( 0, 2, ESelect::Select0 );
I get the error:
error: ‘Hw’ has not been declared
error: ‘ESelect’ has not been declared
What can be wrong?
Using #include <some_header.h> causes the compiler to search the system include directories before any user directories. Many *nix systems have a system header called select.h already, so you are probably including that instead of your own select.h.
Change all occurrences of:
#include <select.h>
to:
#include "select.h"
Ditto for #include <HW.h>.
Ideally you should not use system header names for your own headers, and you should always use "" for user headers and <> for system headers.
For future reference, a useful technique for debugging such problems is to use g++ -E ... or equivalent to see what headers are actually being included.
Looks like you do not have
#include "HW.h"
in Test.cpp

Difference between two header orderings in C++ seems to generate error

I have a class called File that is defined (along with other classes) in the header "dmanager1.h". In the "dmanager1.cpp" file (implementation for the dmanager1.h file), when I list the headers in one order I get an error when trying to compile along with my main.cpp (main.cpp is empty except for the header call and an empty "int main()"...basically I'm just testing the class .h and .cpp files)... If I switch the headers around in the dmanager1.cpp file I get no errors. I don't understand what is happening. The error I'm getting is:
error: 'File' does not name a type
I get said error when I have my header's ordered in my "dmanager1.cpp" as follows:
#include "dmanager1.h"
#include <iostream>
#include <cstring>
If I switch the header's around to:
#include <iostream>
#include <cstring>
#include "dmanager1.h"
...I don't get the compilation error. Is the first order getting parsed funny? Any thoughts would be greatly appreciated.
EDIT: Added part of the header in question...
#ifndef _dmanager1_h
#define _dmanager1_h
//--------------------
// Forward References
//--------------------
// Node_L, Node_T, and Sector are defined in File: dmanager1a.h
class Node_L;
class Node_T;
class Sector;
class File
{
public:
// Default Constructor
//File();
// Constructor: Allowing "name", "size", and/or "permissions" to be set
// Permissions set to default of 0 == read and write
File(const char * & name, float size = 0, int permissions = 0) : timestamp(11223333) {};
// Default Destructor
~File();
//returns an int corresponding to the date modified (mmddyy)
int get_date_mod(void) const {return timestamp;}
// Return's current level of permission on the File: 0 = read/write, 1 = read only
int get_permission(void) const {return permission;}
// Set's Permission to "level": 0 = read/write, 1 = read only
int set_permission(int level);
private:
// Data members
char * name;
float size_OA;
//function used to update "date modified"
void update_timestamp(void);
// Current permission level of the file: 0 = read/write, 1 = read only
int permission;
//value modified by update_timestamp() and the value returned by get_date_mod(). Date file last edited.
int timestamp;
};
Most likely your dmanager1.h header needs something that iostream or cstring define.
As a result, it doesn't get parsed correctly, and the compiler doesn't understand the declaration of your File class.
If you post your dmanager1.h file, you'll be able to get a more detailed answer.
Make sure that each of your headers is completely self-sufficient. It needs to #include headers for everything that it uses and not assume that they will be included by something else. Every header should work even if it is the only header that a .c file includes.
I'm betting that your dmanager1.h header is using something from the standard library and you aren't including the header that it needs. Swapping the header appears to fix the problem, but it's only working by coincidence.
One diagnostic test you can do is to create a .c file that contains nothing but the line #include "dmanager1.h". Try to compile it. If the compiler throws an error, it should provide hints as to which additional headers need to be included.
Update: I can compile using the initial portion of the header that you posted using g++ -Wall and I get no errors or warnings at all. Please post a sample that reproduces the problem.

Proper implementation of global configuration

My goal is to have global constants in a C++ game I'm working on (to represent some graphics info and the like). My current implementation is to toss them all in a .h and include them everywhere. This works, except that every time I change a setting, the entire code base must be recompiled.
So, my next idea was to toss them in some configuration txt file and parse them in, that way no code is actually changed when settings change. The parser was simple enough, and I could put the values into the constants, but because the parser was a code block, the constants were no longer global.
Is there a good way to solve this? Perhaps some way to make them global despite being in a block or some way to avoid recompiling everything when changing settings?
The way I used solve this is to put the variables in a separate global namespace, which is in a header file named something like config.h, then include that file everywhere.
// In config.h
#ifndef CONFIG_H
#define CONFIG_H
namespace config
{
extern int some_config_int;
extern std::string some_config_string;
bool load_config_file();
}
#endif
Then in a source file, you define the variable and also set them to a default value. This source file also have the code to load the variables from your configuration file.
// In config.cpp
namespace config
{
int some_config_int = 123;
std::string some_config_string = "foo";
}
bool config::load_config_file()
{
// Code to load and set the configuration variables
}
Now in every source file you need the configuration variables, include config.h and access them like config::some_config_int.
However, there is no "proper" way of solving this, all ways that work are proper in my eyes.
Another way to do this would be to create a singleton class.
#include <fstream>
#include <map>
#include <string>
class ConfigStore
{
public:
static ConfigStore& get()
{
static ConfigStore instance;
return instance;
}
void parseFile(std::ifstream& inStream);
template<typename _T>
_T getValue(std::string key);
private:
ConfigStore(){};
ConfigStore(const ConfigStore&);
ConfigStore& operator=(const ConfigStore&);
std::map<std::string,std::string> storedConfig;
};
Here the configuration is saved in a map, meaning as long as parseFile can read the file and getValue can parse the type there is no need to recompile the config class if you add new keys.
Usage:
std::ifstream input("somefile.txt");
ConfigStore::get().parseFile(input);
std::cout<<ConfigStore::get().getValue<std::string>(std::string("thing"))<<std::endl;
What about creating functions that return your constants that you can specify in a .cxx file? For example:
// foo.h
const int BAR();
// foo.cxx
const int BAR() {
return 10;
};
put only the declarations in head file and put the definitions in a cpp file. then you change the definitions in cpp file will not cause all code recompiled

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.