C++ :: Passing Function As Argument To Other Function?
Jul 3, 2013
I am trying to pass function as argument to another function. My idea is to write function that can works with any type of array, and for it to be able to compare array items I'd like to use my own compareTo function. But I need to be able to pass function to use for comparing argument.
To say it short I am trying to write my own qsort that would take compareTo as one argument just like original qsort does.
Here is my code
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
template <class T>
int compareTo( T a,T b){
[code]....
and errors
1>d:my documentsvisual studio 2012projects est est est.cpp(29): error C2896: 'void DoSomething(T,int (__cdecl *)(T,T))' : cannot use function template 'int cmp(T,T)' as a function argument
1> d:my documentsvisual studio 2012projects est est est.cpp(8) : see declaration of 'cmp'
1>d:my documentsvisual studio 2012projects est est est.cpp(29): error C2784: 'void DoSomething(T,int (__cdecl *)(T,T))' : could not deduce template argument for 'T' from 'int [3]'
1> d:my documentsvisual studio 2012projects est est est.cpp(21) : see declaration of 'DoSomething'
How to pass an int that I got from user input into a function to use it. I am trying to print out the words to a string of numbers.
I got the input from user. I got an absolute value of the input. I then separate the string into individual digits and name them. I can print these out. Then I started my if statement by checking if the original input was zero, and if it is, printing zero and exiting. Then I an trying to pass the digits into a switch function and this is where I go off the rails.
how can i pass an array as an argument to the function? in getCoin() fcn, I am supposed to pass coins array as an argument to the function. fcn prompts user to enter coin(Date, Type and Country). values entered by user are read and assigned to the coins array. I tried the code below.
//# include "Coins.h"; #include <iostream> #include <string> using namespace std;
I have in the past written code for templated functions where one function argument can be either a function pointer or a Functor. Works pretty straightforward.
Now I am in a situation where I am actually trying to pass a function pointer as template argument to a class. Unfortunately this does not work, I can pass the Functor class but not the function pointer. Below code illustrates the issue:
The idea is to have the definition of the Record class simple and readable and have a maintainable way to add auto-conversion functions to the class. So the lines I commented out are the desirable way how I want my code to look. Unfortunately I could not come up with any way that was close to readable for solving this.
error C3867: 'WordParsor::Form1::PutUpfrmIO': function call missing argument list; use '&WordParsor::Form1::PutUpfrmIO' to create a pointer to memberc:userskingc++wordparsorwordparsorForm1.h... and the suggestion fix generate another error.
One person suggested the gcroot<> object wrapper... but I do not know how to modify/declair the function or its argument type.
I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .
By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..
Code: int main (int argc, char *argv[]){ //get_parameter_value(argc,argv); // buffer[packet_size+1]= char ("'"); while (argc > 1) { if (argv[h][0] == '-')
I want to use one median function "selectfunction" to choose one of the 2 other functions at random to pass my 2-dim array to the selected function. There is a problem in the median function
#include <iostream> #define random(x)(rand()%x) // for random number between numbers of 0 and 1 using namespace std; void proc1 (int iArray[][2]); void proc2 (int iArray[][2]); void selectfunction(int iArray[][2]); int A[4][2] = {{1, 2} , {3, 4} , { 5, 7} , {8, 1} };
I was reading about void as function argument, but I did not fully understand it's meaning in C.
In C++ void foo(void) {} and void foo() {}
are the same. It means no arguments for foo function. But in C it's different. First function means the same as in C++, but second means
In C, an empty parameter list means that the number and type of the function arguments are unknown. But if it is unknown you can't use this arguments if user specifies same. Because here are no variables to store them. So doesn't result are the some? You do not get any arguments. O do I can get this arguments from some hidden variable?
For example.
void foo() { printf("%d", var); } foo(5);
It is very unclear for me. Do this apply to main function too?
int main(void) int main()
or can I use arguments given to int main() like given to int main(int argc, char* argv[])
I get an error when i try to compile this code. I tried to allocate memory in main function and that works. But why it doesn't work in function? I think that there is something wrong with function argument, but not sure.
Code:
#include <iostream> #include <fstream> using namespace std; struct Word
I was wondering if one could write a function that could accept one or the other variable type.
Ex: I have 2 arrays, int** and double**, and a function
Code: void PGMWrite(double** Matrix, int Matrix_dimension){.....}
Is there any way to change the function to
Code: void PGMWrite(int** Matrix || double** Matrix, int Matrix_dimension){.....}
And then have some sort of type identifier in the function that picks the correct section via an if loop? If so how, and how would I identify in the function if the input it type double or int?
I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:
Returns: BASE_OK RS232 data acquisition success BASE_BASE_232_GETDATA_ERR RS232 data acquisition failure
I have trouble writing the relevant code in the main program that invokes this function. Here is a snippet of what I have tried:
# include "Baseboard.h" int main () { Baseboard _Baseboard; // Class name is Baseboard char *msg ;
[Code] ......
The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?
Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value) .
void foo(const double va, const int q) { int qaa[q]; ...... return; }
However, the compiler indicates allocator cannot allocate an array of constant size 0... how can I use the argument "q" to fix the size of array "qaa"?
Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?
Code: #include <stdio.h> int compareid(void* info, int value); // ansi declaration int compareid(void* info, int value)
I was trying to look up solution for this for quite a while already but found nothing. I am writing a simple console based turn based RPG game for my class project. I was trying to have a member function attack() in class of the player character, which affects the component called health of the class Enemy. both this classes are inherited from the base class Unit. I tried to pass the object of type enemy as an argument to the function attack, but the function call gives me Error: too many arguments in function call. Here's the code for classes:
In the above program, I define an operator<< function in global namespace which has exactly the same signature as the one define in <string> header : [URL] . In the main function, I call operator<< on cout and a string, after which I guess it would cause ambiguity. The result is out of my anticipation, it compiles and prints [hi]. So I guess my function is a better match because it does not require argument-dependent lookup (ADL). Moreover, even if I add using namespace std; at the beginning of the main function, the program still compiles and prints [hi].
In short, I have two question:
#1 : Why is my function a better match (therefore no ambiguity) in the two cases respectively (with and without using namespace std;)?
#2 : Does using namespace affect ADL? Is ADL still performed after applying using namespace, or is it not performed because the function is thrown into global namespace?
We are programming a function calculator and my instructor hinted that the add() function is almost exactly like the subtract() function and we would only have to write one function if we "called it in a special way." This got me thinking, can I turn a function's argument to negative during the call to make it subtract?
Example:
void addSub(int &num1, int &num2) into this void addSub(int &num1, int -(&num2))