I have some problems with C++.
I build application using GAlib library (it's a C++ Library of Genetic Algorithm Components - http://lancet.mit.edu/ga/).
In one of examples (full code of example: http://lancet.mit.edu/galib-2.4/examples/ex6.C) author create function that initializes tree:
void TreeInitializer(GAGenome & c)
{
GATreeGenome<int> &child=(GATreeGenome<int> &)c;
// destroy any pre-existing tree
child.root();
child.destroy();
// Create a new tree with depth of 'depth' and each eldest node containing
// 'n' children (the other siblings have none).
int depth=2, n=2, count=0;
child.insert(count++,GATreeBASE::ROOT);
for(int i=0; i<depth; i++){
child.eldest();
child.insert(count++);
for(int j=0; j<n; j++)
child.insert(count++,GATreeBASE::AFTER);
}
}
He invoke function in that way:
genome.initializer(TreeInitializer);
and He don't pass an argument. When I trying pass argument to that function by changing declaration for example:
void TreeInitializer(GAGenome &, int deph);
Compiler shows me errors. I don't know how to invoke this function properly. I know it's related to reference but passing (or not passing) argument in that way is new to me.
How to pass more arguments to that function?
The line genome.initializer(TreeInitializer); does not call TreeInitializer - instead, it passes the pointer to that function to genome.initializer, so it can call it with whatever arguments it needs/multiple times. If you want TreeInitializer to accept more arguments, you need to modify the initializer to accept a different type of function. It is probably defined as
void initializer(void (*f)(GAGenome&))
{
// ...
f(genome);
// ...
}
You need to change the argument type to void (*f)(GAGenome&, int) and change the lines that call f.
On the other hand, if you can't modify the initializer function but want to be able to specify the depth, then the best you can do is make a global variable that you set to whatever depth you need and have TreeInitializer use that variable. It isn't a clean solution if you think global variables are evil, but it's your only choice if the initializer doesn't let you supply any "context".
What you what to do in
GATreeGenome<int> &child=(GATreeGenome<int> &)c;
is a casting from a GAGenome to GATreeGenome . If you haven't a casting operator for this case, it won't work...
Related
I have a Mysql table that I am using as a list of different calculations that needs to be done.
Each line in the table has a column of type INT that has the number of the function that needs to be called.
e.g. line 6, data, (function) 1.
I read all the lines one by one and I need to call the relevant functions for each line.
What is the best way to construct it in C++?
should I have another function that returns the pointer of the functions that needs to be called ?
Are there other recommended solutions?
Thanks
It depends on the type of the function (input/outputs) but assuming they are all the same, you can make an array of function pointers. For example:
std::vector<void(*)(int)> MyArray;
Will declare an array of function pointers returning void and taking one int as parameter. Then you can put the functions you want in it and when you want to call them you can use MyArray[i]
If the actual type for the function pointer is long and hard to type, you can use decltype(MyFunction) instead. This requires C++11 though.
Using function pointers may work may work but I would rather make use of something like Strategy pattern.
class DataProcessor {
public:
virtual void process(Data& data) = 0;
// some other things like dtors etc
}
For each type of "function" you can create its corresponding DataProcessor.
To ease lookup, you may make use of a factory, or simply a std::map<int, DataProcessor> (instead of using int as key, will you consider using an enum?), or even a vector/array of DataProcessor.
As a suggestion, this is another way:
//Create only a function and make a switch statement in it:
void myfunction (std::pair<int,int> aRow) { // function:
int result;
int data = aRow.second;
int function_id = aRow.second;
switch(function_id){
case 1:{
//Funcion with any signature
break;
}
case 2:{
//Funcion with another signature
break;
}
//and so on...
}
//do something with the result...
}
int main () {
//Fetch your mysql data here:
std::vector<std::pair<int, int> > myMySQLdata;
for_each (myMySQLdata.begin(), myMySQLdata.end(), myfunction);
}
I need to register a member function using luabind which is supposed to take a lua-function as parameter. For any normal function I would usually just do this:
int SomeLuaFunction(lua_State *l)
{
luaL_checkfunction(l,1);
int fc = luaL_ref(l,LUA_REGISTRYINDEX);
[...]
}
Luabind however uses the parameter list, so I'm unsure how to tell it I'm expecting a function:
void Lua_ALSound_CallOnStateChanged(lua_State *l,boost::shared_ptr<ALSound> pAl,<function-parameter?>)
{
[...]
}
lua_bind(luabind::class_<ALSound COMMA boost::shared_ptr<ALSound>>("ALSound")
.def("CallOnStateChanged",&Lua_ALSound_CallOnStateChanged)
);
(Only the relevant part of the code is shown here, lua_bind is using luabind::module)
lua-example of what I'm trying to accomplish:
local al = ALSound() -- I'm aware this wouldn't work since I haven't defined a constructor
al:CallOnStateChanged(function()
[...]
end)
Perhaps there is a way to add additional functions to an already registered class without luabind? Any suggestions would be appreciated.
If you want to be able to have a function that takes Lua objects as parameters, you should have the function take a luabind::object as a parameter. Then you can check to see if it's a function and call it if it is.
I am learning C++ and very new at using classes, and I am getting very confused in trying to use them. I am trying to convert my existing code (which used structs) so that it uses classes - so while I know what I am trying to do I don't know if I'm doing it correctly.
I was told that when using functions from the class, I first need to instantiate an object of the class. So what I have tried (a snippet) in my main function is:
int main()// line 1
{
string message_fr_client = "test"; //line2
msgInfo message_processed; //line 3
message_processed.incMsgClass(message_fr_client); //line 4
if (!message_processed.priority_check(qos_levels, message_processed)) //line 5
cout << "failure: priority level out of bounds\n"; //line 6
return 0; //line 7
}
Could you help me clarify if my following assumptions are correct? The compiler is not showing any error and so I don't know if it is error-free, or if there are ones lurking beneath.
At line 4, is the function incMsgClass being performed on the string message_fr_client and returning the resultant (and modified) message_processed?
At line 5, the function priority_check is being performed on the message_processed and returning a boolean?
In my class definition, I have a function getPath that is meant to modify the value of nodePath - is it just a matter of using message_processed.getPath(/*arguments*/)?
I haven't included the body of the functions because I know they work - I would just like to find out how the class functions interact. Please let me know if I can be clearer - just trying to clear up some confusion here.
Here is my class:
#ifndef clientMsgHandling_H
#define clientMsgHandling_H
#include <list>
#include <map>
#include <queue>
class msgInfo
{
public:
msgInfo();
msgInfo(int, int, int, std::string, std::list<int>);
/*classifying message*/
msgInfo incMsgClass(std::string original_msg);
/*message error checks*/
bool priority_check(int syst_priority, msgInfo msg); //check that message is within qos levels
bool route_check(std::map<std::pair<int, int>, int> route_table, msgInfo msg); //check that route exists
void getPath(msgInfo msg, std::map<std::pair<int, int>, int> route_info, int max_hop);
private:
int source_id;
int dest_id;
int priority;
std::string payload;
std::list<int> nodePath;
};
#endif
While it may compile (and even run), there are a few oddities with the code as shown:-
First off, class methods know which object they are operating on - so your priority_check and route_check methods probably don't need msgInfo as a parameter.,
for example, your old non-class function might be like this
bool priority_check(int p, msgInfo msg)
{
return msg.priority < p;
}
But the new one should look like this:
bool msgInfo::priority_check(int p)
{
return priority < p;
}
Also, incMsgClass is a bit odd, as it's a non-static class method that returns a msgInfo object. It's difficult to tell without understanding what it's supposed to do, but it seems possible that this function should actually be a constructor, rather than a regular method.
One other thing is that you're currently passing a msgInfo by value to those methods. So if the method needed to modify the passed msgInfo, it would not have any effect. It's generally better to pass objects by reference or const reference to other methods. So, back to the previous non-method example, it should really be this.
bool priority_check(int p, const msgInfo &msg)
...
But, as I said, you probably don't need the msgInfo parameters anyway.
At line 4, is the function incMsgClass being performed on the string message_fr_client
Yes
and returning the resultant (and modified) message_processed?
Whatever it's returning, you're ignoring the return value. It can modify the object itself, yes, because the function is not const.
At line 5, the function priority_check is being performed on the message_processed and returning a boolean?
Yes
In my class definition, I have a function getPath that is meant to modify the value of nodePath - is it just a matter of using message_processed.getPath(/arguments/)?
If a member function is intended to modify one of the class members, it's just a matter of not marking that function const
Hard to tell without implementation-details, but here we go:
I. You are passing a std::string as value (C++ is call-by-value by default), so you get a copy of the std::string in your method. If you want to work on the object you passed and manipulate it, use a reference on the object, like
msgInfo incMsgClass(std::string& original_msg); // notice the ampersand
then you can change your signature to
void incMsgClass(std::string& original_msg);
as you don't need to return the std::string you passed.
II. Yes, at least according to your signature
III. Can see a node_path only as a member.
For all your questions, see C++-FAQ.
Your basic assumptions are correct.
message_processed.incMsgClass(message_fr_client); //line 4
This line is not correct. The function you call returns msgInfo which is simply dropped. You should assign it to something. But it is not as it is usually done. You should make it a constructor of msgInfo, like
class msgInfo
{
public:
msgInfo(std::string original_msg);
...
}
Then you could call it like this
msgInfo message_processed(message_fr_client);
That line would create a msgInfo that is already properly initialized.
There is another pattern for creating class instances - static creating function. In your case you could mark incMsgClass static and then call it like
msgInfo message_processed = msgInfo.incMsgClass(message_fr_client);
I seriously doubt you need this pattern here, so I'd advise to move to constructor.
As of other functions, I see no problems there. Just note that all member functions not marked as const can modify the object they are called on. So, you don't need to pass this object explicitly. For functions a pointer to the object they are called on is available by name this. Also the functions can access all class variables as if these variables are global for normal (non-member) functions.
I've got a program with a treeview class, a console class, and an options class.
I'd like to pass the treeview object to the options object, and be able to access dynamic values inside of the treeview (file lists and so on).
I've tried passing by reference, and it compiles, but through a few debug messages, I can tell it's not the same object, so the values are all empty.
Options panel Init header:
public:
void Init (HWND, PnlConsole&, PnlTree&);
...
private:
PnlTree tree_;
PnlConsole console_;
...
Options panel Init function:
void PnlOptions::Init(HWND hwnd0, PnlConsole& console0, PnlTree& tree0) {
tree_ = tree0;
console_ = console0;
...
Instantiation of classes in main file:
PnlTree pnl_tree;
PnlOptions pnl_options;
PnlConsole pnl_console;
Call to Init inside main function:
pnl_options.Init(hwnd0, pnl_console, pnl_tree);
I've been working at this for a long time (as some people have read on my previous questions) and it's very frustrating. Can someone help me to get this working?
console0 and tree0 are being passed by reference into Init() but the assigments within the function result in copies of the arguments, due to the types of tree_ and console_.
It is not possible to change the types of tree_ and console_ in this context because Init() is not the constructor and reference types must be assigned immediately (in the constructor initializer list).
A solution would be to make the types pointers and take the addresses of the arguments. Note that there is a lifetime requirement in that the objects referred to by console0 and tree0 must exist for as long as the PnlOptions requires them.
From above code, pnl_console and pnl_tree passed to init seems to be structures, so they will exist only for time, when block with Init function will exist.
In your Init function, you are passing reference via &, but assigned it to structure. I would recomend, pass it with *
void PnlOptions::Init(HWND hwnd0, PnlConsole * console0, PnlTree * tree0)
PnlTree * pnl_tree;
PnlOptions pnl_options;
PnlConsole * pnl_console;
pnl_options.Init(hwnd0, &pnl_console, &pnl_tree);
could someone please tell me what I need to do in order to create an array of objects in a function (other than in the main function).
I will try to explain by making up some sort of example...
Let's say I have a program named TimeScheduler.cpp that implements the class Schedule.h
(and I have the implementation in a separate file Schedule.cpp where we define the methods).
In the declaration file we have declared two constructors
Schedule(); //the default
and
Schedule(int, int, int);//accepts three arguments
to get to the point--let's say in the main program file TimeScheduler.cpp we created our own functions in this program apart from the functions inherited from the class Schedule. so we have our prototypes listed at the top.
/*prototypes*/
void makeSomeTime();
etc.....
we have
main(){
//etc etc...
}
we then define these program functions
void makeSomeTime(){
//process
}
let's say that inside the function makeSomeTime(), we would like to create an array of Schedule objects like this
Schedule ob[]={
summer(5,14, 49),
fall(9,25,50)
};
what do I have to do to the function makeSomeTime() in order for it to allow me to create this array of objects.
The reason I ask is currently i'm having difficulty with my own program in that it WILL allow me to create this array of objects in main()....but NOT in a function like I just gave an example of. The strange thing is it will allow me to create a dynamic array of objects in the function..... like
Schedule *ob = new Schedule[n+1];
ob[2]= Schedule(x,y,z);
Why would it let me assign to a non-dynamic array in main(), but not let me do that in the function?
This is not correct:
Schedule ob[]={
summer(5,14, 49),
fall(9,25,50)
};
You appear to be trying to introduce 3 new names:
ob, which is an array of Scedules
summer, which is a Schedule
fall, which is a Schedule
You can't introduce summer and fall as new names like that. Perhaps this was just a typo, and you meant:
Schedule ob[]={
Schedule(5,14, 49),
Schedule(9,25,50)
};
...which is perfectly fine, and can exist in a function such as:
void make_schedule()
{
Schedule ob[]={
Schedule(5,14, 49),
Schedule(9,25,50)
};
}
But now you have another problem -- your make_schedule function returns void. The Schedule array you created in make_schedule is created and then just thrown away. If you want to return an array from a functtion, the best thing to do is to use a vector, and return that:
std::vector<Schedule> make_schedule()
{
Schedule ob[]={
Schedule(5,14, 49),
Schedule(9,25,50)
};
const size_t num_obs = sizeof(ob)/sizeof(ob[0]);
std::vector<Schedule> ret;
std::copy( &ob[0], &ob[num_obs], std::back_inserter(ret));
return ret;
}
A poorer alternative is to use dynamic allocation to allocate your array, and return a pointer to the first element. In this case, when using new [] it's important to note that you can only use the default constructor.
I decided that instead of using a vector, I could use an unordered_map. I didn't realize that when you 'name' an object in c++, you aren't really giving it a name...it is simply used as a sort of temporary reference. if you want to use names you are better off using a name as a sort of key value in a set. like:
string foodname;
foodname = "cake";
[foodname, 10.95]
foodname = "bread";
[foodname, 5.75]
I found help with unordered_map on http://msdn.microsoft.com/en-us/library/bb981993.aspx