adding class to mfc project - c++

I am trying to add a class which holds the object of the gui. The file is COptimataModulDlg, and the new class I want to add is Manager.
I've tried writing this and it didnt work:
//Manager.h
#pragma once
class Manager
{
public:
Manager();
~Manager();
private:
COptimataModulDlg omg; //the problem is with this line
};
it works only when I move this line to manager.cpp and I don't understand why.

You probably didn't #include the file where COptimataModulDlg is defined in Manager.h? Maybe it is included in manager.cpp?

Related

C++ Create Instance of 2nd Class within Instance of 1st Class and Pass 1st Class as Paremeter

I'm using wxWidgets with CodeBlocks, and I'm trying to instantiate a class called FileManager as an attribute of the Frame for wxWidgets, and pass the wxFrame parent to the constructor of FileManager. The objective is to be able to able to refer to the wxFrame from within FileManager. I'm getting an error "FileManager does not name a type". Not sure what I'm doing wrong thanks
The project is called "MullSimple_CB" so the main frame class is "Mull_Simple_CBFrame" Here are the files.
So the main object is a wxFrame object of class MullSimple_CBFrame, defined inside MullSimple_CBMain. The class I want instantiated as a member of that class is FileManager
MullSimple_CBMain.h
#ifndef MULLSIMPLE_CBMAIN_H
#define MULLSIMPLE_CBMAIN_H
#include "non-wx/file_manager.h"
class MullSimple_CBFrame: public wxFrame
{
public:
MullSimple_CBFrame(wxWindow* parent,wxWindowID id = -1);
virtual ~MullSimple_CBFrame();
// ERROR ON THIS LINE:
// "'FileManager' does not name a type"
FileManager fileManager(MullSimple_CBFrame parentFrame);
private
DECLARE_EVENT_TABLE()
};
#endif // MULLSIMPLE_CBMAIN_H
MullSimple_CBMain.cpp
#include "wx_pch.h"
#include "MullSimple_CBMain.h"
#include <wx/msgdlg.h>
#include "non-wx/file_manager.h"
MullSimple_CBFrame::MullSimple_CBFrame(wxWindow* parent,wxWindowID id)
{
FileManager fileManager(this);
}
FileManager.h
#ifndef FILE_MANAGER_H_INCLUDED
#define FILE_MANAGER_H_INCLUDED
#include "../MullSimple_CBMain.h"
class FileManager
{
private:
MullSimple_CBFrame storeMainFrame;
public:
// constructor
FileManager(MullSimple_CBFrame mainFrame);
};
#endif // FILE_MANAGER_H_INCLUDED
FileManager.cpp
#include "file_manager.h"
#include "../MullSimple_CBMain.h"
FileManager::FileManager(MullSimple_CBFrame mainFrame): storeMainFrame(mainFrame))
{
}
Your file_manager.h file includes MullSimple_CBMain.h and the MullSimple_CBMain.h file includes your file_manager.h.
You end up with a never ending chain of includes... which never resolve.
Consider putting forward declarations into a single .h file and then have your .h files only include it as opposed to including the individual class.h files themselves. The only time you need to include the class.h files themselves is if you need the compiler to know about the full definition of the class as opposed to just knowing the class exists at all.

Creating a cross platform c++ touch manager. Passing Objective-c objects in c++ involved code

So I am trying to create a multitouch class that can be used cross platform (iOS, Android, etc). The goal is that the engine does not need to know the platform in order to get updated touch data.
Right now I am doing the general design and am specifically implementing the iOS side. The Android etc side will come much later.
In order to do this I am trying to use the delegation pattern. There are really only two interface items.
There is a method update(float dt) to be called on each new frame with the dt. This method then will return a c++ struct of information about all the touches or perhaps set a property that can be accessed. I am not sure.
On the iOS side the only other function that needs to be called from outside is touchesDown which passes in a NSSet* of the UITouches and the UIView that they are in.
Here is the design I am working with thus far
Multitouch\
PlatformTouchManager.h (C++ Abstract Class with Update)
iOS\
iOSMultiTouch.cpp (Objective-c++)
iOSMultiTouch.hpp (Objective-c++ Class inheriting PlatformTouchManager)
MultTouch.cpp (C++)
MultiTouch.h (C++ class)
Here is PlatformTouchManager.h
#define MAX_TOUCHES 5
#define PLATFORM_iOS
//#define PLATFORM_ANDROID
class PlatformTouchManager {
public:
// Update All The Events
virtual void update(float time) = 0;
};
And of course Multitouch.h
class Multitouch {
private:
PlatformTouchManager* manager;
public:
Multitouch() {
#ifdef PLATFORM_iOS
manager = new iOSMultiTouch();
#endif
}
~Multitouch() {
delete manager;
}
Multitouch(const Multitouch&) = delete;
void update(float dt);
void* getManager() {
return manager;
}
};
And finally iOSMultiTouch.hpp
#include <stdio.h>
#include "../PlatformTouchManager.h"
#include <set>
#import <UIKit/UIKit.h>
class iOSMultiTouch: public PlatformTouchManager {
public:
// Initializer
iOSMultiTouch();
// Destructor
~iOSMultiTouch();
// Update All The Events
void update(float time);
// Touch Down Set
void touchesDown(id<NSSet> set, id<UIView> view);
};
It may be worth noting that this code is included by MultiTouch.h (c++) and my native view code which is objective-c++.
The cross platform engine thus creates a Multitouch object, keeps a reference and every frame calls update.
I am imagining that then iOS UIView will get the Multitouch* from the cross platform engine. Cast it into a iOSMultiTouch* object and call touchesDown:(NSSet *)touches withEvent:(IUView *)view on the delegate.
I am having a serious problem doing this.
If I try to import UIKit inside iOSMUltiTouch.hpp it causes build chaos so it would appear I can only import it inside of the source file. Therefore my touch down method needs to be void touchesDown(void* touches, void* view);.
The problem is then implementing this as when I try to cast these void* pointers into their proper types I get the warning Cast of C pointer type void* to objective-c pointer type id requires a bridged cast.
I understand that there are some ARC concerns here as ARC cannot track such things when casted into a primitive pointer but how do I get around this?
iOSTouchManager does indeed need to hold a strong reference to the UITouch objects up until after the frame the touch is released.
How do I implement this cast? Is there a different way I should be designing my classes to make this easier?
For those of you who were wondering these are the errors that pop up should you import uikit from iOSMultiTouch.hpp
Parse Issue
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:512:1: Expected unqualified-id
../Multitouch/MultiTouch.cpp:9:10: In file included from ../Multitouch/MultiTouch.cpp:9:
../Multitouch/MultiTouch.h:15:10: In file included from ../Multitouch/MultiTouch.h:15:
../Multitouch/iOS/iOSMultiTouch.hpp:15:9: In file included from ../Multitouch/iOS/iOSMultiTouch.hpp:15:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:9:1: Expected unqualified-id
../Multitouch/MultiTouch.cpp:9:10: In file included from ../Multitouch/MultiTouch.cpp:9:
../Multitouch/MultiTouch.h:15:10: In file included from ../Multitouch/MultiTouch.h:15:
../Multitouch/iOS/iOSMultiTouch.hpp:15:9: In file included from ../Multitouch/iOS/iOSMultiTouch.hpp:15:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:
Semantic Issues
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:514:9: Unknown type name 'NSString'
../Multitouch/MultiTouch.cpp:9:10: In file included from ../Multitouch/MultiTouch.cpp:9:
../Multitouch/MultiTouch.h:15:10: In file included from ../Multitouch/MultiTouch.h:15:
../Multitouch/iOS/iOSMultiTouch.hpp:15:9: In file included from ../Multitouch/iOS/iOSMultiTouch.hpp:15:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h:8:
Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:9: In file included from Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
Problem
You got ton of errors because you imported iOS framework (UIKit) inside a C++ file (iOSMultiTouch.cpp).
Solution
For implementation files which you need to import iOS frameworks to, you have to use .mm instead of .cpp extension. .cpp is C++ extension, not Objective-C++ extension.
In this case, you need to rename iOSMultiTouch.cpp to iOSMultiTouch.mm. Replace void * with Objective-C pointers and it will work as expected.
Utils.h will keep nothing related to Objective-C and can be used anywhere. Utils.mm is used to work with Objective-C parts.
Utils.h
PlatformTouchManager* GetIOSTouchManager();
Utils.mm
#include "Utils.h"
#include "iOSMultiTouch.hpp"
PlatformTouchManager* GetIOSTouchManager() {
return new iOSMultiTouch();
};
Multitouch.h
#include "Utils.h"
// Other include
class Multitouch {
private:
PlatformTouchManager* manager;
public:
Multitouch() {
#ifdef PLATFORM_iOS
manager = GetIOSTouchManager();
#endif
}
~Multitouch() {
delete manager;
}
Multitouch(const Multitouch&) = delete;
void update(float dt);
PlatformTouchManager* getManager() {
return manager;
}
};

Virtual Classes in Eclipse

I'm having a problem with Eclipse's indexing. Here's a minimal working example.
I create a header file, Parent.h, in which I define the following virtual class:
class Parent
{
protected:
virtual ~Parent() {}
public:
virtual void OverrideMe() = 0;
};
Then, I create another header file, Child.h:
#include "Parent.h"
class Child : public Parent
{
};
Immediately, Eclipse complains that "Symbol Parent could not be resolved."
The problem magically goes away if I create a source file Parent.cpp and in it put #include "Parent.h". More strangely, it does not reappear if I delete this source file.
There seems to be something crazy going on with Eclipse's indexing? People with similar problems have recommended doing an "Index -> Rebuild". This does not work for me.
Any help is appreciated. Thanks in advance.

Can't acces property c++

This should be a C++ specific.
I have a property m9ReloadAnim in the header file, I can access it from the constructor, but when I try to access it from an other function I get an error like: EXC_BAD_ACCESS or something like: "The address does not contain an object ".
I have a header class like this:
#ifndef __SWAT__Weapon__
#define __SWAT__Weapon__
#include "cocos2d.h"
class Weapon : public cocos2d::CCSprite
{
private:
cocos2d::CCAnimation *m9ReloadAnim = cocos2d::CCAnimation::create();
public:
Weapon();
~Weapon();
void reloadM9();
};
#endif
And a cpp file like this:
enter code here
#include "Weapon.h"
#include "cocos2d.h"
Weapon::Weapon(){
m9ReloadAnim->setDelayPerUnit(1.1f);
}
Weapon::~Weapon(){
}
void Weapon::reloadM9(){
m9ReloadAnim->setDelayPerUnit(1.1f);
}
You could not initialize variable like this:
cocos2d::CCAnimation *m9ReloadAnim = cocos2d::CCAnimation::create();
Only static const int could be init in class declaration.
Move this init to your ctor:
Weapon::Weapon()
: m9ReloadAnim(cocos2d::CCAnimation::create())
{
m9ReloadAnim->setDelayPerUnit(1.1f);
}
or
Weapon::Weapon()
{
m9ReloadAnim = cocos2d::CCAnimation::create();
m9ReloadAnim->setDelayPerUnit(1.1f);
}
Sometimes the becomes corrupted so you can't tell where errors originate. I would suggest to put a breakpoint at the entry point of each method, and step the code line by line to make sure that it's triggering in the reloadM9 method. Check to see the m9ReloadAnim is NULL or if it points to the object created at initialization. Additionally you need to check if you are using the library properly.

C++ - How to call creator class/object

I need to call properties and functions of an object from a different class.
The idea is passing 'this' as a parameter to the other class constructor. E.g.:
instance = ClassName(this);
And then do:
ParentClass parentInstance;
ClassName::ClassName(MainApp _instance){
parentInstance = _instance;
}
However, my compiler says that ParentClass does not name a type. Ideas?
Also, should I use a pointer to save memory? How?
Thanks in advance.
UPDATE:
Ok, sorry for the delay. Here it goes the actual code. First, a simple class.
Game class:
Header file
#ifndef _GAME
#define _GAME
#include "ofMain.h"
class Game{
public:
Game();
~Game();
void hi();
};
#endif
cpp file:
#include "Game.h"
Game::Game(){}
Game::~Game(){}
void Game::hi(){
cout << "hi, I'm game! " << endl;
}
Then, from MainApp I create the object:
- Relevant code on header file:
#ifndef _MAIN_APP
#define _MAIN_APP
#include "ofMain.h"
#include "Game.h"
class MainApp : public ofSimpleApp{
public:
Game game;
};
#endif
Relevant code on the cpp file:
game = Game();
game.hi();
This obviously works as I'm only creating a bloody object. However, problem comes with composition.
I could pass the main app as argument in the constructor, I could pass it via game.setParent(this);... problem is, I can't even define the variable to store the reference to the app.
E.g.: (making it easy/inefficient without pointers or anything)
Game.h:
#define _GAME
#ifndef _GAME
#include "ofMain.h"
#include "MainApp.h"
class Game{
MainApp app;
public:
Game();
~Game();
void hi();
};
#endif
This returns a "does not name a type" error and declaring class MainApp returns an "incomplete type" error
I'm sure I'm doing something dumb.
UPDATE 2:
The problem with that method is that I can't call a function of the pointed object now.
This is Game.h:
#ifndef _GAME
#define _GAME
#include "ofMain.h"
class MainApp;
class Game{
public:
Game();
Game(MainApp* _app);
~Game();
void hi();
MainApp* app;
};
#endif
As you see, app (of the type MainApp) is passed as a parameter. That's fine, MainApp exists as it's the forward declaration. However, when I try to call any of app's functions I can't (compiler error saying Request for member appHi in .... which is non-class type 'MainApp'.
MainApp is NOT included in Game.h but Game.h IS included in MainApp.h.
Ideas?
The problem is you have a circular reference - Game includes MainApp, and MainApp includes game. You need a 'forward declaration', as per the example by DeadMG.
See here.
It's called composition and is a common pattern. It's highly efficient in both semantics and in terms of runtime speed/memory footprint.
Your code example is a little too much pseudocode for me to read it correctly. Let me show you how it's done.
class X;
class Y {
...
void DoSomething(X* x, ... args);
};
class X {
Y y;
void DoSomething() {
y.DoSomething(this, args);
}
};
I think there may be two issues here:
1) You need to declare the ParentClass (i.g. #include its .hpp-file) before using it
2) The assignment "parentInstance = _instance" will invoke the assignment operator, which i'm guessing is not what you want. let "parentInstance" be a pointer instead.
Note the section on "#include."
http://www.cplusplus.com/doc/tutorial/program_structure/
After the "Intro to the C++ Language" section look for the verbiage about #include.
http://www.cprogramming.com/tutorial/lesson1.html
Namespaces:
http://www.tenouk.com/Module23.html
HTH
That's not how things work in C++. Unlike javascript, you cannot inject methods or fields into existing objects at runtime.
Madsen is on the right track here, but we need more code; What is the class heirarchy of ParentClass, ClassName and SaleraApp. Which classes are base and/or dervied?
When you write: parentInstance = _instance; the compiler will try to generate a default copy constructor if one is not defined. Your problem might be that you are trying to create a dervied class object from a base class pointer.
Also, "this" is a pointer.
If all you need to do is use functions and data members of another class, read up on the friend keyword. It will allow access to class members from other classes.
UPDATE: Alternatively, store a pointer or reference to the object you need access to, and make getters for data members and make the functions public... but I get the feeling this is not what you're after...