Why doesn't this test function work?

I think the problem resides in 'E' but I'm not sure why,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

void test (int, int, double&, double&, char&);
int main ()
{
	double x, y;
	int m;
	char next;
	test ('a', 10, 35, y, 'E');
	
}

void test (int a, int b, double& c, double& d, char& e)
{

}
To call void test( int, int, double&, double&, char& ), the last three arguments must be non-const lvalues.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

void test (int, int, double&, double&, char&);

int main ()
{
    double x = 289.4, y = 534.9 ;
    char next = 'E' ;

    std::cout << "before: x == " << x << ", y == " << y << ", next == '" << next << "'\n" ;
    test( 'a', 10, x, y, next );
    std::cout << " after: x == " << x << ", y == " << y << ", next == '" << next << "'\n" ;
}

void test( int a, int b, double& c, double& d, char& e )
{
    // objects referred to by c, d and e may be modified by the function
    c += a ;
    d -= b ;
    e = '!' ;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

void test (int, int, double, double&, char);
int main ()
{
    double y = 0;
    
    test ('a', 10, 35, y, 'E');
}

void test (int a, int b, double c, double& d, char e)
{
    d++;
    
    cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << endl;
}


This is another way of looking at it to get the OP to work. It all hinges on what the function is meant to do given that m, x and next aren't being used and keeping in mind passing by reference is only useful if a variable is declared and then only if the function is being used to change its value.

Maybe also the first parameter should be a char, even though int works, to make the code just a little more robust.

Topic archived. No new replies allowed.