C++ :: Passing Function As Parameter?
Jan 7, 2015
gcc v.8.3 -std=gnu++11
[URL]
I'm trying to pass a function as a parameter and failing. It seems simple, until I get the error messages.
Here is the code:
class MinimalSolver {
typedef double (*func)(double sum, double char);
void driver();
[Code]....
View 2 Replies
ADVERTISEMENT
Oct 6, 2014
How come when we pass an array as a parameter to a function, the entire array is copied and is available to function?
View 1 Replies
View Related
May 2, 2013
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. By removing, I mean that you should make it appear as if the elements hadn't been there. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10 The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9 The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1 The array contains: 1
The bolded area is where I'm having trouble. How I can go about doing this, passing an array into the function as a parameter?
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
const int MAX = 10;
int a[MAX] = {0};
int i;
[Code]...
View 5 Replies
View Related
Dec 26, 2013
Pseudocode:
template<typename T /*, some parameter for member_function */>
class Foo {
public:
void someFunction() {
T t;
t.member_fuction(...);
} }
I'm trying to make the call to T::member_function a templated value because member_function might vary by name in my scenario. Since std::mem_fn isn't a 'type', i can't do something like Foo<std::string, std::mem_fn(&std::string::clear)> foo;
I also want to take into account that member_function might have more than one parameter. That is, the first parameter will always be something known but there might be other defaulted parameters.
The only thing I can think of is to make a proxy structure, something like this:
template<typename T, T> struct proxy;
template<typename T, typename R, typename... Args, R (T::*member_function)(Args...)>
struct proxy<R (T::*)(Args...), member_function> {
R operator()(T &obj, Args.. args) {
return (obj.*member_function)(std::forward<Args>(args)...);
} }
Which would then allow me to do (for example) this:
Foo<std::string, proxy<void(std::string::*)(), &std::string::clear> >
when Foo is implemented like this:
template<typename T, typename member_function>
class Foo {
public:
void someFunction() {
T t;
member_function()(t);
} };
That implementation works for me.
View 10 Replies
View Related
Sep 25, 2012
I created the following code to pass the the variable 'inputVoltage' by reference to the function 'input'. It certainly works when I run the program, but I dont think it is a standard way of doing it, i.e. the use of '*' and '&' is not according to convention ? Or perhaps the way did it is acceptable ?
int input (double *inputVoltage);
int main ( {
double inputVoltage;
input(&inputVoltage);
[Code]....
View 2 Replies
View Related
Jul 29, 2013
I have a class and I would like to be able to pass an extra parameter to the function that is executed.
BigInt operator / (BigInt N,BigInt D) {
...
}
is what I have now. but I would like to do something like this. so the default value for a is 10. and if the user does something like N/D (12) Then the value of a is 12.
BigInt operator / (BigInt N,BigInt D, int a=10) {
...
}
View 2 Replies
View Related
Jun 11, 2013
I have a class matrixType that has some overloaded operators (+, -, *, and <<). With a view to having clearly-delineated, perfectly-formatted, four-sided matrices, as shown below:
A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9
and NOT this jagged ones shown below:
A = 1 2 3
4 5 6
7 8 9
or
A + B = 1 2 3
4 5 6
7 8 9
,
I want a scheme in which the string literals (A, A+B, etc.) could be passed as parameters to the overloaded stream insertion (<<) operator function so that I could use the string’s length to determine how much offset from the display screen’s left to apply to each matrix’s row (by using the setw() function). However, I do know that the << operator is a binary operator, meaning the function cannot take more than two parameters: that is what compounds my problem!
View 10 Replies
View Related
Sep 19, 2014
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}
[code]....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 3 Replies
View Related
Sep 28, 2014
My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.
#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;
/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array */
void DisplayArray (int array[], int array_size);
[Code] ....
View 1 Replies
View Related
Aug 23, 2013
Code:
#include <stdio.h>
void ASCII_to_EBCDIC( size_t, unsigned char *);
void EBCDIC_to_ASCII( size_t, unsigned char *);
void to_ASCII(unsigned char *);
void to_EBCDIC(unsigned char *);
/* conversion tables */
static unsigned char
[Code] ....
The above snippet is for a buffer/string, where as i want to pass file name as a parameter and want function to process the file line by line?
View 2 Replies
View Related
May 1, 2013
#include <cstdlib>
#include <iostream>
struct tax_node {
char form; // tax form letter
int version; // tax form number
[Code] ....
I cannot seem to get why function print_contents will not work. The couts at the end of the program is just to test that it printed correctly. But, if I need it to print the contents such when print_contents(ptr2) is called. I think it should be tax_ptr in the parameter list but I am not quite sure.
View 1 Replies
View Related
Nov 16, 2014
I want to create events and then, functions which are subscribed to the event can access information about the event. For example, in Class 2 below, I want it to be able to access things such as touch.position, etc. of class 1.
Class 1:
public delegate void TouchEventHandler (EventArgs e);
public event TouchEventHandler TouchBegan;
public Vector2 touchPosition;
void Update () {
if (Input.touchCount > 0) {
[Code] ...
Class 2:
void OnTouchBegan (EventArgs e) {
Debug.Log ("Ran");
}
View 5 Replies
View Related
Oct 6, 2013
I've been playing around with this piece of code:
#include <iostream>
#include <string>
#include <algorithm>
template <void(*funky)(const std::string&)>
void callback()
{funky("Hello World!");}
[Code] ....
But when I try to build it, I get this error on line 24:could not convert template argument 'lambda' to 'void (*)(const string&) {aka void (*)(const std::basic_string<char>&)}'|
I thought the lambda expression I wrote would decay to a function pointer matching the template parameter. I can guess that the constexpr qualifier might have changed the type, but without it my compiler complains that lambda needs to be declared as constexpr...
So is there a way to pass lambda expressions as template parameters?
Without having to use std::function
View 4 Replies
View Related
Dec 17, 2014
Here is my issue: I am making a simple audioplayer in Xamarin.android but i want every time i change the track to make a crossfade effect. So im using 2 mediaplayers at the same time for the fade. The problem is that im defining one time the players and i pass the player as a parameter like this:
public MediaPlayer player = null;
public MediaPlayer player2 = null;
....
If i have to fadeout the player and start the next one im doing it like this:
if (player != null){
if (player.IsPlaying) {
cts = new CancellationTokenSource();
token = cts.Token;
FadeOut (player, 2000 ,token);
[Code] .....
So my problmem is that player and player2 remain always null. Why? i guess c# creates a copy of player and player2 and use this one. How i can pass a mediaplayer as parameter and always use only player and player2?
View 8 Replies
View Related
Jan 24, 2015
Does putting a ref when passing a struct to a parameter work? Say i have this code
struct struct1 {
public int i;
public void show() {
Console.WriteLine(i);
[Code] ....
The output doesn't change. The result is
0
100
500
0
View 1 Replies
View Related
May 30, 2013
I've was trying out a function template to automatically get the type of a lambda, but it seems that it won't compile
I've tried two different ways:
1.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf)
-> unordered_map<string const, HASHER>&& {
return unordered_map<string const, int, HASHER>(bucketCount, hf);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });
2.
template<class HASHER>
auto make_unordered_map(size_t bucketCount, HASHER const && hf2)
-> unordered_map<string const, int, decltype(hf2)> {
return unordered_map<string const, int, decltype(hf2)>(bucketCount, hf2);
} auto x = make_unordered_map(1, [](string const& key)->size_t { return key[0]; });
The test code are located here:
1. [URL] ....
2. [URL] ....
They are both based on the code that is stated to work in those examples. I.e.:
auto hf = [](string const& key)->size_t { return key[0]; };
unordered_map<string const, int, decltype(hf)> m (1, hf);
View 13 Replies
View Related
Jul 6, 2014
error says "cannot convert 'int*' to 'int' in function main()
and also
type mismatch in parameter in function sort(int,int)
Heres the code:
#include<iostream.h>
#include<conio.h>
void main() {
void sort(int,int);
clrscr();
[Code] .....
View 11 Replies
View Related
Oct 28, 2013
I have a class as below:
// RemoteControlMonitor.H
typedef void (*keyaction)(unsigned int key);
class RemoteControlMonitor {
private:
keyaction rph;
keyaction rrh;
[Code] .....
But I got compile error as below:
RemoteControlMonitor.H:58: invalid type `void *' for default argument to `void (*)(unsigned int)'
rcx1.C: In function `void __static_initialization_and_destruction_0(int, int)':
rcx1.C:54: ANSI C++ forbids implicit conversion from `void *' in default argument
What can I do?
View 1 Replies
View Related
Apr 17, 2014
How I can delete a parameter in a function .
int *buildTrail(int antIndex, int start, double *pheromones) {
int *trail = new int[tabu];
bool *visited = new bool[tabu];
trail[0] = start;
visited[start] = true;
[Code] ....
If I comment all lines includes visited word , no exception occurs , Otherwise , exception throws.
Simply put , How can i delete visited parameter as long as its role has been finished?
.
.
.
delete visited ;
return trail;
View 4 Replies
View Related
May 17, 2013
void foo(FILE *f1, FILE **f2) {
fputs("...", f1);
fputs("...", *f2);
} int main(void) {
FILE *fp1 = fopen(...),
*fp2 = fopen(...);
if(fp1 && fp2) {
foo(fp1, &fp2);
...
}
...
}
Forever, I've passed FILE objects into functions like the first parameter; I've never had an issue reading or writing files using that form - no file errors, no compiler warnings, etc. Recently, I saw the second parameter form, and wondered why that was?
I still don't quite get this part of pointers. What's the second parameter form doing differently than the first when the first version *appears* to work as intended??
View 6 Replies
View Related
Jan 11, 2015
Is there any function which can return parameter list of a function.
For example : get_param(f(int x,char y )) return parameter x-> int y-> char
and f_name ->f
View 3 Replies
View Related
Nov 26, 2014
#include <iostream>
#include <initializer_list>
using namespace std;
void doSomething(std::initializer_list<int> list) {
} int main() {
doSomething({2,3,6,8});
return 0;
}
I write a small piece of code like above. But I can not compile it successfully. I try it both with and without the line "using namespace std", but nothing worked.
The error list: Symbol 'initializer_list' could not be resolved
I use Eclipse CDT and GCC 4.9.1.
View 3 Replies
View Related
Sep 11, 2014
is it true or false
a function like void myfun(int num){} can receive type "int var" but can't receive type "const int var"
AND
a function like void myfun(const int num){} can receive both type "int var" and also type "const int var"
View 3 Replies
View Related
May 2, 2013
I have been working on this all day, and its due in like an hour and a half. I have done everything the program wants except the last part. Here is the assignment:
Write a program that inputs 10 integers from the console into an array, and removes the duplicate array elements and prints the array. You may assume that all the integers are between 0 and 100, Write at least 1 function in addition to the main function, and pass an array into that function as a parameter. e.g.
Please enter your 10 numbers: 1 2 3 4 5 6 7 8 9 10
The array contains: 1 2 3 4 5 6 7 8 9 10
Please enter your 10 numbers: 1 1 3 3 3 6 7 8 9 9
The array contains: 1 3 6 7 8 9
Please enter your 10 numbers: 1 1 1 1 1 1 1 1 1 1
The array contains: 1
The bolded part is what I cant get to work. I have tried this and it keeps telling me I have not identified the items when I have.
Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace std;
[Code]....
View 1 Replies
View Related
Oct 20, 2013
How can I pass a function as a parameter? I have a class that I'm trying to reuse and one of the methods in this class need to take three parameters, two ints and a function. In other words I want to be able to call a custom function every time this method is invoked when used in other classes. The function I want to call will not return any values, its a void function.
In my Class:
void Utility::someFunction(int var1, int var2, void customFunction) {
int num1 = var1;
int num2 = var2;
[Code] .....
View 9 Replies
View Related
May 30, 2013
I'm making a code that uses a Function pointer. The problem is, when I try to compile appears an error like:
error: no matching function for call to 'rnVector::rnVector()'
Here's part of the code:
phiFunction::phiFunction(double (*f)(rnVector), rnVector (*df)(rnVector)) {
//... Here comes the code stuff...
}
View 12 Replies
View Related