I have divided my project into this source folders:
src/
src/THREADS
src/UKF_IMU+GPS
src/UKF_WCT
I have declared the structure in this file "src/UKF_IMU+GPS/main_general.h"
main_general.h
//Structure Declarations
struct FD
{
int IMU, GPS;
};
struct S_POS
{
double X=0, Y=0, Z=0;
double Latitude=0, Longitude=0, Altitude=0;
};
struct S_IMU
{
double ACCX=0, ACCY=0, ACCZ=0, GYRX=0, GYRY=0, GYRZ=0;
double ACCX_Bias=0, ACCY_Bias=0, ACCZ_Bias=1, GYRX_Bias=0, GYRY_Bias=0, GYRZ_Bias=0;
double Pitch=0, Roll=0, Yaw=0;
//Variables to Calculate AT
double AT=0;
unsigned int Time=0;
};
struct S_GPS
{
int Date=0, TimeHour=0, NumSatUsed=0;
double Yaw=0, Velocity, Vel_X=0, Vel_Y=0, Vel_Z=0;
double Std_Dev_Lat=0, Std_Dev_Lon=0, Std_Dev_Alt=0;
double HDOP=0;
//Variables to Calculate AT
double AT=0;
unsigned int Time=0;
};
Then, I have declared an structure global variable object in "/src/THREADS/IMUandGPS.cpp"
IMUandGPS.cpp
#include "../UKF_IMU+GPS/main_general.h"
/* Global variables */
struct S_POS POS_Snapshot;
struct S_IMU IMU_Snapshot;
struct S_GPS GPS_Snapshot;
I do some stuff with the structure and it works perfect.
I also use the same global object in the other file "/src/THREADS/Write_IMUAndGPS_OF.cpp"
Write_IMUAndGPS_OF.cpp
#include "../UKF_IMU+GPS/main_general.h"
/* External Global variables */
extern struct S_POS POS_Snapshot;
extern struct S_IMU IMU_Snapshot;
extern struct S_GPS GPS_Snapshot;
I do some stuff with the structure and it works perfect as well.
The problem comes here, I have to use the POS global structure in this file:
"/src/src/UKF_WCT/UKF_Algorithm.cpp"
UKF_Algorithm.cpp
#include "../UKF_IMU+GPS/main_general.h"
/* External Global variables */
extern struct S_POS POS_Snapshot;
PosX = POS_Snapshot.Latitude;
PosY = POS_Snapshot.Longitude;
PosZ = POS_Snapshot.Altitude;
Includes and everything is the same but the compiler gives me an error:
forward declaration of 'struct S_POS' UKF_Algorithm.cpp
invalid use of incomplete type 'struct S_POS' UKF_Algorithm.cpp
invalid use of incomplete type 'struct S_POS' UKF_Algorithm.cpp
invalid use of incomplete type 'struct S_POS' UKF_Algorithm.cpp
PosX,Y and Z are also double so is not type problem...Why could be this problem?
I have already solved it!
The problem was that I had another main_general.h file in "src/UKF_WCT/main_general.h" so the compiler found this file instead of "/UKF_IMU+GPS/main_general.h".
I have changed the name of the file and it works perfect!
Related
I have a header file in a project where I am declaring a number of structs. Some of the latter structs have members that are the types of earlier declared structs. I am aware that these member structs must be declared before they are used but I have encountered an odd behaviour.
In the file below I tried to add a new member to the second struct (LUT) of type Coordinate. As yo can see the bottom struct libraryEntry has a similar member; this has been the case for a while and has caused no problems.
#ifndef STRUCTS_H
#define STRUCTS_H
#pragma once
#include <string>
#include "Enums.h"
struct Coordinate {
int X;
int Y;
Coordinate(int x, int y) : X(x), Y(y) {}
};
struct LUT {
int offset;
std::string hexCode;
bool modifiedByTrojan;
//Coordinate xyCoordinate; <======= Causes build to fail when uncommented
};
struct libraryEntry {
int id;
DeviceType deviceType;
int offSet;
Coordinate xyCoordinate;
std::string hexCode;
DeviceConfiguration deviceConfig;
libraryEntry(int idNum, DeviceType deviceType, int offSet, Coordinate xyCoordinate, std::string hexCode) :
id(idNum),
deviceType(deviceType),
offSet(offSet),
xyCoordinate(xyCoordinate),
hexCode(hexCode)
{
}
};
#endif
Adding the Coordinate member above causes the error:
'LUT::LUT(void)':attempting to refernce a deleted function
Why is this only happening in the second struct?
In the struct that works (libraryEntry), you have defined a constructor, and in your constructor you are initialising xyCoordinate using its two-arg constructor.
In the struct that fails to compile, you haven't defined any constructors, so you get the default no-arg constructor, which initialises everything, including your Coordinate member, using their default (no-arg) constructors. Coordinate doesn't have a default constructor because you declared a different constructor and that causes the default constructor to be deleted, hence your error message.
I am trying to reach a static variable declared in MyClass.h from MyClass.cpp. But I get following errors.
I made a research but still have no clue why my code does not compile. I use visual studio 2013.
MyClass.h
#ifndef __MyClass_h_
#define __MyClass_h_
class MyClass {
static int x;
public:
static int y;
};
#endif
MyClass.cpp
#include "MyClass.h"
void MyClass::sor() (const string& var1, const unsigned count) const {
// What goes here? See below for what I have tried
}
So, if I use:
int MyClass::x=8;
This says int MyClass::x redefinition and MyClass::x 'MyClass::x' : definition or redeclaration illegal in current scope
If I use:
MyClass::x=8;
This gives the error 1 unresolved external.
If I use:
MyClass::y=8;
This also gives the error 1 unresolved external.
If I use:
int MyClass::y=8;
This says int MyClass::y redefinition and 'MyClass::y' : definition or redeclaration illegal in current scope
You need to understand you don't have a static variable in a header, how other answers suggest. You have a static member of a class, which is perfectly fine.
In order to access it you write: MyClass::x. You need to initialize it also.
Unrelated to the static member, you need to declare the method also:
header:
#ifndef __MyClass_h_
#define __MyClass_h_
class MyClass {
static int x;
public:
static int y;
void sor() (const string& var1, const unsigned count) const;
};
#endif
source file:
#include "MyClass.h"
int MyClass::x = 0; // intialization
void MyClass::sor() (const string& var1, const unsigned count) const {
MyClaxx::x = 11; // access it
}
You have a declaration for the static variable but don't have the definition for it. In order to use static class members you should define them in the corresponding translation unit. You can't do it in the header file because it will violate ODR(one definition rule) so you should define it in the .cpp file to confrom the aforementioned rule.
So you have to put int MyClass::x = 0; in your cpp file in global scope(or under the namespace if you have one) to get it working. Note, that you could use whatever value insted of 0 or even didn't provide any(in this case it will be 0 anyway, due to special treatment of global(static) variables.)
When static variable is declared in a header file is its scope limited to .h file or across all units.
Refer here for source
This is simple. When you declare a static variable in a header file, it's scope is limited to header file. When you going to use that static variable in a .cpp file you getting an error like this. It is because you didn't give the definition of the static variable. So in any of the .cpp file you need to give the definition of static variable. ex :
.h file
class MyClass {
static int x;
.....
}
Top of the .cpp file you should define the static variable like this.
#include "MyClass.h"
int MyClass::x = 0 ;
void MyClass::sor() (const string& var1, const unsigned count) const {
MyClass::x = 8 ; // you can access without any issue
}
I am receiving the following error from the compiler
./include.list.h(22) error: identifier "input" is undefined.
Here is the header file where I explicitly define input-
#ifndef INIT_H
#define INIT_H
#include "list.h"
class input{
public:
//** thermo variables **//
int N;
double nddensity;
double ndtemp;
double ndvol;
double ndside;
double ndsideh;
// ** force field variables** //
double eps;
double sigma;
double rcut;
double rv;
double rcut2;
double rv2;
double rcrv2;
input();
};
void print(input &);
double randomnumber();
void position(list &, input &, int flag);
#endif
In init.cpp, I have the class initialization:
input:: input() {
//** thermo variables **//
N = 500;
nddensity =.8;
ndtemp = 2.0;
ndvol = N/nddensity;
ndside = pow(ndvol,1.0/3.0);
ndsideh = ndside/2;
// ** force field variables** //
eps = 1;
sigma = 1;
rcut = 2.5;
rv = 1.1*rcut;
rcut2 = rcut*rcut;
rv2 = rv*rv;
rcrv2 = (rv-rcut)*(rv-rcut);
}
I can't seem to figure out why input would be undefined to void print. Any help is appreciated.
You have a circular dependency.
You can forward declare the other class, but this is still a poor design. Better to just do away with the circular dependency altogether. At second glance, do you even need to include list.h? Where are you using anything declared in it?
You include list.h before the class definition, so it's not available in that header. According to the error message, something in list.h needs a declation of the class.
You don't need a full definition of list here, so replace the #include with a forward declaration
class list;
Within list.h, you probably need a similar declaration of input; or it might be necessary to include init.h if the header does something complicated with it.
I have two classes, and this is the header of one of them:
#ifndef WRAPPER_HPP
#define WRAPPER_HPP
#include <SDL/SDL.h>
using namespace std;
class Wrapper
{
private:
//SDL_Surface *screen;
public:
static SDL_Surface *screen;
static void set_screen(SDL_Surface *_screen);
static void set_pixel(int x, int y, Uint8 color);
static void clear_screen(int r, int g, int b);
static SDL_Surface* load_image(char path[500]);
static void draw_image(SDL_Surface *img, int x, int y, int width, int height);
static void draw_line(int x1, int y1, int x2, int y2, Uint8 color);
};
#endif
I am calling Wrapper::set_screen(screen) from another file and I get this error:
In file included from /home/david/src/aships/src/Wrapper.cpp:6:0:
/home/david/src/aships/src/Wrapper.hpp: In static member function ‘static void Wrapper::set_screen(SDL_Surface*)’:
/home/david/src/aships/src/Wrapper.hpp:11:18: error: invalid use of member ‘Wrapper::screen’ in static member function
/home/david/src/aships/src/Wrapper.cpp:10:3: error: from this location
I also get a similar error for the definition of every single function on Wrapper.cpp, for example:
void Wrapper::set_pixel(int x, int y, Uint8 color)
{
/* Draws a pixel on the screen at (x, y) with color 'color' */
Uint8 *p;
p = (Uint8 *) screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel;
*p = color;
}
On compile:
/home/david/src/aships/src/Wrapper.hpp: In static member function ‘static void Wrapper::set_pixel(int, int, Uint8)’:
/home/david/src/aships/src/Wrapper.hpp:11:18: error: invalid use of member ‘Wrapper::screen’ in static member function
/home/david/src/aships/src/Wrapper.cpp:17:17: error: from this location
I know it's related to the class being static and thus the variable Wrapper.screen is not accessible or something, but I'm not sure of how to fix it. Any ideas?
You are using a static variable
static SDL_Surface *screen;
in your code.
In C++ when you declare a static variable in the .h (or .hpp) you are creating a variable that is general (static) to the class. Thus, to use it in another file you have to redeclare it (which I'm guessing you didn't) to create a variable in that file referencing the static one. In your case put this:
SDL_Surface* Wrapper::screen;
in the .cpp file.
I'm not sure the theory is well explained, but it works like that.
Your class and member (screen) are not static, which means they don't actually exist.
You can't access a non static member in a static function.
Try to make your data members to be static.
I'm not convinced that the code abstract you show us is an accurate characterization of your problem.
Your header should not include using namespace std; — it doesn't use or declare anything from the std namespace, and specifying using namespace std; is generally regarded as 'not a good idea', doubly so when it appears in a header file.
It also isn't clear that your header needs to include SDL/SDL.h. If the Uint8 type is easily isolated (not necessarily valid), then your header file can simply use a forward declaration of the SDL_Surface class. (Your implementation code will need to include SDL/SDL.h; but you should not burden the users of your wrapper class with unnecessary #include directives when simple forward declarations would suffice.)
This code is self-contained (does not need any headers), but more or less simulates what you could use, and it compiles OK:
#ifndef WRAPPER_HPP
#define WRAPPER_HPP
typedef unsigned char Uint8;
class SDL_Surface;
class Wrapper
{
public:
static SDL_Surface *screen;
static void set_screen(SDL_Surface *_screen);
static void set_pixel(int x, int y, Uint8 color);
static void clear_screen(int r, int g, int b);
static SDL_Surface *load_image(char path[500]);
static void draw_image(SDL_Surface *img, int x, int y, int width, int height);
static void draw_line(int x1, int y1, int x2, int y2, Uint8 color);
};
#endif
//#include <SDL/SDL.h>
typedef unsigned short Uint16;
class SDL_Surface
{
public:
Uint8 *pixels;
Uint16 pitch;
struct
{
Uint8 BytesPerPixel;
} *format;
};
// End of SDL/SDL.h
void Wrapper::set_pixel(int x, int y, Uint8 color)
{
/* Draws a pixel on the screen at (x, y) with color 'color' */
Uint8 *p;
p = (Uint8 *) screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel;
*p = color;
}
It also compiles without warnings. The (Uint8 *) cast (copied from the original) is unnecessary. With the class definition given, it is superfluous; if you are needing to use a cast because the type of the pixels member of SDL_Surface actually isn't Uint8, are you sure it is a good idea? And can't you use reinterpret_cast<Uint8>(screen->pixels) instead to make it clearer?
Can you reduce your problem to code analogous to this that still shows the actual error?
The code below works as long as I keep it all in the "main.cpp" file.
//#include "Travel.h"
//#include "Obj.h"
// "Travel.h"
typedef int travel_t;
class Travel
{
public:
static const travel_t AIR;
static const travel_t WATER;
static const travel_t LAND;
};
// "Travel.cpp"
// #ifndef TRAVEL_H
// #define TRAVEL_H
//
// #include "Travel.h"
const travel_t Travel::AIR = -2;
const travel_t Travel::WATER = -1;
const travel_t Travel::LAND = 0;
// #endif //TRAVEL_H
// "Obj.h"
// #ifndef OBJ_H
// #define OBJ_H
//
//#include "Travel.h"
template<typename T, travel_t travel>
class Obj
{
public:
void foo(){};
};
// #endif //OBJ_H
// "main.cpp"
int main()
{
Obj<int, Travel::AIR> objAirTravel;
objAirTravel.foo();
return 0;
}
However, as soon as I moved code to different headers and implementation files as indicated, it doesn't compile any more. :-( How can I fix that problem? What is the problem/rule behind it? This is the compiler error I get (using gcc):
main.cpp|45|error: 'Travel::AIR' is not a valid template argument for type 'int' because it is a non-constant expression|
main.cpp|45|error: invalid type in declaration before ';' token|
main.cpp|47|error: request for member 'foo' in 'objAirTravel', which is of non-class type 'int'|
In order to use a constant as a template argument, its value must be available in the current translation unit. When you move the definition of Travel::Air to a different source file, its value is no longer available to the compiler in main.
Since it's an integer constant, you can declare the value in the declaration inside the class:
class Travel
{
public:
static const travel_t AIR = -2;
static const travel_t WATER = -1;
static const travel_t LAND = 0;
};
Now the values are available to use as template arguments in any translation unit that includes this class definition.