odd error

This is a discussion on odd error within the C++ Programming forums, part of the General Programming Boards category; I have some code which does not work and I was just wondering why and also how to fix it. ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    odd error

    I have some code which does not work and I was just wondering why and also how to fix it.

    Code:
       else if( choice == 1 )
       {
          long unsigned int num;
          
          cout << "\nEnter number: ";
          cin >> num;
       
          if ( prime( num ) == 1 )
             cout << "The number is prime.";
          else
             cout << "The number is NOT prime."; 
       }
    
    
    int prime( int &number )
    {
    ...
    }
    The error I get is:

    test.cpp:40: error: could not convert `num' to `int&'
    test.cpp:5: error: in passing argument 1 of `int prime(int&)'
    It works if the variable 'num' is just a plain int.


    Would it help speed up the program by using references llike I have? Another block of code in the program sends plenty long unsigned ints to a function for processing and I just thought that using references might reduce overhead and speed up things. If not I will happily just pass by value.

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    your prime function is expecting a pointer to a variable and you are simply only passing a variable:

    try something like:
    Code:
     else if( choice == 1 )
       {
          long unsigned int num;
          
          cout << "\nEnter number: ";
          cin >> num;
       
          if ( prime( &num ) == 1 )
             cout << "The number is prime.";
          else
             cout << "The number is NOT prime."; 
       }
    
    
    int prime( int &number )
    {
    ...
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    I was not using pointers. Your example also displays an error. The error only happens with a long unsigned int. With a normal int everything works fine.

    test.cpp: In function `int main()':
    test.cpp:39: error: could not convert `&num' to `int&'
    test.cpp:5: error: in passing argument 1 of `int prime(int&)'
    Performance wise, is it worth using pass-by-reference?

    [EDIT]

    By changing the function to
    Code:
    int prime( long unsigned int &number )
    {
    ...
    }
    The error is resolved. I still would like to know if there is a performance gain by using references?
    Last edited by cyberCLoWn; 01-17-2005 at 08:41 PM. Reason: Resolved

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    how about this:

    Code:
    int prime (unsigned long int & number) {
    
            . . . 
    }
    make sure you variable types are the same.

    edit: performance wise if you are dealing with very large numbers than, yes it is preferable to use pass by reference.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 01:17 AM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

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