CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2011
    Posts
    91

    Identifier not found

    Hello.

    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.

    Hopefully someone can help me.

    Thanks.

    Seán

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Identifier not found

    Is ran2 a class member? Are you calling it from inside that class? Not really enough information given.

  3. #3
    Join Date
    Oct 2011
    Posts
    91

    Re: Identifier not found

    Hello,

    Thanks for the reply.

    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.

    Seán
    Attached Files Attached Files

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Identifier not found

    Your
    Code:
    double ran2(int &idum);
    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?
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2011
    Posts
    91

    Re: Identifier not found

    Hey,

    Thanks for that! I will give it a go.

    Quote Originally Posted by VictorN View Post
    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?
    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.

    Again, thanks for the information!

    Seán

  6. #6
    Join Date
    Oct 2011
    Posts
    91

    Re: Identifier not found

    Hello again,

    I have been trying some of the suggested fixes, and now I am getting this error

    "error C2664: 'CAboutDlg::ran2' : cannot convert parameter 1 from 'int *' to 'int &'"

    And I really don't know how to fix this!

    Seán

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Identifier not found

    Quote Originally Posted by o.fithcheallaigh View Post
    And I really don't know how to fix this!
    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?

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Oct 2011
    Posts
    91

    Re: Identifier not found

    Quote Originally Posted by Paul McKenzie View Post
    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.

    Seán

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Identifier not found

    To the OP:

    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?

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Oct 2011
    Posts
    91

    Re: Identifier not found

    Hello.

    The & symbol will give the address of whatever it is before. So, in this case the address of idum.

    So, it is assigning to x, the address of idum, and not the actual value.

    Seán

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Identifier not found

    Quote Originally Posted by o.fithcheallaigh View Post
    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.
    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).

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Oct 2011
    Posts
    91

    Re: Identifier not found

    Okay, thanks for that.

    Over the summer, I am just going to go right back to the start and learn it the right way!

    Many thanks.

    Seán

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured