array of functions c++ Error initialising? - c++

I am trying to create an array of boolean functions, this is currently where I am at.
typedef bool(*fn)(DIYObject*, DIYObject*);
static fn collisionfunctionArray[] =
{
DIY::sphere2Sphere
};
bool DIY::sphere2Sphere(DIYObject* obj1, DIYObject* obj2)
{
DIYSphere *sphere1 = dynamic_cast<DIYSphere*>(obj1);
DIYSphere *sphere2 = dynamic_cast<DIYSphere*>(obj2);
if (sphere1 != NULL && sphere2 != NULL)
{
float X;
X= sphere1->m_position.x - sphere2->m_position.x;
X = X *X;
float Y;
Y = sphere1->m_position.y - sphere2->m_position.y;
Y = Y *Y;
float distance;
distance = sqrt(X + Y);
float distanceCompare;
distanceCompare = sphere1->m_radius + sphere2->m_radius;
if (distance < distanceCompare)
{
sphere1->m_velocity = vec3(0,0,0);
sphere2->m_velocity = vec3(0, 0, 0);
}
}
return false;
}
So at the moment I am only trying to insert one function into the array but I am receiving the following error
Error 2 error C2440: 'initializing' : cannot convert from 'bool (__thiscall DIY::* )(DIYObject *,DIYObject *)' to 'fn'
I think I'm taking in the same arguments so I don't really understand what the issue is here. Thanks

The problem is that sphere2Sphere is a member function of the DIY class and needs an object to put in its this pointer.
As your sphere2Sphere function doesn't use the this pointer (I think), you can make it static, which means it will then match the fn type (as the compiler will know it doesn't need the (hidden) this parameter).
Note: The static keyword goes in the method declaration in your class definition, which you haven't shown here.

Related

a value of type "void *" cannot be assigned to an entity of type "RANDOMSTRUCT *"

So I was working on malloc in void. And I have a code:
int iInitRandomPhaseArrays(WS_ELEMENT *Aufbau, RANDOMSTRUCT **random)
{
WS_ELEMENT *Act;
int iCounter = 0, i;
RANDOMSTRUCT *dummy;
Act = Aufbau;
if (*random != NULL)
return -1;
while (Act != NULL)
{
if (Act->operation == Linsenarray)
iCounter++;
Act = Act->pNext;
}
if (iCounter)
{
dummy = malloc(iCounter * sizeof(random));
ran1_3ARG(&ran1_idum, &ran1_iy, ran1_iv);
dummy[0].idum = ran1_idum;
dummy[0].iy = ran1_iy;
memcpy(dummy[0].iv, ran1_iv, sizeof(ran1_iv));
for (i = 0; i < iCounter; i++)
ran1_3ARG(&dummy[i].idum, &dummy[i].iy, dummy[i].iv);
dummy[0].Anzahl = iCounter;
*random = dummy;
}
return iCounter;
}
here error:
a value of type "void *" cannot be assigned to an entity of type "RANDOMSTRUCT *"
Can anyone help me solve it?
Change the line:
dummy = malloc(iCounter * sizeof(random));
to say:
dummy = (RANDOMSTRUCT *)malloc(iCounter * sizeof(RANDOMSTRUCT));
dummy = malloc(iCounter * sizeof(random));
this allocates the wrong amount of memory (a multiple of a pointer size, not the pointed-to) and returns a void*. In c++ void* doesn't implicitly convert to other pointer types. In c it does.
Assuming you actually mean to use C-isms in C++ code, write this:
template<class T>
T* typed_malloc( std::size_t count = 1 ) {
return static_cast<T*>(malloc( sizeof(T)*count ));
}
this function is a type-safe version of malloc that handles 9999/10000 uses, and prevents an annoying class of bugs.
Then change the line of code to:
dummy = typed_malloc<RANDOMSTRUCT>(iCounter);
Sometimes using malloc in c++ isn't easy to remove, because your code interacts with c code. This kind of change can eliminate bugs before they happen as you modify c code to c++ relatively transparently.

Issue passing template type to function and using for local variable assignment c++

I have the following code:
template<typename T> void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen) {
int nD = 0;
// if T is of type std::pair<int,int> then set no. of dimensions to 2
if (typeid(T) == typeid(std::pair<int, int>)) {
nD = 2;
}
// else if T is of type RWM::Triple<int,int,int> then set no. of dimensions to 3
else if (typeid(T) == typeid(RandomWalkMethods::Triple<int, int, int>)) {
nD = 3;
}
else {
return;
}
// Create vector of T structs to store DLA structure results
std::vector<T> aggResults;
// Initialise particle spawning type and attractor type for DLA system
RandomWalkMethods::ParticleSpawnType spawn = RandomWalkMethods::CONSTANT_RANDOM_BOUNDINGBOX_EDGE;
RandomWalkMethods::AttractorDLAType attractor = RandomWalkMethods::POINT;
// Under-estimate for fractal dimension of the DLA
const double fractalDimUnderestimateRecip = 1 / 1.65;
for (int i = 100; i <= 1000; i += 100) {
// initialise spawnDiameter using: exp(log(n)/fDUR) = n^{1/fDUR}
int spawnDiam = 2*static_cast<int>(std::pow(i, fractalDimUnderestimateRecip));
// if system is 2-dimensional, compute DLA for 2D on given lattice
if (nD == 2) {
aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
}
// else if system is 3 dimensional, compute DLA for 3D on given lattice
else if (nD == 3) {
aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk3D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
}
// compute the minimum bounding radius which encloses all particles in the DLA structure
double boundingRadius = std::sqrt(maxMagnitudeVectorOfMultiples< double, T >(aggResults));
}
}
which I may call with a statement such as
computeFractalDimensionData< std::pair<int,int> >(lattice, randNumGen);
or
computeFractalDimensionData< RandomWalkMethods::Triple<int,int,int> >(lattice, randNumGen);
where Triple is simply a struct I defined with 3 elements (essentially the same as std::pair but extended for 3 fields). Also, the functions diffusionLimitedAggregateRandomWalk2D and diffusionLimitedAggregateRandomWalk3D return types of std::vector<std::pair<int,int>> and std::vector<Triple<int,int,int>> respectively.
The issue is that when I call with either statement above I get the following errors (occurring at the assignment statements aggResults = ...):
binary '=': no operator found which takes a right-hand operand of type 'std::vector<std::pair<int,int>,std::allocator<_Ty>>' (or there is no acceptable conversion)
and similarly for the case of Triple<int,int,int>. From what I understand, this implies that I'd need an overloaded assignment operator for these 2 structs - however I do not think that is the issue here as the following statement has been used correctly before in my program:
std::vector< std::pair<int,int> > aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(nParticles, boundingBox, spawnDiam, latticeType, randNumGen, attractor, &diffLimAggFile);
So I know that I can assign the result of the DLA methods to variables of the correct type however the compiler complains if I try it through the use of passing a type to a template function as was shown above.
What is happening here and how would I go about solving this issue?
This comes from the fact that
aggResults = diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
with aggResults being a std::vector<T> is compiled even if T is Triple<int, int, int> but diffusionLimitedAggregateRandomWalk2D returns a std::vector<std::pair<int, int>>.
Suggested solution : declare a templated function and specialize it for some T.
template<typename T>
void computeFractalDimensionData(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen);
template<>
void computeFractalDimensionData<std::pair<int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
// ...
}
template<>
void computeFractalDimensionData<Triple<int, int, int>>(RandomWalkMethods::LatticeType latticeType, gsl_rng* randNumGen)
{
// ...
}
It makes more readable code and fails to compile the following line with a helping compilation error:
computeFractalDimensionData<void>(lattice, randNumGen);
YSC's solution is good. I want you just to notice that the following code in your function is a wrong use of templates:
// if system is 2-dimensional, compute DLA for 2D on given lattice
if (nD == 2) {
aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk2D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
}
// else if system is 3 dimensional, compute DLA for 3D on given lattice
else if (nD == 3) {
aggResults = RandomWalkMethods::diffusionLimitedAggregateRandomWalk3D(i, spawn, spawnDiam, latticeType, randNumGen, attractor);
}
Templates are for static polymorphism, and you are using dynamic code (these if (nd == ...)) in a template function. Proper use of a static polymorphism could be introducing a template parameter dimension.

request for member ‘begin’ in ‘ionDistance’, which is of non-class type ‘float*’

I have written the code below and I am receiving the error. What am I doing wrong?
float sampledEnergies ( float ionDistance[], float ionEnergy[])
{
float samTime[1000];
float simPos[1000];
float closeEnergy[1000];
float close;
int maxSamples = chamberLength / (driftVel * adcSampleRate);
for (int i = 0; i < maxSamples; i++)
{
samTime[i] = i * adcSampleRate;
simPos[i] = chamberLength - (driftVel * samTime[i]);
printf("%.2f\t%.2f\n",samTime[i],simPos[i]);
close = lower_bound(ionDistance.begin(),ionDistance.end(), simPos[i]);
for (int j = 0; j < maxSamples; j++)
{
if (close = ionDistance[j])
{
closeEnergy[i] = ionEnergy[j];
}
}
}
}
The above is the code and the error is as follows.
TBraggSimulation_v1.cpp: In function ‘float sampledEnergies(float*, float*)’:
TBraggSimulation_v1.cpp:37: error: request for member ‘begin’ in ‘ionDistance’, which is of non-class type ‘float*’
TBraggSimulation_v1.cpp:37: error: request for member ‘end’ in ‘ionDistance’, which is of non-class type ‘float*’
Your ionDistance is a pointer (to a first element of an array) and not a standard-library container. Your code tries to call begin and end, which are only defined for containers.
To obtain a range of iterators for a pointer, use:
lower_bound(ionDistance, ionDistance + n, simPos[i]);
Here n is the number of elements in your ionDistance array. I don't understand your code enough to suggest it's equal to maxSamples; if it's not, add a parameter to your function:
float sampledEnergies ( float ionDistance[], float ionEnergy[], size_t numIons)
{
lower_bound(ionDistance,ionDistance + numIons, simPos[i]);
}

Resolve c++ pointer-to-member error in VS 2003

I have a c++ program that I'm trying to port from VS98 to VS2003 (incremental steps). One error that occurs throughout is "Error 2275"
For instance: k:\RR\chart\chartdlg.cpp(2025): error C2475: 'CRrDoc::cFldFilter' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
The offending code is shown below:
void CDataPage::OnBtnLabelField()
{
FLDID fid ;
LPMFFIELD f ;
CRrApp *pApp = (CRrApp *)AfxGetApp();
CMainFrame *pFrame = (CMainFrame *)AfxGetMainWnd();
CRrDoc *pDoc = (CRrDoc *)pFrame->GetActiveDocument();
CSelectFieldDlg dlg;
//**************************************************
//BOOL CRrDoc::*zcFldFilter = &CRrDoc::cFldFilter;
//dlg.ck = CRrDoc->*zcFldFilter;
//**************************************************
dlg.ck = pDoc->cFldFilter ;
dlg.TitleTextID = IDS_2676;
fid = (FLDID)dlg.DoModal();
if (fid != NOID)
{
f = pDoc->m_pComposite->mfbyndx(fid);
// find index
int i, iCount;
iCount = m_lboxLabel.GetCount();
for (i = 0; i < iCount; i++)
{
if(fid == m_lboxLabel.GetItemData(i))
{
m_lboxLabel.SetCurSel(i);
OnSelchangeComboLabel();
}
}
}
}
I tried handling it according to a Microsoft page: But that just generated a set of other problems (the commented code between the asterisks). Note that I also commented out the following line:
dlg.ck = pDoc->cFldFilter
Unfortunately, this leads to a new error: k:\RR\chart\chartdlg.cpp(2022): error C2440: 'initializing' : cannot convert from 'BOOL (__cdecl )(LPMFFIELD)' to 'BOOL CRrDoc:: '
The definition in the .H file looks like:
public:
static BOOL cFldFilter(LPMFFIELD f);
Any ideas how to handle the pointer-to-member issue?
since you have:
static BOOL CRrDoc::cFldFilter(LPMFFIELD f);
its type is not a member variable but a function:
//BOOL CRrDoc::*zcFldFilter = &CRrDoc::cFldFilter; // doesn't work
BOOL (*zcFldFilter)(LPMFFIELD) = &CRrDoc::cFldFilter; // works
Since dlg.ck is of a correct type, you should do
dlg.ck = &CRrDoc::cFldFilter;

error C2663: overloads have no legal conversion for 'this' pointer

please help me for this errors
code:
u16 ip_defragment(){
u16 result;
fragip_set::iterator i;
IP_FRAGMENTED new_defrag;
IP* pcurpack = (IP*) malloc(cur.len);
memcpy(pcurpack, cur.data, cur.len);
new_defrag.saddr = cur.saddr;
new_defrag.daddr = cur.daddr;
new_defrag.protocol = cur.ip.ppack->protocol;
new_defrag.id = i2i(cur.ip.ppack->id);
i = ip_frags.find(new_defrag);
if(i != ip_frags.end()){
i->packets.insert(pcurpack);
const_cast<u16&>(i->cur_len) += cur.ip.len - cur.ip.hlen;
const_cast<u32&>(i->last_time) = time();
if(!(cur.ip.bmore_fr) && (i->tot_len == 0)){
const_cast<u16&>(i->tot_len) = cur.ip.fr_offs + cur.ip.len;
}
if(i->cur_len == i->tot_len){
for(set<IP*>::iterator k = i->packets.begin(); k != i->packets.end(); k++){
// must copy to another buffer
if(i2i((*k)->frag_off) & IP_OFFMASK){
memcpy(ip_defrag_buffer, *k, (*k)->ihl<<2);
} else {
memcpy(ip_defrag_buffer + (i2i((*k)->frag_off) & IP_OFFMASK) * 8,
*k + ((*k)->ihl<<2), (i2i((*k)->tot_len))-((*k)->ihl<<2));
}
}
IP* defr_ip = (IP*) &ip_defrag_buffer;
defr_ip->tot_len = i2i(i->tot_len);
defr_ip->frag_off = 0;
result = i->tot_len;
ip_frags.erase(i);
return result;
}
return 0;
}
if(!(cur.ip.bmore_fr)){
new_defrag.tot_len = cur.ip.fr_offs + cur.len;
} else {
new_defrag.tot_len = 0;
}
new_defrag.cur_len = cur.ip.len; // with header size
new_defrag.last_time = time();
i = ip_frags.insert(new_defrag).first;
if(i != ip_frags.end())
i->packets.insert(pcurpack);
return 0;
}
compiled project and view only 2 errors similar
line 15 : i->packets.insert(pcurpack);
end line : i->packets.insert(pcurpack);
error with 2 lines : error C2663: 'std::_Tree<_Traits>::insert' : 4 overloads have no legal conversion for 'this' pointer
IntelliSense: no instance of overloaded function "std::set<_Kty, _Pr, _Alloc>::insert [with _Kty=IP *, _Pr=std::less<IP *>, _Alloc=std::allocator<IP *>]" matches the argument list and object (the object has type qualifiers that prevent a match)
please help me?
I had exact same error with std::set, while passing it to a lambda expression:
C2663 'std::_Tree<std::_Tset_traits<_Kty,_Pr,_Alloc,false>>::insert': 5 overloads have no legal conversion for 'this' pointer
lambda expression prototype was:
[se1, ele1](auto val) {
/* here I was editing se1 set, but above se1 is passed value type */
}
I have changed to:
[&se1, ele1](auto val) {
/* now since se1 set is sent as reference type above, it is good to edit
changes stays as I expect it to be
*/
}
Now compilation succeeds.
I used with count_if function, which calls the lamda expression for eac element, so the compiler knows that modifications should persist in set se1, which is perfectly logical.
If you desire to have the original set unchanged, then send a copy of it.