I am trying to access atomic private variables defined in class A using vector of objects . This vector of objects is passed by value in other class B defined in another header file. While retrieving the the value from vector I am getting the value as 0
I have few std::atomic private variables in a class say class A
A.hpp file
class A
{
public:
vector<A> updatePktCount();
private:
std::atomic pktCount;
std::atomic recvPkt;
std::atomic droppedPkt;
};
A.cpp
// there will be different pkts for different ports
vector a[2]; // 2 since I have 2 port ID's
vector<A> A::updatePktCount(int portId)
{ a.push_back(A());
//some function that gets packet details based on portId
pktCount = getPacketCount(int portID, int recvPkt,int pktCount,int droppedPkt);
pktCount = pktCount++;
recvPkt = recvPkt++;
droppedPkt = droppedPkt++;
a.push_back(A());
return a;
}
Class B in different header file B.hpp
class B: public A {
void getPacketCountFromA();
};
B.cpp file
void B::getPacketCountFromA()
{
vector<A> storeValue;
A a;
storeValue = a.updatePktCount();
for(auto i = storeValue.begin(); i != storeValue.end(); ++i)
{
// print values
}
}
I have created a vector of objects in A like vector<A> a[2] for 2 portId but when I retrieve this vector in getPacketCountFromA I am getting the vector size as 2 but all the values in the vector is 0
Am I doing anything wrong here ?
Result
size of vector is 2
pktCount = 0
recvPkt = 0
droppedPkt = 0
recvPkt = recvPkt++; is retrieving the value of recvPkt, increment recvPkt, then store the retrieved value (0) back into recvPkt, losing the effect of the increment. Just increment the value:
++recvPkt;
The same for your other variables.
Related
I have a function in a common module(class) which takes a struct reference of vector type:
The structure has an element which is of Vector type.
Please refer following code snippet:
bool getFrameInfo(ABC& abc);
struct ABC {
int numberOfFrames;
std::vector<XYZ> framInfo;
....
}
struct XYZ {
int size;
Position Pos;
...
}
I need to access and store members of struct XYZ in member variables or local variables in my class which calls the function defined in the common module to get frame information.
Please suggest ways to access and store "XYZ" struct members so that I can use them repeatedly in my program to draw a frame.
Example:
bool getFrameInfo(ABC& abc) {
abc.framInfo[0].size = 10;
// more code, where you return something somewhere
}
This will access your vector from abc, then index its first element (I assume it exists), and access the size field of the first struct of your vector. It stores 10 in its size.
In header there is deffinition of structure:
struct SNeuron
{
//the number of inputs into the neuron
int m_NumInputs;
//the weights for each input
vector<double> m_vecWeight;
//ctor
SNeuron(int NumInputs);
};
In cpp file there is:
SNeuron::SNeuron(int NumInputs): m_NumInputs(NumInputs+1)
{
//.. some body
}
What actualy the header of constructor of structure does? There is construction
SNeuron::SNeuron(int NumInputs): m_NumInputs(NumInputs+1)
Is it the behaviour of this strane construction same like if I write it for example in Java like this:
class Neuron {
int m_NumInputs;
public Neuron(int numInputs) {
this.m_NumInputs = numInputs+1;
//... rest of constructor body
}
}
So the expected behaviour is that I take the numInputs (input of structure) and
: m_NumInputs(NumInputs+1)
means I want to set the local variable of structure which will be the input value increased by one?
I'm working on converting a lua program into a C++ program but I've hit a road block, I can't figure out how to convert this into C++
function newPool()
local pool = {}
pool.species = {} --imports data from local species = {}
pool.generation = 0
pool.innovation = Outputs
pool.currentSpecies = 1
pool.currentGenome = 1
pool.currentFrame = 0
pool.maxFitness = 0
return pool
end
I know many basics of both languages and i know it works in lua but i need it in C++. Can anyone help me?
Lua has something called Tables which allows you to add key-value pairs without a predefined struct as in C/C++. So the Lua code you posted is adding key-value pairs to pool (read comments in code):
local pool = {} -- Declare a new Table
pool.species = {} -- Add another Table to pool called 'species'
pool.generation = 0 -- Add the key 'generation' with value '0'
pool.innovation = Outputs -- Add the key 'innovation' with value 'Outputs'
pool.currentSpecies = 1 -- Add the key 'currentSpecies' with value '1'
pool.currentGenome = 1 -- Add the key 'currentGenome' with value '1'
pool.currentFrame = 0 -- Add the key 'currentFrame' with value '0'
pool.maxFitness = 0 -- Add the key 'maxFitness' with value '0'
In C++ you have several options. 1) you can create a struct and declare what you need (I'm guessing on some datatypes but if you have the full Lua program you can figure them out):
struct Pool
{
Species species; // You'll have to define Species in another struct
int generation;
SomeEnum innovation; // You'll have to define SomeEnum in an enum
int currentSpecies;
int currentGenome;
int currentFrame;
int maxFitness;
}
If you have a class then you can use the struct Pool shown below (add the struct Pool definition from above to the .h file below above class Kingdom):
// I'm doing this as a class since you are programming in C++ and I
// assume you will want to add more functions to operate on similar
// objects.
class Kingdom
{
public:
Kingdom();
Pool* NewPool();
private:
Pool _pool;
}
In your .cpp file:
#include "Kingdom.h"
Kingdom::Kingdom()
{
// _pool.species = whatever you define struct Species as
_pool.generation = 0;
_pool.innovation = SomeEnum::Outputs; // You'll have to define SomeEnum
_pool.currentSpecies = 1;
_pool.currentGenome = 1;
_pool.currentFrame = 0;
_pool.maxFitness = 0;
}
Pool* Kingdom::NewPool()
{
Pool* newPool = new Pool;
memcpy(newPool, &_pool, sizeof(Pool)); // Make a copy
return newPool; // Return the new copy
// The newPool value is dynamic memory so when the calling function is done
// with newPool it should delete it, example:
// Kingdom myKingdom;
// Pool* myNewPoolStruct = myKingdom.NewPool();
// ... do some coding here
// delete myNewPoolStruct;
}
Option 2) would be if all of your key-value pairs were the same type; i.e. all keys were std::string and all values were int. Remember, the Lua code is using Tables so you can create the equivalent in C++ with std::map<>. Then you could use std::map<std::string, int> as follows:
// In your .h file change
Pool* NewPool();
Pool _pool;
// to
std::map<std::string, int> NewPool();
std::map<std::string, int> _pool;
Then in your .cpp file change the constructor to:
Kingdom::Kingdom()
{
_pool["species"] = 0; // Some int representation of species
_pool["generation"] = 0;
_pool["innovation"] = 1; // Some int representation of Outputs
_pool["currentSpecies"] = 1;
_pool["currentGenome"] = 1;
_pool["currentFrame"] = 0;
_pool["maxFitness"] = 0;
}
std::map<std::string, int> NewPool()
{
std::map<std::string, int> newPool;
newPool = _pool; // Copy - double check this against std::map
return newPool; // Double check this is a true copy and not a pointer
}
With std::map you can create key-value pairs on the fly just like the Lua code you provided. In short, I'd go with the struct Pool approach because with std::map<> you'll have to remember strings which isn't good practice and your IDE should have intellisense which will always show you the contents of struct Pool whenever you hit the . or -> operators.
I have made this custom class
#ifndef VEHICLE_H_
#define VEHICLE_H_
Class Vehicle {
public:
Vehicle();
Vehicle(char,char,int,int);
virtual ~Vehicle();
char getLicense_plate();
void setLicense_plate(char);
char getBrand();
void setBrand(char);
int getTime_in();
void setTime_in(int);
int getTime_out();
void setTime_out(int);
char license_plate;
char brand;
int timei;
int timeo;
};
And I have created a Vehicle vector with size 50 in the main.cpp but I don't how to check each value if they are empty.
#inlcude<iostream>
#include<vector>
#include<algorithm>
#inlude "Vehicle.h"
using namespace std;
int main()
{
vector<Vehicle> avai_space(50);
for(int i=0;i<avai_space.size();i++)
{
//if(avai_space(i) == NULL??){}
vector<Vehicle> avai_space(50); does not create an empty vector with a storage capacity of 50; it creates a vector with 50 valid entries where each entry gets initialized using the default constructor of Vehicle class.
To create an empty vector with max storage of 50, use:
vector<Vehicle> vehicle_vec;
vehicle_vec.reserve(50);
You can use vehicle_vec.push_back() to add items into the vector without affecting the storage.
Once the vector is populated with entries, you can now use vehicle_vec.size() to iterate through the valid entries in this vector:
for (size_t i = 0 ; i < vehicle_vec.size() ; i++) {
// Use vehicle_vec[i], for e.g.
std::cout << vehicle_vec[i].brand << "\n";
}
vector<Vehicle> avai_space(50); creates 50 Vehicles. They exist after this line and are not empty, but what ever Vehicle's default constructor makes them.
If you want to store Vehicles in your vector, but not create them yet (I assume that's what you mean with == NULL), just write vector<Vehicle> avai_space; and then add new Vehicless to the end with std::vector::push_back.
None of the elements will ever be 'empty', as in a null pointer, because they're stored by value, not by pointer.
That scenario would correspond to a declaration like
std::vector<Vehicle*>
or, if the pointers need to own the objects
std::vector<std::unique_ptr<Vehicle>>
I am confused with C++ vector and ask for help.
I declare a class CBoundaryPoint:
class CBoundaryPoint:
{
public:
double m_param;
int m_index;
}
And then I define a vector:
vector<CBoundaryPoint> vBoundPoints;
CBoundaryPoint bp;
double param;
// other codes
bp.m_param = param;
vBoundPoints.push_back( bp );
It surprises me that for every element in vBoundPoints, the value of m_param is totally different from the given value param. I just don't know why.
For Example:
param = 0.3356;
bp.m_param = param; // so bp.param equals to 0.3356;
vBoundPoints.push_back( bp ); // while (*(vBoundPoints.end()-1)).m_param = -6.22774385622041925e+066; same case to other elements
So what happened and why? I'm using VS2010.
You probably get garbage when you resize the vector or create a vector of a certain size using the size_type constructor. You get default-constructed objects in your vector, and these contain primmitive types. Since you have no user defined default constructor, the values are essentially random, or "garbage".
You can fix this by adding a default constructor to your class:
class CBoundaryPoint:
{
public:
CBoundaryPoint : m_param(), m_index() {} // initializes members to 0. and 0
double m_param;
int m_index;
}