Including a certain header file causes errors from SFML - c++

I'm trying to use ChaiScript with SFML for my game engine. All the SFML stuff works fine, until I include chaiscript.hpp in my game object header file. My GameObject header file looks a little like this:
#include <a bunch of standard libraries>
#include "imgui.h"
struct Object {
std::string name;
void init();
void update();
void render();
*some template functions*
};
The problem occurs when I try to include chaiscript.hpp in the above file. I get a repeating error (8 of them, to be exact) from SFML's Rect.inl file: '(': illegal token on right side of '::', and then no other errors. It compiles fine when I don't include chaiscript.hpp, or when I include chaiscript.hpp in the object cpp file. I've used ChaiScript before with SDL and never had an issue like this, so is it an SFML macro messing something up? How can I avoid this problem?

It sounds like something in ChaiScript is messing up something in SFML. Reversing the order that you include them may remove those errors (include SFML before ChaiScript, or vice versa).

Related

wxWidgets - Splitting project into multiple files

I am very new to C++, I'm making this music application using wxWidgets, this is my first project in C++. As of now, I have 4 files, app.hpp and app.cpp which has a class that inherits from wxApp that launches the application, and frame.hpp and frame.cpp which holds the base frame and panel, and all the widgets, and their appropriate functions. I want to move all the functions to a separate file, but I get some errors like there is this function in frame.cpp
void Frame::ClearPlaylist(wxCommandEvent& event)
{
mediaCtrl->Stop();
playlistBox->Clear();
}
I tried moving it in another file called command.cpp and created a new class called command and prefixed all functions to Command:: .... and somethings like the playlistBox here, is a widget which I want in frame.cpp only, as it is a widget, so I did #include frame.hpp and prefixed it with Frame::playlistBox, but that gave a error saying invalid use of non static data member. So do I have to make everything in frame.hpp a static object? Or if anyone has a better solution for organizing a project like this please do share.

Load OpenGL function pointers using glad withing a QOpenGLWidget

So I am reading up on some OpenGL and I want to use the QOpenGLWidget for drawing to maybe create some other helpful UI elements later. I am using glad for resolving the function pointers to OpenGL but I have no idea how to use Qt's getProcAddress function!
Inside my QOpenGLWidget subclass' initializeGL() function I have tried:
if(gladLoadGLloader((GLADloadproc) currentContext()->getProcAddress) {}
but that did not work out since Qt's function is overloaded. When I use
if(gladLoadGL()) {}
it doesn't work either. My includes are:
#include <glad\glad.h>
#include "OpenGLViewport.h"
#include <QDebug>
#include <QOpenGLContext>
I have searched Mr. Google and I've had a diligent look through the Qt documentation and found nothing. I want to use GLAD just so my rendering code is not bound to Qt too tightly, in case I want to switch later.
EDIT: I am aiming to use the noninstanced OpenGL functions with Qt (though the documentation recommends otherwise if I recall correctly). Because then I'd be able to seemlessly switch to GLFW for providing a window etc.
Moved solution from question to answer:
ANSWER: So it turns out I just had some things mixed up, this is how I got it to work, in case anyone has the same problem:
add glad.c in your project
add the necessary headers to your include directory
the .cpp file of your QOpenGLWidget subclass should have following components:
// Subclass.cpp
#include <glad/glad.h>
// important, subclass header file after glad!!
// otherwise glad won't "hook"
#include "Subclass.h"
void Subclass::initializeGL()
{
if(gladLoadGL()) initialized = true; // initialized is a class member here
else; // handle errors
}
void Subclass::paintGL()
{
if(initialized); // render here
}

Arduino: how to use object of other class as arguments of my library?

I'm breaking my head trying to use Servo.h inside of a library I made. The compiler consistently gives me the same error, as if it does not recognize the class, which is included in my library.
I am trying to make a new class, one of it's properties is a Servo object, which I should pass in the constructor. No matter how I try, I keep receiving the same error message when trying to compile my sketch:
In file included from /home/nezah/Arduino/My
sketches/CameraShutter/CameraShutter.ino:8:0:
/home/nezah/Arduino/libraries/Shutter/Shutter.h:13:19: error: expected
')' before '*' token
Shutter(Servo *servo);
It seems that the include statement is ok, as I get a different message if I mess it to go wrong or remove it completely. I already tried to change "" for <> and even copied the source in a folder and use the full path. No change as far as I don't mess it (on purpose). I already read this.
I also tried to pass it as a pointer, using Shutter(Servo* servo), Shutter(Servo *servo) and Shutter(Servo& servo). Same error message.
In some arduino.cc forum I read that I rather forget it and avoid using libraries inside other libraries, but I bet this is possible.
Is there anybody so kind as to give me some hints on how to do this?
I leave you part of my .h and .cpp of the library I'm trying to write (which, by the way, turns a servo into a physical button presser but with burst capability).
/*
* Shutter.h - Library to make a photocamera shutter out of a servo
* alternatively it could press any physical button with a servo.
*/
#ifndef Shutter
#define Shutter
#include "Servo.h"
class Shutter {
public:
Shutter(Servo *servo);
Servo getServo();
void shut();
private:
Servo _servo;
}
#endif
And here is my .cpp:
/*
Shutter.cpp - Library for flashing Shutter code.
Created by David A. Mellis, November 2, 2007.
Released into the public domain.
*/
#include <Arduino.h>
#include "Servo.h"
#include "Shutter.h"
Shutter::Shutter(Servo *servo) {
_servo = servo;
}
NOTE: If I remove some code and take away the "Servo" part of the constructor, I get an error message on the "getServo()" code. The problem seems to be that the compiler does not recognize "Servo" as a valid type inside my library.
Thanks in advance!
In the constructor of your class you are passing a pointer of the type Servo, so you must store that value in another pointer. To do this you must change:
*.h
#ifndef SHUTTER_H
#define SHUTTER_H
#include "Servo.h"
class Shutter {
public:
Shutter(Servo *servo);
Servo *getServo() const;
void shut();
private:
Servo *_servo;
}
#endif
*.cpp
Shutter::Shutter(Servo *servo) {
_servo = servo;
}
Servo *Shutter::getServo() const
{
return _servo;
}
Use:
Servo servo;
Shutter shuter(&servo)
Looks like the problem was that the class name and the #ifndef marker was the same, so there was a name conflict somehow. It is well explained in this thread: How to properly use a header file to be a complete class?
After fixing this, it compiled well.

Visual Studio not building correctly

I've been having two different problems when trying to compile a C++ project today.
Sometimes it won't reflect any of my changes in the new build: If I change some wording on the output or change around actual functionality and compile and hit Start Debugging, it will behave exactly as it did before I made the changes. Hitting Clean or Rebuild Solution fixes this, but it takes about a full minute to compile this. I guess it's not detecting any changes, but in the output window I see it list the file names of files I made changes to...
I'm also getting a lot of "...already defined in main.obj" errors (one for every function and variable) whenever I try to use a header file or define a function outside of a class. Renaming the functions lets it compile once, but then the second compile will bring up the errors again. It kinda works if I just define the class in a .cpp file, don't use a header file, and don't use any functions outside of the class.
The project is an open-source program I downloaded the other day to mess with (I'm building a bot to control it). I didn't have the first problem until today, but the second one's always been happening. All I've done is add a file (two if you count both Bot.cpp and Bot.h); Bot.cpp includes Bot.h and some files from the program, and the program's main.cpp includes Bot.cpp.
I'll post some code I guess, but I can't find anything wrong with what I'm doing. I'm wondering if there's something I need to do to the existing files? (there were VS solution files included with the project that I used to open it, since VS Express doesn't help you create projects from existing code.)
//Bot.h
#ifndef _Bot_h_
#define _Bot_h_ 1
#include <string>
class Bot{
private:
uint32 inputs = 0;
bool active = false;
public:
Bot(){};
~Bot(){};
void Start();
void Stop();
void Run();
void Wait(int seconds);
void Press(int buttons);
};
#endif
//Bot.cpp
#ifndef _Bot_cpp_
#define _Bot_cpp_ 1
#include "main.h"
//Some other project files included between these
#include "Bot.h"
using namespace std;
void Bot::Start(){
if (active == false){
active = true;
Run();
}
}
void Bot::Stop(){
active = false;
}
void Bot::Run(){
while (active == true){
printf("Has my code updated?\n");
Wait(2);
}
}
//There are more functions defined here
#endif
All I've really done in the original source code is include Bot.cpp at the bottom of the list of includes in the main.cpp file, and then add a call to create the class. I'm a little out of practice with C/C++ so maybe it's something simple. I'm also bad at writing posts like this so if anything needs clarified I'll try to help.

Expected specifier-qualifier-list before... Error in C++/Objective-C iPhone project

So I'm going through the first tutorial in O'Reilly's iPhone 3D Programming book. At this point in the tutorial, it pulls all the OpenGL ES stuff into a seperate c++ interface. I have followed the book to the letter, as far as I can tell, yet I can't seem to figure out this compiler error. I'm fairly new to C++ (mostly C# in the past), so I'm sure it's something stupid.
Below is the current state of all the relevant files.
I have a c++ header file called IRenderingEngine.hpp with the following contents:
enum DeviceOrientation {
Unknown,
Portrait,
PortraitUpsideDown,
LandscapeLeft,
LandscapeRight,
FaceUp,
FaceDown,
};
struct IRenderingEngine* CreateRenderer1();
struct IRenderingEngine {
virtual void Initialize(int width, int height) = 0; //Compiler error "expected specifier-qualifier-list before 'virtual'
virtual void Render() const = 0;
virtual void UpdateAnimation(float timeStep) = 0;
virtual void OnRotate(DeviceOrientation newOrientation) = 0;
virtual ~IRenderingEngine() {}
};
I have an objective-c/c++ header file called GLView.h that looks like this:
#import "IRenderingEngine.hpp"
#import <OpenGLES/EAGL.h>
#import <QuartzCore/QuartzCore.h>
#interface GLView : UIView {
EAGLContext* m_context;
IRenderingEngine* m_renderingEngine; //Compiler error: Expected specifier-qualifier-list before "IRenderingEngine"
float m_timeStamp;
}
- (void) drawView: (CADisplayLink*) displayLink;
- (void) didRotate: (NSNotification*) notification;
#end
And finally, a GLView.mm file with a barebones implementation:
#import "GLView.h"
#implementation GLView
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
- (id)initWithFrame:(CGRect)frame {
return self;
}
- (void) drawView:(CADisplayLink *)displayLink
{
}
-(void) didRotate:(NSNotification *)notification
{
}
#end
You need to rename HelloArrowAppDelegate.m to a .mm file. It states in the book on page 20 (middle section with the paws bullet point). I missed that section and had the same problem. After changing the file to .mm the program worked.
This error message e.g. occurs if one of your two headers gets included in a plain Objective-C source file, which doesn't know anything about handling C++ code.
To allow GLView to be used from plain Objective-C sources, use only a forward declaration for the rendering engine and don't include the C++ header in GLView.h:
// GLView.h:
struct IRenderingEngine;
#interface GLView : UIView {
struct IRenderingEngine* m_renderingEngine;
// ...
#end
// GLView.mm:
#import "IRenderingEngine.hpp"
// ... etc.
Alternatively you can use opaque pointers for wrapping C++ instances to keep the Objective-C interface more stable, see e.g. Rob Napiers post on the subject.
When this is fixed, you still need to fix the declaration for CreateRenderer1() as others pointed out - either forward-declare struct IRenderingEngine; before the function or just move it after the definition of the struct.
I had the exact same problem. It turned out to be due to the fact that renaming HelloArrowAppDelegate.m to HelloArrowAppDelegate.mm in Xcode's Groups & Files did not actually rename the file! Once I used Finder to rename it the project compiled OK. Note Xcode may complain that the HelloArrowAppDelegate.m is missing when you open the project again, just right click it, delete it and then use "Add existing files" to add the HelloArrowAppDelegate.mm file back in.
This may seem a cop-out but I had exactly the same problem when I updated to XCode 4.x
I got around it by downloading the source code from O'reilly's website and just loaded the project. Xcode appeared to recognise it as an old project and configured itself accordingly; no compilation issues, worked first time. It's not the same as typing it in yourself but it works.
The line
struct IRenderingEngine* CreateRenderer1();
Must come after the declaration of the IRenderingEngine class. (Also, if you intended to use the variable as a pointer to that structure type you should omit the parentheses in its declaration.)
For your other error I think you want to prepend the line with struct.
I believe this line is causing the error:
struct IRenderingEngine* CreateRenderer1();
I'm not quite sure what you're trying to do there, but I think it should be removed entirely.