C++ -> Visual Studio 2013 -> Windows Form -> Objects accessed through methods - c++

Hello once again I am asking some experts for help. This time it is not about Linux commands but Visual Studio 2013 Windows Form in C++.
As always, details :
I have project with 2 forms, that's not really important.
Important things are :
I have 6 files : Windows.h, Windows.cpp, Game.h, Game.cpp, Test.h,Test.cpp
Including : Windows.cpp includes Windows.h, Windows.h includes Game.h, Game.h includes Test.h, Test.cpp includes Test.h as well.
Windows.h and Game.h are form declarations. This is where I need work to be done.
Windows. cpp is used as main, it executes whole project and do stuff.
Windows.h is Form for Menu, it just connects to Game.h Form and becomes hidden when Game appears.
Game.h is Form with game. I need to create and operate on objects in methods included in Forms connected to Buttons.
Example : Click on one button creates one object class Test with variable int number = 1 and click one other button changes this variable to 2.
I can't access same object through methods, all I can do is create two same objects and operate on them but they are different beings declared in other method. Is there any solution to construct object which can be accessed through every Form's method ?
Test.h
#pragma once
ref class Test
{
public:
int nr;
char *test;
Test();
Test(int n, char *t)
{
nr = n;
test = t;
}
};
Test.cpp
#include "Test.h"
Test::Test()
{
}

I declared objects in class Game as variables what gave me access to those objects in Game's method.

Related

Call c++ function in Widget blueprint

I create widget, inherited from C++ class below.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Widget_Manager.generated.h"
UCLASS()
class CRY_API UWidget_Manager : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, Category ="test")
int32 x;
UFUNCTION(BlueprintCallable, Category = "test")
int32 increase_x(int32 i);
};
I want to output the value of x in value1, then increase it, and output it to value2.
My problem is that the blueprints do not see the function increase_x().
Not in elements list, not in function search.
Also i tried property CallInEditor.
How I can call this function in widget?
Please make sure that your not using Hot Reload (the Compile C++ button in the Editor).
If you make changes to your C++ code, especially Headers, always close the Editor and recompile it from Visual Studio. Hot Reload cannot resolve changes to Header files, which means things like Functions and new Properties will not be available in Blueprint if your using Hot Reload until you do a recompile from Visual Studio.
Also its best to leave Context Sensitivity on.

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.

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.

Why Qt default project use separate header files for manwindow.cpp?

I just created a Qt default project with a Qt designer form.
The class MainWindow is declared in a mainwindow.h and then included in mainwindows.cpp.
Why is it done this way ? Why not a declaration of this form directly in mainwindows.cpp ?:
class MainWindow
{
...
}
What is the proper way to add my code ? For example, a button that trigger a method.
In C++ you typically put class definitions into header files (.h), and method implementations in source files (.cpp). That allows clients of the class to use the class without having to see the implementation of each function. That also means that when adding a method, you'll typically have to make two changes: add the method to the class definition (in the header) and then add the method's implementation to the .CPP file.
In header file:
class MainWindow
{
void SomeMethod();
};
In source file:
void MainWindow::SomeMethod()
{
// Your code here.
}
The definition of MainWindow class is needed in another file, where an instance of it is constructed in the main function and then shown. That's why the class needs to be defined in a header file.
There are a number of ways to add your own code: for the button you described you could create in entirely in the QtCreator UI, or you could create it "programmatically" in the MainWindow constructor.