473,466 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trying to templatize an algorithm.

Hello kind people.

I have the classes "point", "curve", "surface", "region" as shown
below. Every class has a member function that returns a list
of pointers to objects that comprise the *this object.

In main function I have implemented an algorithm
(some lines of code noted as "My Algorithm") which in
the current state works for "surface" and "curve" objects.

I am trying to templetize these lines of code to make them work for
"curve-point", "region-surface" e.t.c, but I have compilation problems.

I'm trying to pass as argument a pointer to the member function (get_***),
but obviously I'm doing it wrong. Can anyone explain me how to do this?

If the pairs are <region, surface> the passed pointer to member function should
be get_surfaces,
If the pairs are <surface, curve> the passed pointer to member function should
be get_curves, e.t.c.

Right now I have implemented all the algorithms I need, but the code is
very long and it is repeated for every pair of classes, and so I need it
to templatize them. I provide the minimum snippet which demonstrates
my error.

I hope that I am asking the question with clarity...

Many thanks for your time.

--------------------------------------------------------------------------
// main.hpp program begins here. This is line 1
class point {
};

class curve {
public:
std::list<point *> get_points() {return belongPoints;}
private:
std::list<point *> belongPoints;
};

class surface {
public:
std::list<curve *> get_curves() {return belongCurves;}
private:
std::list<curve *> belongCurves;
};

class region {
public:
std::list<surface *> get_surfaces() {return belongSurfaces;}
private:
std::list<surface *> belongSurfaces;
};

class prog {
public:
std::list<point *> m_Points;
std::list<point *> m_selPoints;
std::list<curve *> m_Curves;
std::list<curve *> m_selCurves;
std::list<surface *> m_Surfaces;
std::list<surface *> m_selSurfaces;
std::list<region *> m_Regions;
std::list<region *> m_selRegions;
};

template<class T1, class T2>
void templatize(std::list<T1 *> &obj1, std::list<T2 *>(*func)()) {
//
typename std::list<T1 *> :: iterator isr = obj1.begin();
while (isr!=obj1.end()) {
// std::list<T2 *> belongList = (*isr)->func();
isr++;
}
//
}
// main.hpp program ends. This is line 48
--------------------------------------------------------------------------
// main.cpp program begins. This is line 1
#include <list>
#include <algorithm>
//
#include "geom.hpp"

int main () {

prog *m_pDoc = new prog;

templatize<surface, curve>(m_pDoc->m_selSurfaces, &surface::get_curves);
// templatize<region, surface>(m_pDoc->m_selRegions, &region::get_surfaces);
// templatize<curve, point>(m_pDoc->m_selCurves, &curve::get_points);

//
// Section "My Algorithm" Begins
std::list<surface *> :: iterator isr = m_pDoc->m_selSurfaces.begin();
while (isr!=m_pDoc->m_selSurfaces.end()) {
std::list<curve *> curveList = (*isr)->get_curves();
std::list<curve *> :: iterator icr = curveList.begin();
while (icr!=curveList.end()) {
if (std::find(m_pDoc->m_selCurves.begin(),
m_pDoc->m_selCurves.end(), *icr)!=
m_pDoc->m_selCurves.end()) {
m_pDoc->m_selCurves.remove(*icr);
}
icr++;
}
isr++;
}
// Section "My Algorithm" Ends
return 0;
}
// Main program ends. This is line 36
--------------------------------------------------------------------------

--
tuko, the ugly.
Jul 22 '05 #1
1 2384
"tuko" <tu**@away.com> wrote in message news:ci**********@nic.grnet.gr...
Hello kind people.

I have the classes "point", "curve", "surface", "region" as shown
below. Every class has a member function that returns a list
of pointers to objects that comprise the *this object.

In main function I have implemented an algorithm
(some lines of code noted as "My Algorithm") which in
the current state works for "surface" and "curve" objects.

I am trying to templetize these lines of code to make them work for
"curve-point", "region-surface" e.t.c, but I have compilation problems.

I'm trying to pass as argument a pointer to the member function (get_***),
but obviously I'm doing it wrong. Can anyone explain me how to do this? <snip> template<class T1, class T2>
void templatize(std::list<T1 *> &obj1, std::list<T2 *>(*func)()) {
This is not the syntax you use for member function pointers; it's the syntax
one uses for non-member function pointers. You need to change

std::list<T2 *>(*func)()

to

std::list<T2 *>(T1::*func)()
//
typename std::list<T1 *> :: iterator isr = obj1.begin();
while (isr!=obj1.end()) {
// std::list<T2 *> belongList = (*isr)->func();
To invoke the member function via the pointer:

((*isr)->*func)()

isr++;
}
//
}

<snip>

HTH

--
David Hilsee
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
0
by: niTT | last post by:
Hi, I'm new to python, as in I read the first 5 chapters of 'dive into python' new. I'm trying to use the levenshtein algorithm to do some edit distance comparisions. However rather than...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
0
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm...
3
by: TD | last post by:
I found a freeware dll, md5lib.dll, on the web and am trying to use it in Acess 2003. I entered this in a module in the database: Public Declare Function StringMD5 Lib...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.