C/C++ :: Syntax Of Function Passing?

Feb 5, 2014

I'm familiar with function pointers as the method to pass a function as an argument to another function. However, I recently encountered some other syntax. I have seen in multiple times and in books,so I know it is probably not a syntax error. But I am not totally sure.

#include <iostream>
void Function1() { std::cout << "Function1
"; }
void Function2(void aFunction()) { aFunction(); }
// make a param that describes the function the will be given
// as a param (return type, identifier,

[Code].....

View 4 Replies


ADVERTISEMENT

C++ :: Syntax Error In Function Call - Type Mismatch In Parameter In Function Sort

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

C/C++ :: Expression Syntax In Function Main

Sep 11, 2012

i wrote a program on implementation of stacks using functions (data structures )...but i got expression syntax error in function main.....

View 2 Replies View Related

C++ :: Call Function Syntax To Be Equivalent To Echo

Aug 19, 2014

What I want to do is simple, however I can not find a way to do it in c++ and I don't know for sure that it is possible. I want to create a function that creates a simple short-hand for printf(x); Such as:

void echo(x){
printf(x);
}

But when I call the function:

echo "hi";//I want it to look like this
//instead of:
echo("hi");// like this.

View 2 Replies View Related

C :: Prime Number Function - Declaration Syntax Error

Jan 8, 2014

I wrote this program and compiled in turboc, but it gets error"declaration syntax error" .

Code:
#include <stdio.h>
int main()
int isprime(int n)
{
if(n<2)
return 0;

[Code] ....

View 5 Replies View Related

C++ :: Sorting Data Based On Some Metric - Function Pointer Syntax

Nov 20, 2014

I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.

To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):

// Below the struct definition of Data
typedef double (Data::*CostF)(unsigned l, double W) const;

The two example functions I want to use are defined in that struct ("Data"):

// Inside the struct definition
inline double someExampleCost(unsigned l, double W) const {
// Returns some basic calculation
}

The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:

// Inside another class ("Foo")
inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) {
// Irrelevant stuff here
std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){
return (*cost)(a, W) < (*cost)(b, W);
});
// More irrelevant stuff here
}

The error shown is "operand of "*" must be a pointer". If I remove the '*':
[code]return cost(A, W) < cost(b, W);

the error becomes: "expression must have a (pointer-to-)function type."

The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:

// In main
Foo bar; // Make an object of the struct that has the "sorting" function
CostF costFunction = &Data::someExampleCost;
// Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);

This bit shows no errors by itself. So, my questions:

a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.

b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing

// In the main again
Data d; // Construct the object, which contains big lookup tables
Foo F(d); // Construct the object, which only holds a reference to a Data object
CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object

but that doesn't work ("a pointer to a bound function may only be used to call the function").

View 4 Replies View Related

C/C++ :: Two Array Member Pass As Parameter - Expression Syntax Error For Function

Nov 8, 2013

In my project, GreedyKnap::calKnap(int nItem, int nCapacity, float fWeights, float fProfits);

This function include two array member pass as parameter. how can i do this?

View 1 Replies View Related

C++ :: Passing 2 Dimensional Array Through Median Function To One Of 2 Other Function

Aug 20, 2013

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} };

[Code]...

View 1 Replies View Related

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'

View 2 Replies View Related

C :: Passing A Value From A Function

Sep 8, 2013

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMD_LINE 500
}

[code]....

The value that opt_rdrct_2 & inpt_rdrct_2 point to are not being passed to opt_rdrct and inpt_rdrct.

View 3 Replies View Related

C++ :: Passing Argument Into Function

May 21, 2013

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.

Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

[Code] .....

View 7 Replies View Related

C :: Passing Pointers Into Function?

Dec 3, 2013

I want to scan numbers in from within a function, but have access to them in main, so I tried using pointers to do so:

Code:
// Path Of Exile socket colours simulation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

[Code].....

View 9 Replies View Related

C :: Passing Values To Other Function?

Jul 6, 2013

The program asks for to ask the user for how many students there are, and how many strawberries they picked. The farm gets to keep half and the students get to keep the rest.

I am using functions and passing values to one function to another. I know I am making it harder then I need to, but I want to work with functions more and passing values. I figured it was better to practice with simple programs.

Code: #include <stdio.h>
float people(float);
float strawberries(float x);

[Code].....

View 4 Replies View Related

C :: Passing Array To Function?

Jul 31, 2014

I want to pass array to function, to fill array with new values and then to print the array in the main. But I have problem because it prints me just array of zeros. Maybe the concept is wrong, I'm new with passing arrays to function.

function:

Code:
void printSum(int *return_array) {
int return_array[3];
int i;
for(i = 0; i < 3 ; i++){
return_array[i] = 5;

Code:

void printSum(int *return_array);
int main {
int m_return_array[3];
int i,j;
for(i= 0 ; i < 3 ; i++){
m_return_array[i] = 0;
} printfSum(start,m_return_array);

[Code]...

View 1 Replies View Related

C :: Passing 2D Array To A Function

Sep 17, 2013

I need to pass a 2D array to a function. I want to know, where I may have made a mistake. This is the piece of code that I found on the web, and I am using it to allocate my 2D array.

Code:
signed char allocate2D( int** arr2D, int rows, int cols )
{
int i;
arr2D = malloc( rows*sizeof( int* ) );
if( !arr2D )
return -1;

[Code]....

My main function passes the 2D array to the function

Code: signed char read_root_message_file( int **Root_Messages, int *num ) This is how I pass the array in the main function to the above function:

Code:
int main( int argc, char *argv[] )
{
int **Root_Messages;
...
...
Root_Messages = NULL;
read_root_message_file( Root_Messages, &num_root_msg );

[Code]....

View 7 Replies View Related

C :: Passing Array To A Function?

Apr 6, 2013

Would this be an example of passing an array to a function??

Code:

void wipe (int theArray[] ) {
int index;

printf("
Inside function wipe() sets each array element back to 0.
");

[code]....

View 4 Replies View Related

C :: Passing Array To Function

Jan 19, 2014

I am following a tutorial and the topic was passing array to function so i tried to do a BMI calculator by myself. I am using code blocks to compile the codes, it is actually working while using compiler's run button. But when I open the exe file, its closing the window after entering the persons' weights and heights. Here is the code

Code:

// Passing array to function example BMI calculator of n person
#include<stdio.h>
#include<math.h>
void assess(float bmi[],int a);
int main(void){
int n,i,j;
}

[code]....

View 4 Replies View Related

C :: Passing Structures To A Function

Feb 26, 2014

I am bit confused in passing the structure to a function. I made a small code and it doesn't build !

Code:
#include<stdio.h>
struct Compute_off_Time(struct WHATTIME);
typedef union _Time {
unsigned long int Value;

[Code] ....

View 1 Replies View Related

C :: Passing Files To Function?

Jan 28, 2014

I need to pass a file as a parameter to the get_corners function, and then fill an array with an unknown quantity of data, and return the number of data to main.

Compiling this generates some messages about unused variables, which is fine for this incomplete code.

Running it just generates a blank screen. So that obviously needs to be fixed. What am I doing wrong?

I have tried moving fpin = fopen ("c: emphw12.txt","r"); to the main function, then function call to get_corners, and then closing the file after return, but same blank screen.

hw12.txt is simply:
4,0,4,7.5,7,7.5,7,3,9,0,7,0,4,0

Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *fpin; // input file

[Code].....

View 5 Replies View Related

C++ :: Passing Array To Function

Jan 8, 2014

I am learning arrays i want to whats the following effect of cods while passing array to the following function

void fun(array[])
void fun(array[4])
void fun(array[],4)
void fun(array[5],4)

View 3 Replies View Related

C++ :: Passing A List Into Function

Feb 22, 2013

Quick question... how do you pass a list in to a function...

For example forvoid reverseListwhat would the parameters be for my function... I'm trying to put everything in my case R statement in a function so I can make a neat class...

#include <iostream>
#include <list>
#include <cstdlib>
#include <conio.h>
using namespace std;

[Code] ....

View 2 Replies View Related

C++ :: Passing Object Into A Function

Feb 13, 2013

This is supposed to check for valid input

if(((c.integer < 0)&&(c.numerator < 0))||(c.denominator<=0)) {
c.integer = 0;
c.numerator = 0;
c.denominator = 0;
}
if((c.integer>0)&&(c.numerator<0))

[Code] ....
And it does just fine, until it's passed into a function, then it doesn't work:

void validInput(Mixed a) {
if(((a.integer < 0)&&(a.numerator < 0))||(a.denominator<=0)) {
a.integer = 0;
a.numerator = 0;
a.denominator = 0;

[Code] ....

It works, when the object is passed, except for two cases (one where the minus sign shifts) and whenever there is a zero or a negative integer in the denominator.

Also, I'm passing the function like validInput(c);

View 4 Replies View Related

C++ :: Passing Variables Into A Function

Mar 29, 2014

ok I have a class Player with lots of variables and im gonna call a function to checkXp, if I call it with the whole player object does it use a lot more memory then if I just passed the couple things I need?

ex
checkXP(Player* play) // this is a whole object of player
or
checkXP(play->getXP(), play->getLVL()) // the variables I want.

I just realized I may not be able to modify anything from player in the checkXP() function

question 1: does passing the whole object use more memory
question 2: if I passed as just the variables I need, I wont be able to modify anything of object play?

View 1 Replies View Related

C++ :: Passing Array In A Function?

Apr 18, 2014

i have defined an object which is an array of my class called Player now i want to pass this whole array as a parameter in a function, i think it will be done by using pointers but i am not able to figure out how it is done?

Player player[4];
void showscoreboard(" ", int cround){}
" " represents the blank where the array should b passed.

View 9 Replies View Related

C++ :: Passing A Vector To A Function

Nov 3, 2013

i'm trying to send a vector to a function however i am getting these errors

error: invalid initialization of non-const reference of type 'std::vector<sf::Texture>&' from an rvalue of type 'std::vector<sf::Texture> (*)(sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::T|

error: in passing argument 1 of 'int set_explosion_textures(std::vector<sf::Texture>&)'|

my relevant code is

//vector containing fighter explosion frames
std::vector<sf::Texture> f_explosion_textures(sf::Texture f_explosion_1, sf::Texture f_explosion_2, sf::Texture f_explosion_3,

[Code].....

View 7 Replies View Related

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 View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved