Sign in to follow this  
OneThreeThreeSeven

Why doesn't this code for references work?

Recommended Posts

I've just been messing around trying to get the hang of references and stuff and I got this error in visual c++ 2008. error C2664: 'AddFunc' : cannot convert parameter 2 from 'void' to 'void (__cdecl *)(int &)'
#include <iostream>
#include "CopyNumber.h"


using namespace std;

struct Add
{
	int Copy;
	int Original;
};

void AddFunc(int &, void (*pf)(int &));
void PFunk(int &);

int main()

{
	CopyNumber makecpy;
	int number;
	int CopyOfNumber = 0;

	cout << "Enter a number: ";
	cin >> number;

	CopyOfNumber = makecpy.MakeCopy(number);
	cout << "Original Number: " << number << " Copy:" << CopyOfNumber;

	AddFunc(number, PFunk(CopyOfNumber));

	return 0;
}
	

void AddFunc(int & x, void (*pf)(int & y))
{
	
	x = x + 200;
	cout << "\nOriginal number is now: " << x;
}

void PFunk(int & b)
{
	b = b + 100;
	cout << "\nThe copy is now: " << b;
}



Anyone know what causes this error? I don't understand it.

Share this post


Link to post
Share on other sites
I'm not exactly sure what you're trying to accomplish, but the problem is that you're calling the AddFunc() function with PFunk()'s result which is void. AddFunc expects a function pointer which would be more like &Func.

Share this post


Link to post
Share on other sites
OK nm I fixed it. It's supposed to just add 200 to the original number and 100 to the copy of the original number. I know there's much easier ways to do that but..

I'm just trying to get a feel for how everything works as a whole i.e. structures, classes, pointers, pointers to funcs, references, etc.

The struct doesn't do anything.


#include <iostream>
#include "CopyNumber.h"


using namespace std;

struct Add
{
int Copy;
int Original;
};

void AddFunc(int &, int &, void (*pf)(int &));
void PFunk(int &);

int main()

{
CopyNumber makecpy;
int number;
int CopyOfNumber = 0;

cout << "Enter a number: ";
cin >> number;

CopyOfNumber = makecpy.MakeCopy(number);
cout << "Original Number: " << number << " Copy:" << CopyOfNumber;

AddFunc(number, CopyOfNumber, PFunk);

return 0;
}


void AddFunc(int & x, int & y, void (*pf)(int & z))
{

x = x + 200;
cout << "\n\nOriginal number is now: " << x;
int passToPF = y;
(*pf)(passToPF);
}

void PFunk(int & z)
{
z = z + 100;
cout << "\nThe copy is now: " << z;
}




Original number : 50
Copy: 50

Original number is now: 250
Copy is now: 150

Share this post


Link to post
Share on other sites
Quote:
Original post by OneThreeThreeSeven
btw the copy doesn't stay at 150 when it returns to main. dunno why. ;o


Because you never incremented CopyOfNumber.

In AddNum function you do:


int passToPF = y;
(*pf)(passToPF);



What this does is create a *copy* of the contents of y (which is a reference) inside the variable passToPF (which is not.) Then you pass the reference of *passToPF* to the function pointer. That means that what you have changed inside PFunk is the value of passToPF, not the value of CopyOfNumber.

If you really want to modify the value of CopyOfNumber, just skip the passToPF variable and pass y directly:


(*pf)(y);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this