forward declaration problems - c++

I think I am having a problem with forward declarations. I think one is necessary, but I'm not sure.
Basically I have a main.cpp:
//main.cpp
#include <iostream>
#include "CalculateForces.h"
#include "ParticleBox.h"
int main(void)
{
//g++ main.cpp ParticleBox.cpp -lgsl -lgslcblas -lm -std=c++0x
CalculateForces* calculate_forces= new CalculateForces();
ParticleBox* particles_box = new ParticleBox(2000,100,100,100);
delete calculate_forces;
delete particles_box;
return 0;
}
CalculateForces.h looks like this:
//CalculateForces.h
//We update the forces on each particle
class ParticleBox;
class CalculateForces
{
public:
CalculateForces(void);
~CalculateForces(void);
int UpdateForces(ParticleBox* particlebox);
int DiscretizeSpace(float cutoff_distance);
int LJForce(int local_index, int remote_index, ParticleBox* particlebox);
};
And finally the ParticleBox.h File looks like this:
//ParticleBox.h
//This is the definition of the particlebox. We manage all the particles in this
//file
//This should be changed to a template so that we can run float and double calcs properly :D
struct Particle;
class ParticleBox
{
public:
ParticleBox(int Num_Particles, float Box_length_x_, float Box_length_y_, float Box_length_z_);
~ParticleBox(void);
int set_num_particles(int Num_Particles);
int InitialiseUniverse(int temp,float mass);
float Boltzmann(float temperature);
int GenerateRandomUniquePositions(int number, float max, float min, float* rand_dim_positions);
private:
//Array to hold particles. Each particle has its own struct
Particle** particle_list_;
int num_particles_;
float box_length_x_;
float box_length_y_;
float box_length_z_;
float* rand_x_positions_;
float* rand_y_positions_;
float* rand_z_positions_;
float cutoff_distance_;
float sigma_;
float epsilon_;
};
int CalculateForces::DiscretizeSpace(float cutoff_distance, ParticleBox* particlebox)
{
......
return 0;
}
I use a forward declaration in ParticleBox.h of the Particle Struct and I can add a pointer of type Particle* to the class. This works fine.
The forward in CalculateForces.h of Class ParticleBox causes loads of compiler errors (too many to post but they start in an identical way to the below). Omitting it produces only a few errors:
In file included from main.cpp:3:0:
CalculateForces.h:9:20: error: ‘ParticleBox’ has not been declared
CalculateForces.h:11:50: error: ‘ParticleBox’ has not been declared
In file included from CalculateForces.cpp:3:0:
CalculateForces.h:9:20: error: ‘ParticleBox’ has not been declared
CalculateForces.h:11:50: error: ‘ParticleBox’ has not been declared
CalculateForces.cpp:12:35: error: ‘int CalculateForces::UpdateForces’ is not a static member of ‘class CalculateForces’
CalculateForces.cpp:12:35: error: ‘ParticleBox’ was not declared in this scope
CalculateForces.cpp:12:48: error: ‘particlebox’ was not declared in this scope
CalculateForces.cpp:13:1: error: expected ‘,’ or ‘;’ before ‘{’ token
I thought i would need the forward declaration as I try to use that type as an argument? What am i doing wrong?
Thanks and sorry for the long post

Your post is quite confusing because you posted the error that show up when you omit the forward declaration and you obviously have some additional errors in your code that mix with the error you asked about.
I assume that with the forward declaration, the errors change as they appear mostly in the implementation files, right? In this case the problem might be that the forward declaration is enough as long as you declare a pointer to the type, but it is not enough when you start using the pointer (dereferencing it).
If that is the case, the problem is most likely that you forgot to #include "ParticleBox.h" in CalculateForces.cpp (or some other implementation files).

As Rob determined the source of the errors was in a bit of code that I did not post. There were a few errors but the biggest was that in CalculateForces.cpp I tried to access int num_particles_; which is of course a private member of ParticleBox.

Related

How to resolve error: invalid use of incomplete type ‘GdkSurface {aka struct _GdkSurface}’?

Questions about incomplete type errors have already been asked here often, but all of the solutions provided there do not help in my case. Adding a forward declaration makes no sense, as GdkSurface has been forward declared already in the Gdk headers. Including the appropriate headers has already been done. Following the error producing code portion + includes.
#include <gdkmm/display.h>
#include <gdkmm/surface.h>
extern "C" {
#include <gdk/x11/gdkx.h>
#include <gdk/gdk.h>
}
extern "C" {
void surface_move(Gdk::Surface* psurface, int x, int y) {
#ifdef GDK_WINDOWING_X11
GdkSurface* surface = psurface->gobj();
GdkSurface *impl = GDK_X11_SURFACE(surface);
XMoveWindow(GDK_SURFACE_XDISPLAY (surface), GDK_SURFACE_XID (surface), x * impl->surface_scale, y * impl->surface_scale);
#endif
}
}
Here are the complete errors:
src/utils.cpp: In function ‘void Gdk::surface_move(Gdk::Surface*, int, int)’:
src/utils.cpp:9:83: error: invalid use of incomplete type ‘GdkSurface {aka struct _GdkSurface}’
(GDK_SURFACE_XDISPLAY (surface), GDK_SURFACE_XID (surface), x * impl->surface_scale, y * impl->surface_scale);
^~
In file included from /home/user/.local/built/include/gtk-4.0/gdk/gdkapplaunchcontext.h:29:0,
from /home/user/.local/built/include/gtk-4.0/gdk/gdk.h:30,
from /home/user/.local/built/include/gtkmm-4.0/gdkmm/enums.h:29,
from /home/user/.local/built/include/gtkmm-4.0/gdkmm/event.h:29,
from /home/user/.local/built/include/gtkmm-4.0/gdkmm/display.h:30,
from ./include/libgdp/utils.hpp:3,
from src/utils.cpp:1:
/home/user/.local/built/include/gtk-4.0/gdk/gdktypes.h:97:16: note: forward declaration of ‘GdkSurface {aka struct _GdkSurface}’
typedef struct _GdkSurface GdkSurface;
^~~~~~~~~~~
src/utils.cpp:9:108: error: invalid use of incomplete type ‘GdkSurface {aka struct _GdkSurface}’
rface), GDK_SURFACE_XID (surface), x * impl->surface_scale, y * impl->surface_scale);
^~
In file included from /home/user/.local/built/include/gtk-4.0/gdk/gdkapplaunchcontext.h:29:0,
from /home/user/.local/built/include/gtk-4.0/gdk/gdk.h:30,
from /home/user/.local/built/include/gtkmm-4.0/gdkmm/enums.h:29,
from /home/user/.local/built/include/gtkmm-4.0/gdkmm/event.h:29,
from /home/user/.local/built/include/gtkmm-4.0/gdkmm/display.h:30,
from ./include/libgdp/utils.hpp:3,
from src/utils.cpp:1:
/home/user/.local/built/include/gtk-4.0/gdk/gdktypes.h:97:16: note: forward declaration of ‘GdkSurface {aka struct _GdkSurface}’
typedef struct _GdkSurface GdkSurface;
^~~~~~~~~~~
I built Gdk, Gtk, Gdkmm and Gtkmm with JHbuild.
It seems this type is private to GDK by design (only a forward declaration is provided). From the GDK4 documentation:
The GdkSurface struct contains only private fields and should not
be accessed directly.
See here for the header in which it is defined (which is not distributed). This is why you get these errors, all you have is a forward declaration to pass around pointers and references. All access to data members is forbidden.
To solve this, you have to use functions that work on surfaces (that are public), such as gdk_surface_get_scale_factor or something similar instead of trying to access data members directly.

Typedef not a member of namespace

So, I'm attempting to fork some open source code and upon compilation I am greeted with these errors:
C2039 'TransactionId': is not a member of 'CryptoNote'
C2061 syntax error: identifier 'TransactionId'
I'm relatively inexperienced with C++ usually confining myself to the realms of C#, however, I can clearly see that TransactionId is a typedef declared in a different file like so:
namespace CryptoNote {
typedef size_t TransactionId;
typedef size_t TransferId;
//more code
And the line throwing the error is:
void sendTransactionCompleted(CryptoNote::TransactionId _id, bool _error, const QString& _error_text);
To my inexperienced eyes, that looks as though TransactionID is definitly a member of Cryptonote is it not?
Any ideas what's going on?
The repo is here: https://github.com/hughesjs/Incendium_GUI
And the necessary submodule is here: https://github.com/hughesjs/Incendium_Crypt
Those typedefs are defined in Incendium_Crypt/include/IWalletLegacy.h.
void sendTransactionCompleted(CryptoNote::TransactionId _id, bool _error, const QString& _error_text);`
is defined in Incendium_GUI/src/gui/SendFrame.h, which includes IWallet.h. However, IWallet.h does not in turn include IWalletLegacy.h. Hence, those typedefs are unknown to SendFrame.h.
It's difficult to say without seeing all the code but a few things come to mind:
Firstly is this the first error you get. Compilation errors with C++ tend to result in a bunch of secondary errors. For example the following results in a similar error to what you see but fails to compile because size_t has not been defined:
namespace CryptoNote {
typedef size_t TransactionId;
typedef size_t TransferId;
}
int main(void)
{
CryptoNote::TransactionId id;
return 0;
}
$ g++ -std=c++11 namespace.cxx -o namespace
namespace.cxx:4:9: error: ‘size_t’ does not name a type
typedef size_t TransactionId;
^~~~~~
namespace.cxx:5:9: error: ‘size_t’ does not name a type
typedef size_t TransferId;
^~~~~~
namespace.cxx: In function ‘int main()’:
namespace.cxx:11:17: error: ‘TransactionId’ is not a member of ‘CryptoNote’
CryptoNote::TransactionId id;
^~~~~~~~~~~~~
See http://www.cplusplus.com/reference/cstring/size_t/ for a list of headers that define size_t.
Is CryptoNote nested inside another namespace?
Is there another CryptoNote defined in the namespace your function is declared in?
Are these in the same header file? If not, is the header file where the namespace is defined included in the header file containing the function declaration?

Understanding 'using' keyword : C++

Can someone please explain below output:
#include <iostream>
using namespace std;
namespace A{
int x=1;
int z=2;
}
namespace B{
int y=3;
int z=4;
}
void doSomethingWith(int i) throw()
{
cout << i ;
}
void sample() throw()
{
using namespace A;
using namespace B;
doSomethingWith(x);
doSomethingWith(y);
doSomethingWith(z);
}
int main ()
{
sample();
return 0;
}
Output:
$ g++ -Wall TestCPP.cpp -o TestCPP
TestCPP.cpp: In function `void sample()':
TestCPP.cpp:26: error: `z' undeclared (first use this function)
TestCPP.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
I have another error:
error: reference to 'z' is ambiguous
Which is pretty clear for me: z exists in both namespaces, and compiler don't know, which one should be used. Do you know? Resolve it by specifying namespace, for example:
doSomethingWith(A::z);
using keyword is used to
shortcut the names so you do not need to type things like std::cout
to typedef with templates(c++11), i.e. template<typename T> using VT = std::vector<T>;
In your situation, namespace is used to prevent name pollution, which means two functions/variables accidently shared the same name. If you use the two using together, this will led to ambiguous z. My g++ 4.8.1 reported the error:
abc.cpp: In function ‘void sample()’:
abc.cpp:26:21: error: reference to ‘z’ is ambiguous
doSomethingWith(z);
^
abc.cpp:12:5: note: candidates are: int B::z
int z=4;
^
abc.cpp:7:5: note: int A::z
int z=2;
^
which is expected. I am unsure which gnu compiler you are using, but this is an predictable error.
You get a suboptimal message. A better implementation would still flag error, but say 'z is ambiguous' as that is the problem rather than 'undeclared'.
At the point name z hits multiple things: A::z and B::z, and the rule is that the implementation must not just pick one of them. You must use qualification to resolve the issue.

C++: Maybe you know this pitfall?

I'm developing a game. I have a header GameSystem (just methods like the game loop, no class) with two variables:
int mouseX and int mouseY. These are updated in my game loop. Now I want to access them from Game.cpp file (a class built by a header-file and the source-file). So, I #include "GameSystem.h" in Game.h. After doing this I get a lot of compile errors. When I remove the include he says of course:
Game.cpp:33: error: ‘mouseX’ was not declared in this scope
Game.cpp:34: error: ‘mouseY’ was not declared in this scope
Where I want to access mouseX and mouseY.
All my .h files have Header Guards, generated by Eclipse.
I'm using SDL and if I remove the lines that wants to access the variables, everything compiles and run perfectly (*).
I hope you can help me...
This is the error-log when I #include "GameSystem.h" (All the code he is refering to works, like explained by the (*)):
In file included from ../trunk/source/domein/Game.h:14,
from ../trunk/source/domein/Game.cpp:8:
../trunk/source/domein/GameSystem.h:30: error: expected constructor, destructor, or type conversion before ‘*’ token
../trunk/source/domein/GameSystem.h:46: error: variable or field ‘InitGame’ declared void
../trunk/source/domein/GameSystem.h:46: error: ‘Game’ was not declared in this scope
../trunk/source/domein/GameSystem.h:46: error: ‘g’ was not declared in this scope
../trunk/source/domein/GameSystem.h:46: error: expected primary-expression before ‘char’
../trunk/source/domein/GameSystem.h:46: error: expected primary-expression before ‘bool’
../trunk/source/domein/FPS.h:46: warning: ‘void FPS_SleepMilliseconds(int)’ defined but not used
This is the code which try to access the two variables:
SDL_Rect pointer;
pointer.x = mouseX;
pointer.y = mouseY;
pointer.w = 3;
pointer.h = 3;
SDL_FillRect(buffer, &pointer, 0xFF0000);
In your GameSystem header, don't define those variables as:
int mouseX;
int mouseY;
instead, you should declare them:
extern int mouseX;
extern int mouseY;
Then in one of your .cpp files you define them:
int mouseX;
int mouseY;
The problem with defining them in a header file is that the compiler will try to instantiate them in every single .cpp where you include the header.

'class X' has no member 'Y'

This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited