My #include dependencies seem to be preventing me from using a class - c++

I am trying to use a function from another class but my dependencies seem to be stopping me.
main.cpp
#include "gesture.hpp"
#include "statemachine.hpp"
Gesture g;
StateMachine sm(g);
gesture.hpp
#include "hand.hpp"
statemachine.hpp
#include "gesture.hpp"
StateMachine(Gesture&);
Gesture *g;
What I am trying to accomplish:
I am trying to use a function from the StateMachine sm that I have declared. This function would be called inside a function in gesture.cpp and I would be giving my Gesture g class a pointer to StateMachine sm. (I would do this after I declare sm in main) I am able to #include "statemachine.hpp" in my gesture.cpp file but I want to move it to gesture.hpp so I can have it as a pointer that is stored as a variable in that class.
So when I do
gesture.hpp
#include "hand.hpp"
#include "statemachine.hpp"
I get the error 'Gesture' does not name a type and expected ')' before '&' token StateMachine(Gesture&);
Can anyone figure out what is going on? I can't move my function to gesture.cpp because it uses an array that is stored in my statemachine.hpp

You didn't provide the details so i will post my guess here.
When the precompiler analyze "gesture.hpp", it will unroll it to something like this:
codes in hand.hpp
StateMachine(Gesture&);
Gesture *g;
the file "gesture.hpp" is not unrolled in statemachine.hpp because i think you had provided some protections against circular dependency. so the compiler don't know what Gesture is.
To solve the compiling error, you can put a forward declaration of Gesture to statemachine.hpp, like this:
class Gesture;
StateMachine(Gesture&);
Gesture *g;

Related

How to proper set up a destructor in C++ with Xcode?

there is something that has been bugging me for a while.
I cannot create a destructor using Xcode (with other IDEs like VS2021 that is no issue).
I get the error:
1. Constructor cannot be redeclared
2. Missing return type for function '˜Pointer'; did you mean the constructor name 'Pointer'?
If I try to declare outside of the class and uncomment the lines in *.cpp and *.hpp the errors get even crazier.
My Pointers.hpp is the following:
#ifndef Pointers_hpp
#define Pointers_hpp
#include <iostream>
class Pointer{
public:
Pointer(void);
˜Pointer(void){};
//˜Pointer(void);
};
#endif /* Pointers_hpp */
and my Pointers.cpp is this one:
#include "Pointers.hpp"
Pointer::Pointer(void){};
//Pointer::˜Pointer(void){};
After several research in the internet, I could not find a solution to that, could any one give me a light on this?
Many thanks in advance,
Raphael
Solved thanks to user4581301:
For those having the same problem I did.
The issue here was the similarity between ˜ and ~
The correct one should be ~
If you are using MacBook Pro the short-key is Option-N.

using WRL to access winRT: where do you find the right names to use for the classfactories and classes?

(This is the next iterative question so I can figure out how to use WRL, after this one was answered I believe correctly.)
So I'm trying to build up to being able to access Bluetooth LE devices through WinRT via WRL.
Continuing in the pattern of a Microsoft example (which is for the uri WinRT class), I am trying at this point just to get the class factory for the DeviceWatcher Class. ( Documentation for that class seems to be here ), this is my pared-down example code:
#include <roapi.h>
#include <combaseapi.h>
#include <iostream>
#include <windows.foundation.h>
#include <windows.foundation.collections.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>
#include <stdio.h>
#include <windows.devices.enumeration.h>
using namespace ABI::Windows::Foundation;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Devices::Enumeration;
int dummy;
ComPtr<IUriRuntimeClassFactory> uriFactory;
ComPtr<IUriRuntimeClass> uri;
ComPtr<IDeviceWatcherRuntimeClassFactory> devWatcherFactory; //This line produces the only error, "IDeviceWatcherRuntimeClassFactory undefined"
//(When the line is omitted, the code compiles, links, and produces the smart pointer to a uri class)
HRESULT hr, hr2, hr3;
int main()
{
// Initialize the Windows Runtime.
RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
// Get the activation factory for the IUriRuntimeClass interface.
ComPtr<IUriRuntimeClassFactory> uriFactory;
HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);
// Create a string that represents a URI.
HString uriHString;
hr = uriHString.Set(L"http://www.microsoft.com");
// Create the IUriRuntimeClass object.
ComPtr<IUriRuntimeClass> uri;
hr2 = uriFactory->CreateUri(uriHString.Get(), &uri);
dummy = 1;
}
It is the MS example, up to the point of its uri WinRT class construction, and compiles and links and correctly constructs the uri class smart pointer when I leave out my added line that declares the ComPtr for IDeviceWatcherRuntimeClassFactory
The line is my attempt to just create a smart-pointer for the DeviceWatcher class factory, and it fails with identified "IDeviceWatcherRuntimeClassFactory undefined". (It also fails if I use small "d" as the second letter of the type argument.
In using "IDeviceWatcherRuntimeClassFactory", I'm trying to proceed by exact analogy to the MS example.
Besides the MS online documentation for the DeviceWatcher class (3), which doesn't seem to help me, I've also tried looking at what VS2022 solution explorer shows as the expansion of the header windows.devices.enumeration.h (which also does not seem to be telling me what name to use as far as I can figure out).
I've also tried using the VS2022 Object Browser, pointed to a custom component set of all .winmd files in C:\Windows\System32\WinMetaData, and again the information is not helping me, given my level of knowledge.
Can someone give me a sound method, for an arbitrary WinRT class, to figure out the type to use for the smart pointer to the class factory?
And, to save another iteration, once I can make a smart pointer to the factory, I will
need to make a smart pointer to the class itself (in my case, to DeviceWatcher). Can someone tell me what exact name to use for the ComPtr type?
Thanks.

Hiding use of class inside a library

I am creating a simple library for Arduino, the intent of which is to wrap and hide another class inside of it. I would like to be able to hide the underlying class entirely from the end user.
I have somewhat simplified this example to keep things clear, but the same problem exists.
The class I am trying to wrap is the Wire library which is called TwoWire.
The header file of the wrapper library:
#ifndef __DERIVEDONEWIRE2_H__
#define __DERIVEDONEWIRE2_H__
#include <Wire.h>
class DerivedWire
{
private:
TwoWire wire;
public:
DerivedWire();
};
#endif
The CPP file:
#include "DerivedWire.h"
DerivedWire::DerivedWire()
{
}
And the Arduino sketch that uses it:
#include <DerivedWire.h>
DerivedWire derivedWire;
void setup()
{
}
void loop()
{
}
This fails to compile:
/Users/andrew/Documents/Arduino/libraries/DerivedWire/DerivedWire.h:9:
error: 'TwoWire' does not name a type
Including Wire.h at the top of the main sketch allows this to compile:
#include <DerivedWire.h>
#include <Wire.h>
DerivedWire derivedWire;
void setup()
{
}
void loop()
{
}
However I would like to completely hide from the user of the DerivedWire library that the Wire library/TwoWire class is used at all.
I have tried changing the line "TwoWire wire;" to "TwoWire* wire;" - now I get the following error:
/Users/andrew/Documents/Arduino/libraries/DerivedWire/DerivedWire.h:9:
error: ISO C++ forbids declaration of 'TwoWire' with no type
/Users/andrew/Documents/Arduino/libraries/DerivedWire/DerivedWire.h:9:
error: expected ';' before '*' token
I've tried a few other things, but it just seems that I need to include Wire.h at the top level.
How do I fix this?
The Wire instance is created on the last line in the lib.
https://github.com/lstoll/arduino-libraries/blob/master/Wire/Wire.cpp
You are NOT supposed to have multiple instances of TwoWire, and there is no reason to hide it, but if you really want to hide it, you have to remove that line.
The arduino build method requires that the lib is included from the main sketch so you can not hide that part without signifigant changes.
If you really really need to hide this Wire instance, you could copy the source of the Wire lib into your own lib, and remove that last line.
Check out the "pimpl idiom", here's a link to get you started: http://en.wikipedia.org/wiki/Opaque_pointer
Pimpl idiom is a way in C++ to hide a class implementation, and all functions that you do not want to be visible.

can't figure out how to fix type dependency

I have this header file:
//region.hpp
#ifndef REGION_HPP
#define REGION_HPP
...
#include "spacepoint.hpp"
class Image;
//class SpacePoint;
class Region
{
Rectangle<SpacePoint> boundaries;
...
it gives this error
error: ‘SpacePoint’ was not declared in this scope
when I uncomment class SpacePoint i get this:
In instantiation of ‘class Rectangle<SpacePoint>’:
region.hpp:15:27: required from here
rectangle.hpp:19:7: error: ‘Rectangle<T>::start’ has incomplete type
I tried to reproduce the problem with a smaller test program but I can't.
I don't know how to go about solving this.
thanks to #Matteo's guidance I fixed the issue.
With this answer I configured doxygen to show the header files dependency.
It was easier to find the loops this way.
Where an include wasn't necessary I removed the include and added a declaration.
In the case of spacepoint.hpp I effectively turned #include <image.hpp> to class Image;
Before:
After:
(the graph is smaller as spacepoint.hpp doesn't depend on image.hpp anymore)

In the .cpp, is there a way to auto-implement all the functions from its .h?

I think this would increase the quality of life when devving, but google came up with nothing and I couldn't find anything specific inside inside Netbeans either.
What I want is to start with this header:
class bla
{
public:
static void gfg(somearg asd);
};
Then I open the blank bla.cpp and pressed 'autoimplement'. After that, it would look like this:
#include "bla.h"
static void bla::gfg(somearg asd)
{
//TODO: implement
throw unimplemented("void bla::gfg(somearg) is unimplemented");
}
Anyone know of a tool like this?
I found http://www.radwin.org/michael/projects/stubgen/
"stubgen is a C++ development tool that keeps code files in sync with their associated headers. When it finds a member function declaration in a header file that doesn't have a corresponding implementation, it creates an empty skeleton with descriptive comment headers."
This looks like it does exactly what you want it to do.
Some time has passed and in the meantime the requested feature seems to have been implemented in netbeans. Refer to https://netbeans.org/bugzilla/show_bug.cgi?id=213811 , which also gives a description on how to use it:
Note:
Implemented CTRL+SPACE.
IDE suggest implementing of class method if CTRL+SPACE was pressed:
- inside file that already has at least one method definition
- between method declarations