Janyl S
Janyl S

Reputation: 49

How to output the incremented value?

How can I output my void function? There is an error (no matching function for call to "increment value"), I don't know why.

I am a beginner in programming.

#include <iostream>
using namespace std;

struct Pair{
    int x;
    int y;
};

void increment_value(int *v){
    (*v)++;
    //  cout<<v<<endl;
}

int main(){
    int a = 1;
    int b = increment_value(a);
    cout<<b<<endl;
    return 0;
}

Upvotes: 0

Views: 54

Answers (3)

Nikhil Badyal
Nikhil Badyal

Reputation: 1697

The problem with your code is that you are passing(a) plain value in argument but in parameter list, you wrote pointer(*v) which gave you an error. Also you wrote void in function which means you will not return anything but on another hand, you wrote int b = function() it means the function will return something which is wrong.

It should be

int increment_value(int v) {
    v++;
    return v;
}

int main() {
    int a = 1;
    int b = increment_value(a);
    cout<<b<<endl;
    return 0;
}

Upvotes: 2

NicholasM
NicholasM

Reputation: 4673

Since you are declaring a function taking a pointer to int as its argument, you need to pass it a pointer to int. This can be done using the & addressof operator.

#include <iostream>

void increment_value(int *v){
    (*v)++;
}

int main(){
    int a = 1;
    increment_value(&a);  // Note "addressof" operator `&`
    std::cout << a << std::endl;  // Expected output:  2
    return 0;
}

Upvotes: 3

hajdarevicedin
hajdarevicedin

Reputation: 59

You are calling function which is supposed to print something while assigning values. That's why it's not printing when you are calling it. And you should return value, so change it from void to int

Upvotes: 0

Related Questions