I created a class that represents a packet of information as described on this code:
#ifndef PACKET_H_
#define PACKET_H_
namespace std {
class Packet
{
public:
Packet();
virtual ~Packet();
void initClass();
void setStartP(char);
void setAddFrom(char);
void setAddTo(char);
void setpDataSize(char);
void setpNumber(char);
void setChecksum(char);
void setEndP(char);
void LoadData(char);
char getStartP();
char getAddFrom();
char getAddTo();
char getpDataSize();
char getChecksum();
char getEndP();
char getData();
private:
char pB[261];
char pDataMax;
char pDataIndex;
};
} /* namespace std */
#endif /* PACKET_H_ */
#include "Packet.h"
#include <iostream>
namespace std {
Packet::Packet()
{
pDataIndex = 0;
initClass();
}
Packet::~Packet()
{
delete this;
}
void Packet::setStartP(char startChar)
{
pB[0] = startChar;
cout << "in Set!";
}
void Packet::setAddFrom(char fromChar)
{
}
void Packet::setAddTo(char toChar)
{
}
void Packet::setpDataSize(char dataSizeChar)
{
}
void Packet::setpNumber(char packetNumber)
{
}
void Packet::setChecksum(char checksumChar)
{
}
void Packet::setEndP(char endChar)
{
}
void Packet::LoadData(char dataChar)
{
}
char Packet::getStartP()
{
return pB[0];
cout << "in Get";
}
char Packet::getAddFrom()
{
return pB[1];
}
char Packet::getAddTo()
{
return pB[2];
}
char Packet::getpDataSize()
{
return pB[3];
}
char Packet::getChecksum()
{
return pB[4];
}
char Packet::getEndP()
{
return pB[260];
}
char Packet::getData()
{
return pB[6 + pDataIndex];
}
void Packet::initClass()
{
pDataMax = 254;
pDataIndex = 0;
}
}
At this point i am just testing it so I just implemented two of the methods. When I try to run the program:
#include <iostream>
#include "Packet.h"
using namespace std;
Packet myPacket;
void buildPacket();
int main() {
buildPacket();
return 0;
}
void buildPacket( )
{
char startP = 0x28;
cout << "Setting startP!" << endl;
myPacket.setStartP(startP);
cout << "Getting startP" << endl;
cout << myPacket.getStartP() << endl;
cout << "Done";
}
The code is fine a compile/build time no issues there, it is a run time it falls over. This is really thruowing me, it really is making me doubt what I actually know about class creation and use in C++.
The program will run up to a certain point and then crashes with a windows message. on the console this is as far as it gets before crashing:
Setting startP!
in Set!Getting startP
(
As I can see it it seems to be on deletion that it crashes but not sure why. I looked around for similar issues but can't really find a reason why it is coming up with this, I would be grateful for some help on this one.
Don't call delete this in the destructor. The object is automatically destructed since it goes out of scope, no need for delete.
You can read more about it here: http://en.cppreference.com/w/cpp/language/scope
Related
For example, I have such function which performs some useful work (for event-driven simulation):
int function()
{
do_useful_work();
return 0;
}
If I need do measurements of performance of this useful_work I should do:
int function()
{
count_time(time_before);
count_X_metrics(X_before);
do_useful_work();
count_time(time_after);
count_X_metrics(X_after);
return 0;
}
This approach makes code more clumsy. Is there a way, patters to do these countings outside of int function() to make code clearer?
You could create your own decorator like the following:
#include<functional>
#include <iostream>
void count_time() {};
void count_X_metrics() {};
void decorator(std::function<void()> work)
{
count_time();
count_X_metrics();
work();
count_time();
count_X_metrics();
}
void do_work_1() {
std::cout << "Hello, World 1!" << std::endl;
}
void do_work_2() {
std::cout << "Hello, World 2!" << std::endl;
}
int main() {
decorator(do_work_1);
decorator(do_work_2);
}
Edit: I'm not sure how your count_time and count_X_metrics functions work, but if you need something more complicated, or a way to keep track of state, you can create an object that will do that work for you. This is certainly different than you need, but hopefully it conveys the point I am trying to make:
#include<functional>
#include <iostream>
int current_time() { return 0; }
int x_metric() { return 0; }
class Timer {
public:
void time(std::function<void()> work) {
// Capture state before
int starttime = current_time();
int startmetric = x_metric();
work();
// Capture state after
int endtime = current_time();
int endmetric = x_metric();
// Update results
ellapsed = endtime - starttime;
metric = endmetric - startmetric;
// Possibly do something with the metrics here.
// ...
}
int get_ellapsed() { return ellapsed; }
int get_metric() { return metric; }
private:
int ellapsed;
int metric;
};
void do_work_1() {
std::cout << "Hello, World 1!" << std::endl;
}
void do_work_2() {
std::cout << "Hello, World 2!" << std::endl;
}
int main() {
Timer t;
t.time(do_work_1);
// Possibly do something with the metrics here.
// cout << t.get_ellapsed();
t.time(do_work_2);
}
In the following code, my goal was to test the outerFunction(myTest).setPrivateVar(5); line. I'm not sure why, but calling this function doesn't change the privateVar member of myTest to 5, but calling cout << outerFunction(myTest).readPrivateVar properly displays the value 200. Can anybody explain why this is?
#include <iostream>
using namespace std;
class Test {
private:
int privateVar;
public:
void setPrivateVar(int);
int readPrivateVar();
};
void Test::setPrivateVar(int privateVarSet) {
privateVar = privateVarSet;
}
int Test::readPrivateVar() {
return privateVar;
}
Test outerFunction(Test passedTest) {
return passedTest;
}
int main(int argc, char* args[]) {
Test myTest;
myTest.setPrivateVar(200);
cout << myTest.readPrivateVar() << endl;
outerFunction(myTest).setPrivateVar(5);
cout << outerFunction(myTest).readPrivateVar() << endl;
return 0;
}
Output:
200
200
You are making copies here, both when you pass something into the function and when you return it:
Test outerFunction(Test passedTest) {
return passedTest;
}
Modifications to outerFunction(myTest) affect the copy, not the original.
To get the semantics you seem to be after, you need to use references:
Test& outerFunction(Test& passedTest) {
return passedTest;
}
This question already has answers here:
Need help regarding saving variables via fstream, do I need to use vector?
(2 answers)
Closed 8 years ago.
I got this problem, where I want to make a Devicelist, which I do with a vector of Devices. But I can't make it read/write correctly.
This is my functions:
#include "Devicelist.h"
bool Devicelist::AddDevice(const char *deviceName, char *type)
{
Device tempDevice(deviceName, type, ++id_);
Devicelist_.push_back(tempDevice);
return true;
}
bool Devicelist::deleteDevice(const char *deviceName)
{
for (int i = 0; i < Devicelist_.size(); i++)
{
if (strcmp(deviceName, Devicelist_[i].getName()) == 0)
{
Devicelist_.erase(Devicelist_.begin() + i);
return true;
}
else
{
cout << "No Device found with that Devicename." << endl;
return false;
}
}
}
bool Devicelist::SaveToFile()
{
//remove("Devices.dat");
ofstream SaveFile("Devices.dat", ios::out | ios::binary);
if (!SaveFile)
{
cerr << "File could not be opened." << endl;
return false;
}
for (int i = 0; i < Devicelist_.size(); i++)
SaveFile.write((const char *)(&Devicelist_[i]), sizeof(Devicelist_[i]));
SaveFile.close();
return true;
}
bool Devicelist::LoadFromFile()
{
ifstream LoadFile("Devices.dat", ios::in | ios::binary);
if (!LoadFile)
{
cerr << "File could not be opened." << endl;
return false;
}
for (int i = 0; i < Devicelist_.size(); i++)
LoadFile.read((char *)(&Devicelist_[i]), sizeof(Devicelist_[i]));
LoadFile.close();
return true;
}
Device Devicelist::findDevice(const char *deviceName)
{
for (int i = 0; i < Devicelist_.size(); i++)
{
if (strcmp(deviceName, Devicelist_[i].getName()) == 0)
return Devicelist_[i];
else
cout << "Couldn't find device." << endl;
}
}
And this is my main:
#include "Devicelist.h"
void main()
{
Devicelist list;
list.AddDevice("Lampe3", "Lampe");
list.AddDevice("Lampe4", "Lampe");
list.SaveToFile();
Devicelist list2;
list2.LoadFromFile();
Device lampe = list2.findDevice("Lampe");
cout << lampe.getName() << endl;
cout << lampe.getID() << endl;
cout << lampe.getType() << endl;
}
Can anyone see what my problem is?
Thanks in advance!
Edit1:
My .h-file for Devicelist is:
#ifndef DEVICELIST_H
#define DEVICELIST_H
#include "Device.h"
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
class Devicelist
{
public:
Devicelist();
bool SaveToFile();
bool LoadFromFile();
bool AddDevice(const char *deviceName, char *type);
bool deleteDevice(const char *deviceName);
Device findDevice(const char *devicename);
private:
vector<Device> Devicelist_;
int id_;
};
#endif
This is my Device .h-file:
#ifndef DEVICE_H
#define DEVICE_H
#include <iostream>
#include <string>
#define DNAME_SIZE 33
using namespace std;
class Device
{
public:
Device(const char *devicename = "Default", char *type = "type", int id = 0);
const char *getName();
int getID();
int getType();
private:
char deviceName_[DNAME_SIZE];
int id_;
int type_;
};
#endif
And Device .cpp-file:
#include "Device.h"
Device::Device(const char *deviceName, char *type, int id)
{
strncpy_s(deviceName_, deviceName, DNAME_SIZE);
if (type == "Lampe")
type_ = 1;
else if (type == "Roegalarm")
type_ = 2;
else if (type == "Tyverialarm")
type_ = 3;
else
{
type_ = 0;
cout << "Type does not exists." << endl;
}
id_ = id;
}
const char *Device::getName()
{
return deviceName_;
}
int Device::getID()
{
return id_;
}
int Device::getType()
{
return type_;
}
The problem is when I try to read view what's saved in the file, this is the output:
http://imgur.com/P8WEAKq
The problem is here in the LoadFromFile function, I think:
for (int i = 0; i < Devicelist_.size(); i++)
LoadFile.read((char *)(&Devicelist_[i]), sizeof(Devicelist_[i]));
The DeviceList is empty, or has the wrong size.
You could write something similar as in AddDevice:
Device tempDevice();
LoadFile.read((char *)(&tempDevice), sizeof(tempDevice));
Devicelist_.push_back(tempDevice);
but the size (how many devices) of the file needs to be known.
As mentioned in a comment, this works only if Device doesn't have pointers to other objects for example. It should be a POD (Plain Old Data) object.
An elegant solution would be to implement streaming operators on Device
You will need to to have Device::StoreToFile write the number of devices before writing out each device. This will enable your Device::LoadFromFile to know how many device instances to read from the file.
I highly recommend you do not write a class or structure bit for bit to the file. If your class or struct has any advanced data structures, such as std::string or std::vector, you can't write them in binary. They may use pointers to memory, which will not be the same when you load them.
Here is a better solution:
1. Pass a buffer of uint8_t to the load and store methods.
2. The methods load from the buffer or store into the buffer (append).
3. The caller reads or writes the buffer to file.
4. The class also has a "size on file" method that returns the space occupied on a file, which may be different than the structure size.
Also search the web for "boost::serialization".
I have one application in which following task are to be done
1.) UI application will send command code (integer value).
2.) DLL interface(in c++) will get that integer value and execute corresponding command function.
commands name and command code are maintained as
#define PING 50
there will be 500 commands and applying SWITCH CASE will not sound good. so i decided to implement function pointer in my code as below
#include "stdafx.h"
#include<iostream>
#define PING 20
using namespace std;
//extern const int PING = 10;
void ping()
{
cout<<"ping command executed";
}
void get_status(void)
{
cout<<"Get_status called"<<endl;
}
class ToDoCommands
{
public:
void getCommand( void (*CommandToCall)() );
};
void ToDoCommands::getCommand( void (*CommandToCall)())
{
void (*CommandToCall1)();
CommandToCall1 = CommandToCall;
CommandToCall1();
}
int main()
{
int code;
ToDoCommands obj;
cout<<"enter command code";
cin>>code; // if UI send 50 then Ping function get executed as #define PING 50
obj.getCommand(ping); // here m passing ping manually..
//obj.getCommand(get_status);
return 0;
}
how can i pass command name corresponding to command code in
obj.getCommand(ping);
You are almost there: make a std::map of std::string to function pointer, initialize it with data pairing a string name to a corresponding function pointer, and then use that map at runtime to pick the correct pointer based on the string parameter passed in.
#include <iostream>
#include <string>
#include <map>
using namespace std;
void ping() {
cout << "ping" << endl;
}
void test() {
cout << "test" << endl;
}
int main() {
map<string,void(*)()> m;
m["ping"] = ping;
m["test"] = test;
// I am using hard-coded constants below.
// In your case, strings will come from command line args
m["test"]();
m["ping"]();
return 0;
}
Link to a demo with std::map.
Here is how you can do it without a map (it will be slower because of the linear search, but you can fix it by ordering names alphabetically and using binary search).
#include <iostream>
#include <cstring>
using namespace std;
void ping() {
cout << "ping" << endl;
}
void test() {
cout << "test" << endl;
}
typedef void (*fptr_t)();
int main() {
const fptr_t fptrs[] = {test, ping};
const char *names[] = {"test", "ping"};
const char *fname = "test";
for (int i = 0 ; i != 2 ; i++) {
if (!strcmp(fname, names[i])) {
fptrs[i]();
break;
}
}
return 0;
}
Link to a demo with arrays.
Declare an array of function pointers. Where you treat the index as your "code". For example:
void foo(){
printf("foo\n");
}
void bar(){
printf("bar\n");
}
int main(void)
{
void (*code_to_function[100])();
int code;
code_to_function[0] = foo;
code_to_function[1] = bar;
printf("Enter code: ");
scanf("%d", &code);
code_to_function[code]();
return 0;
}
Please note that for this rudimentary example, inputting integer code other than 0 and 1 will result in a segfault.
I should say #dasblinkenlight is right but if you don't want to use std::map you should implement a map yourself. This can be buggy and not a optimized way, but if you don't want to use STL, it seems you should implement it yourself.
You can use 2 arrays with corresponding indices. One of them is a char * array and another one is function pointers. They are better to be encapsulated in a class named something like MyMap.
class MyMap {
public:
...
inline void add(char *name, (void (*ptr)(void)) ) {
names_[currIndex_] = name; // Or stcpy
ptrs_[currIndex_] = ptr;
currIndex_++;
}
inline (void(*)(void)) get(char *name) {
int foundIndex = -1;
for (int i = 0; i < currIndex_; i++) {
// Find matching index
}
if (foundIndex_ >= 0) {
return ptrs_[foundIndex_];
}
return NULL;
}
private:
int currIndex_;
char *names_[10];
(void (*ptrs_[10])(void));
};
I have a class for performing various array operations. I like to use my insert method in my populate method.
Can someone guide me on that? Here is the code:
#include <iostream>
#include <cstdlib>
using namespace std;
const int MAX=5;
class array
{
private:
int arr[MAX];
public:
void insert(int pos, int num);
void populate(int[]);
void del(int pos);
void reverse();
void display();
void search(int num);
};
void array::populate(int a[])
{
for (int i=0;i<MAX;i++)
{
arr[i]=a[i];
}
}
void array::insert(int pos, int num)
{
for (int i=MAX-1;i>=pos;i--)
{
arr[i] = arr[i-1];
arr[i]=num;
}
}
void array::del(int pos)
{
for (int i=pos;i<MAX;i++)
{
arr[pos]=arr[pos + 1];
}
}
void array::display()
{
for (int i=0;i<MAX;i++)
cout<<arr[i];
}
void array::search(int num)
{
for (int i=0;i<MAX;i++)
{
if (arr[i]==num)
{
cout<<"\n"<<num<<" found at index "<<i;
break;
}
if (i==MAX)
{
cout<<num <<" does not exist!";
}
}
}
int main()
{
array a;
for (int j=0;j<MAX;j++)
{
a.insert(j,j);
}
a.populate(a);
a.insert(2,7);
a.display();
a.search(44);
system("pause");
}
I like to use my insert method in my
populate method. Can someone guide me
on that?
That would mean that instead of the straightforward and efficient "copy from one array to another" approach, you'd call insert for each value of the input with the correct index in place of the assignment.
To call a method on the current instance, from inside a method:
insert(x, y);
//or
this->insert(x, y);
Your code also contains an error, in that you pass a wrong type to populate in main. It expect int* (a real array), not an array object.
Please elaborate your question. If you just need a good container have a look at the STL (Standard Template Library) std::vector. It's part of the C++ standard and comes with your compiler.
If you want to learn how to write a custom class, please try to be more precise in your question.
Also consider the wealth of beginner tutorials available on the net, for example:
http://www.learncpp.com/
Here is a little example on how to write a custom class with one member function calling the other and accessing a private data member (note that inside a member function you can refer to any other member directly):
#include <iostream>
class Example
{
private:
int some_private_stuff;
public:
Example();
void function_a();
void function_b();
};
Example::Example(){
some_private_stuff = 1;
}
void Example::function_a(){
std::cout << "this is function a" << std::endl;
some_private_stuff = 2;
std::cout << "changed private_stuff to " << some_private_stuff << std::endl;
}
void Example::function_b(){
std::cout << "this is function b" << std::endl;
function_a();
}
int main() {
Example e;
e.function_b();
return 0;
}