I have function under a 'public:' section of my code. It is called
Code:
double ran2(int &idum)
Later on, for a button click in the dialog box, I have a line of code that says
Code:
x = ran2(&idum);
But this line creates an error stating "error C3861: 'ran2': identifier not found". But since the original function is under a public section, I don't understand where I am going wrong here.
Sorry about that ...I have attached pretty much the full code. I thought since it was coming under the public deceleration I could call it without any issues.
is declared within the CAboutDlg class while you are calling it from within the CSeбnУFithcheallaigh_CPP_Assign11Dlg class!
So either move its definition into CSeбnУFithcheallaigh_CPP_Assign11Dlg class or call it from within CAboutDlg class or create temo CAboutDlg object and call it for this object like:
Code:
void CSeбnУFithcheallaigh_CPP_Assign11Dlg::OnBnClickedButton1()
{
...
int idum = 111;
...
CAboutDlg dlg;
x = dlg.ran2(&idum);
...
}
PS: I wonder how could you compile .txt file?!
PPS: seriously, don't you know yet how to post code snippets using Code tags? After 86 posts and being here since the last 6 months?
PPS: seriously, don't you know yet how to post code snippets using Code tags? After 86 posts and being here since the last 6 months?
I didn't compile a .txt file, I just copied the code into it. Also, if you look at my original post, you will see I do know how to use the code tags. But I just thought since I was posting a large bit of code, it might be easier to look at from a .txt file.
The error is obvious. You are passing a pointer when you shouldn't be passing a pointer.
How are you learning C++? Do you have a C++ book where you have examples of correct usage of the language? Or are you just trying anything until something works?
Or are you just trying anything until something works?
Pretty much.
I am learning for a class at uni, where the idea seems to be learning through doing. Normally we have lab classes where we can get help, but they ended long before the current work was even given to us.
I have some books that I try and use for reference, but they haven't been too much help at the minute.
The issue I see is that you're trying to use a complex language where you haven't learned the language's basic fundamentals. The problem with this is that the simplest of things, chapter 1 or 2 issues in any elementary C++ book or tutorial, will stump you, as these errors are stumping you now.
Code:
x = dlg.ran2(&idum);
If I asked you tell me what that line of code does, can you explain it? The reason for your last error is probably that line of code. What is the purpose of the "&" in that line of code?
I am learning for a class at uni, where the idea seems to be learning through doing. Normally we have lab classes where we can get help, but they ended long before the current work was even given to us.
Well, you can't use C++ that way, where you're just trying to get something to compile successfully. What happens when you run the program, and it doesn't work correctly? How are you going to fix it?
Or worse, what if the program happens to run, but then you made a mistake where the program just happens to run OK "by luck", i.e. your code invokes undefined behaviour? You take the program to another machine, and it crashes right away? How will you fix that?
I have some books that I try and use for reference, but they haven't been too much help at the minute.
The errors you're encountering are very simple ones. Passing values to functions should be covered in any C++ book.
If a function takes a reference, you don't pass a pointer, which is what that line of code I showed you is doing. That is what the "&" does in that context. The "&" is the address-of operator when you do this:
Code:
x = ran2(&idum);
The address-of operator is not the same thing as a reference.
Code:
double ran2(int &idum)
This code says that idum is a reference parameter. The "&" in this context means reference, not address-of. So the same character "&" in C++ is used in two completely different contexts, and this is what is confusing you (but shouldn't if you actually are reading C++ books).
If the function takes a reference to T, you pass a T, not the address of T.
Code:
x = ran2(idum);
That should be the proper way to pass the parameter. If the function were written this way:
Code:
double ran2(int *idum)
Then your original code would have worked, but then you would have to change the code in ran2 to use pointer syntax (changing the "." to "->" in most cases).
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.