Using unmanaged c++ objects in /clr - c++

I've got some classes like class below written in c++ and i have to implement them into the Windows Forms. Is there any solution to create unmanaged objects in Windows Forms /clr classes?
#pragma once
#ifndef _HOTEL_H
#define _HOTEL_H
#include "Room.h"
#include "Adress.h"
#include "Employee.h"
#include "Apartament.h"
#include "TechnicalRoom.h"
#include "RecreationRoom.h"
#include <vector>
#include <string>
using namespace std;
class Hotel {
protected:
int HotelID, HotelStars, NumberOfEmployee, NumberOfClients, NumberofRooms;
string HotelName;
Adress HotelAdress;
vector <Room*> Rooms;
vector <Person*> People;
public:
//methods
Hotel(int = 3, string = "Hotel");
~Hotel();
string getName();
int getNumberOfClients();
int getNumberOfEmployee();
int getHotelStars();
void changeNumberOfStars(int);
void BookApartament(int, int);
void AddRoom(int);
void DeleteRoom(int);
void AddEmployee();
void DeleteEmployee(int);
friend ostream & operator<< (ostream &out, Hotel &h);
friend ref class MainWindow;
};
#endif

It sounds like you might want something along the lines of:
namespace SomeCompany
{
public ref class Hotel
{
::Hotel* pHotel;
public:
Hotel() : pHotel(new ::Hotel()) {}
~Hotel() {
delete pHotel;
pHotel = nullptr;
}
!Hotel() {
delete pHotel;
}
// ... etc. ...
};
}
See How to: Wrap Native Class for Use by C# for many more details.

Related

Using friend function correctly across multiple Classes in C++

I am trying use a friend function. The function should be a friend to all the classes that i have. But i get multiple errors some of which says incomplete type. The following are files that i have:
main.cpp
#include <iostream>
#include "my_ClassA.h"
#include "my_ClassB.h"
#include "my_ClassC.h"
#include "my_ClassD.h"
int main()
{
std::cout<<"Hello World";
my_namespace::my_ClassA object1;//this will do some computation and set everything up for object1
my_namespace::my_ClassB object2(object1);//this object2 will use object1 to further do other computation
my_namespace::my_ClassC object3(object2);
my_namespace::my_classD object4(object4);
//runComputation(object1, object2, object3, object4);
return 0;
}
my_ClassA.h
#pragma once
#include<string>
#include <vector>
//these three includes are for friend function BUT result in error incomplete type
#include "my_ClassB.h"
#include "my_ClassC.h"
#include "my_ClassD.h"
/////////////////////////////////////////////
namespace my_namespace{
class my_ClassA{
friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
private:
std::string name;
std::vector<std::string> vec;
public:
std::vector<std::string> get_vec();
};
void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
}
my_ClassA.cpp
#include "my_ClassA.h"
std::vector<std::string> my_namespace::my_ClassA::get_vec(){
return vec;
}
my_ClassB.h
#pragma once
#include<string>
#include <vector>
#include "my_ClassA.h"
#include "my_ClassC.h"
#include "my_ClassD.h"
namespace my_namespace{
class my_ClassB{
friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
private:
std::string name;
std::vector<std::string> vec;
public:
std::vector<std::string> get_vec();
my_ClassB(my_ClassA);
my_ClassB(){
;
}
};
}
my_ClassB.cpp
#include "my_ClassB.h"
std::vector<std::string> my_namespace::my_ClassB::get_vec(){
return vec;
}
my_namespace::my_ClassB::my_ClassB(my_ClassA temp_objA){
;
}
my_ClassC.h
#pragma once
#include<string>
#include <vector>
#include "my_ClassB.h"
#include "my_ClassA.h"
#include "my_ClassD.h"
namespace my_namespace{
class my_ClassC{
friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
private:
std::string name;
std::vector<std::string> vec;
my_ClassB objB;
public:
my_ClassC(my_ClassB);
std::vector<std::string> get_vec();
};
}
my_ClassC.cpp
#include "my_ClassC.h"
#include "my_ClassB.h"
std::vector<std::string> my_namespace::my_ClassC::get_vec(){
return vec;
}
my_namespace::my_ClassC::my_ClassC(my_ClassB temp_objB){
;
}
my_ClassD.h
#pragma once
#include<string>
#include <vector>
#include "my_ClassA.h"
#include "my_ClassB.h"
#include "my_ClassC.h"
namespace my_namespace{
class my_ClassD{
friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
private:
std::string name;
std::vector<std::string> vec;
my_ClassA objA;
public:
std::vector<std::string> get_vec();
};
}
my_ClassD.cpp
#include "my_ClassD.h"
std::vector<std::string> my_namespace::my_ClassD::get_vec(){
return vec;
}
I tried using the getters of each of the classes in the main.cpp. But some my classes have large size vectors,sets etc etc. So i do not want to copy them again and again. So instead i want to access the data members of the classes directly in a function called runComputation. And that function will be taking the object created as references so that copy doesn't happen inside main.cpp.
What i have is this: First i create different objects which may take the previously created object as input in main.cpp. After all the objects are created successfully, i want to run some computation on those objects. Now the problem is that i can use the getters and setters of the objects created in the main.cpp file. But the objects have large vectors and other objects inside them, and so they will be copied each time i use them in a for loop using getters. To avoid this i want to create a friend function that can take these objects as references and avoid copying. How can i resolve this ? And is there any better way of achieving this?
PS: I am aware of ADL. Will ADL be used when i write a friend declaration like friend void runComputation(someobject&); and then after the class to make this friend function visible void runComputation(someobject&);
Make a my_forwards.h header file. It contains
namespace my_namespace {
class my_ClassA;
class my_ClassB;
class my_ClassC;
class my_ClassD;
}
(or just write this manually at the top of every header file).
Now don't include my_ClassA.h in the other headers at all. If you have the body of a function in my_ClassB that requires the definition of my_ClassA, put it in a .cpp file instead of in the header.
// my_forwards.h
namespace my_namespace {
class my_ClassA;
class my_ClassB;
class my_ClassC;
class my_ClassD;
void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
}
// my_ClassA.h
#pragma once
#include<string>
#include <vector>
#include "my_forwards.h"
namespace my_namespace{
class my_ClassA{
friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
private:
std::string name;
std::vector<std::string> vec;
public:
std::vector<std::string> get_vec();
};
}
// my_ClassA.cpp
#include "my_ClassA.h"
// include other my_ClassX here if needed, after my_ClassA.h
// implementation of my_ClassA:
std::vector<std::string> my_namespace::my_Class::get_vec() {
return vec;
}
In some cases this may require declaring a destructor/constructor in a header file, and doing a my_ClassA::my_ClassA()=default; in the cpp file.
Now, your my_ClassC has a member variable of type my_ClassB. This is a case where you have to #include "my_ClassB.h" in the my_ClassC.h header file, because my_ClassC needs the definition of my_ClassB. But in most cases, either a forward declaration is good enough, or slight modification (changing a value parameter to a reference one, for example) is good enough that you don't have to cross-include the header files.
Don't use return by value to avoid copying large vectors, return const reference to access data or non-const reference to modify.
E.g., header:
#pragma once
#include<string>
#include <vector>
//these three includes are for friend function BUT result in error incomplete type
#include "my_ClassB.h"
#include "my_ClassC.h"
#include "my_ClassD.h"
/////////////////////////////////////////////
namespace my_namespace{
class my_ClassA{
friend void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
private:
std::string name;
std::vector<std::string> vec;
public:
// use this getter to modify inner data by reference
std::vector<std::string>& get_vec();
// use this getter to access data by const referenct
std::vector<std::string> const & get_vec() const;
};
void runComputation(my_ClassA&,my_ClassB&,my_ClassC&,my_ClassD&);
}
implementation:
#include "my_ClassA.h"
std::vector<std::string>& my_namespace::my_ClassA::get_vec() {
return vec;
}
std::vector<std::string> const & my_namespace::my_ClassA::get_vec() const {
return vec;
}
You may want to refactor classes to a chain of function calls:
// class or struct which contains intermediate state
State state;
runComputation1(&state);
runComputation2(&state);
runComputation3(&state);
runComputation4(&state);
The problem is due to circular dependency of different files. Instead of including the headers just use forward declarations for different class parameters and the issue is resolved.

Change value of Object Variable through a vector

I'm developing a simulation game in cpp using Visual Studio 2017 for School and in the development phase I got stuck in this situation.
So, what I did was create a new project to try and recreate that issue in the simplest form, so that it would be easier to debug.
Below is the main file and all the associated source codes:
main.cpp
#include "header.h"
#include "Vehicle.h"
#include "Car.h"
int main() {
Vehicle v;
v.addCar(1);
v.addCar(2);
v.addCar(3);
cout << v.getCars()[1].id << endl;
v.getCars()[1].id = 99;
cout << v.getCars()[1].id << endl;
system("pause");
return 0;
}
header.h
#ifndef CLUSTE2R_H
#define CLUSTE2R_H
#pragma once
#include <iostream>
#include <vector>
using namespace std;
#endif
Car.h
#ifndef CLUSTE1R_H
#define CLUSTE1R_H
#pragma once
#include "Vehicle.h"
using namespace std;
class Car : public Vehicle
{
public:
int id;
Car(int id);
~Car();
};
#endif
Car.cpp
#include "Car.h"
Car::Car(int id)
{
this->id = id;
}
Car::~Car()
{
}
Vehicle.h
#ifndef CLUSTER_H
#define CLUSTER_H
#pragma once
#include <vector>
//#include "Car.h"
class Car;
using namespace std;
class Vehicle
{
private:
vector<Car> cars;
public:
Vehicle();
~Vehicle();
vector<Car> getCars();
void addCar(int id);
};
#endif
Vehicle.cpp
#include "Vehicle.h"
#include "Car.h"
#include <vector>
using namespace std;
//class Car;
Vehicle::Vehicle()
{
}
Vehicle::~Vehicle()
{
}
vector<Car> Vehicle::getCars()
{
return this->cars;
}
void Vehicle::addCar(int id)
{
Car c(id);
cars.reserve(cars.size() + 1);
cars.push_back(c);
}
So, what I'm trying to do is to get the following output:
2 \n 99
This is what I'm getting:
2 \n 2
What am I doing wrong? I believe the issue is associated with the main.cpp file. But I'm not quite sure how to achieve what I want in any other way...
Currently, you are returning a new instance of a vector when you call getCars() function from your Vehicle, this means that all changes to the vector will not be applied to the original vector in the class.
To fix this you could just return a reference of the vector(changing the vector<Car> getCars(); to std::vector<Car>& getCars()).
You could also make a local copy of the vector and then setting the vector to the class.

how to use pointer to ostream in class

I need ostream pointer in class which will be created at the time of construction of class.
My code is :
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <climits>
#include <cstdio>
#include <fstream>
using namespace std;
class test2_t {
public:
test2_t ()
{
std::filebuf fb;
fb.open ("dump.txt",std::ios::out);
ostream *output_ = new std::ostream(&fb);
}
virtual ~test2_t ()
{}
ostream *output_;
void printing()
{
print(output_);
}
void print(ostream *out)
{
*out<<"dump data"<<"\n";
}
private:
/* data */
};
int main( )
{
test2_t obj;
obj.printing();
}
But is getting Segmentation fault I don't know why. Please help me out.
You made the following mistake in your code:
You "redeclared" your "Output"-variable in the constructor - so the pointer ios only stored in a local variable within the constructor-scope.
change this line: ostream *output_ = new std::ostream(&fb);
into: *output_ = new std::ostream(&fb);
In this way, the member-variable of your class is filled with the correkt pointer.
You can change your construtor function like follows to get it working:
test2_t () : output_(new std::ofstream("dump.txt")) {
}
Don't forget to release the resource in the destructor:
virtual ~test2_t () {
delete output_;
}

Debugging C++ compiler error

I'm still a noobie in c++ so I am not to skilled in debugging yet. Just trying to figure out how to fix this compilation error.
CruiseShip.cpp:11: error: expected ā€˜)ā€™ before ā€˜nā€™
CruiseShip.cpp
#include "CruiseShip.h"
#include "Ship.h"
#include <iostream>
using namespace std;
Ship s;
int passengers;
CruiseShip(string n, string y, int p) : Ship(n,y)
{
passengers=p;
}
void print()
{
cout<<"Name: "<<s.getName()<<"\nMaximum passengers:"<<passengers<<endl;
cout<<"-------------------------"<<endl;
}
CruiseShip.h
#ifndef CRUISESHIP_H
#define CRUISESHIP_H
#include "Ship.h"
#include <string>
using namespace std;
//class Ship;
class CruiseShip:public Ship{
private:
int passengers;
Ship::Ship s;
public:
CruiseShip(string, string, int);
virtual void print();
};
#endif
Ship.h
#ifndef SHIP_H
#define SHIP_H
#include <string>
using namespace std;
class Ship{
private:
string name;
string built;
public:
Ship();
Ship(string, string);
string getName();
string getBuilt();
virtual void print();
};
#endif
You have 3 errors:
1 and 2. You don't declare print and CruiseShip (The constructor) as part of the class CruiseShip when you define them. You need to:
CruiseShip::CruiseShip(string n, string y, int p) : Ship(n,y) {
virtual void CruiseShip::print() {
3, you dont have a namespace Ship so this is unnecessary:
Ship::Ship s; // This only needs to be Ship s <- NameSpace::ObjectType nameOfObject;
After this it will compile http://ideone.com/wJ6mPO. It will not link however, because you have undefined references to all of the functions you have yet to define.

Constructor of a children class that have an array that contains objects of another class

Dialog.h
#include "WBasic.h"
#include "WButton.h"
#include "WData.h"
#ifndef WDIALOG_H_INCLUDED
#define WDIALOG_H_INCLUDED
class WDialog : public WBasic
{
private:
WButton wB;
WData wD;
public:
//Constructor
WDialog(const int& e = 0, const WButton& = WButton(0,0), const WData& = WData(0,0,0));
~WDialog();
};
#endif // WDIALOG_H_INCLUDED
Dialog.cpp
#include <iostream>
#include "WDialog.h"
WDialog::WDialog(const int& e, const WButton& WBUTTON, const WData& WDATA) :
WBasic(e), wB(WBUTTON), wD(WDATA)
{
}
The code above works great, however I'm trying to make "WButton wB" a vector changing it to"WButton wB[3];"
class WDialog : public WBasic
{
private:
WButton wB[3];
WData wD;
};
But then I've no idea how deal with the Constructor.
You can use vector to solve this problem.
I have written a small example below.
#include <iostream>
#include <vector>
using namespace std;
class A{
};
class B{
public:
B():vec (4,A())
{
}
private :
vector<A> vec;
};
int main() {
// your code goes here
B obj();
return 0;
}
You can observe how I have initialized vector vec with three class A object.
In my opinion if you can (your compiler support C++11) prefer std::array
#include <array>
std::array<WButton, 3> wB;
Then in your contructor use an initializer list:
WBasic(e),
wB{WButton(...), WButton(...), WButton(...)},
wD(WDATA)