Pointer error

This is a discussion on Pointer error within the C++ Programming forums, part of the General Programming Boards category; Hi, I am getting this error: "ERROR -: Function 'swap' must return a value' to function main" But it does ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Pointer error

    Hi, I am getting this error:

    "ERROR -: Function 'swap' must return a value' to function main"

    But it does rerturn a value, the temp value to main to swap the values.

    I have never come across this error, but I am 100% sure my logic is correct.

    Here is the code:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int swap ( int *ptr1, int *ptr2 );
    
    int main ( void )
    {
       int x = 5, y = 10;
     	 
       cout << "Value of x before swap: " << x << endl
     		  << "Value of y before swap: " << y << endl;
     		  
       swap ( &x, &y );
       
       cout << "\nValue of x after swap: " << x << endl
       	  << "Value of y after swap: " << y << endl;
    	  
       cin.get();
     	 		
       return 0;
    }
    
    int swap ( int *x, int *y )
    {
       int temp = *x;
       
       *x = *y;
       
       *y = temp;
    }

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Your function swap doesn't need to return value. You declared it as returning int, so compiler expect that in definition body see return statement. Just declare function as void swap(... if you just want to swap variables
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    thanks, it worked! If i had passed the values by reference, would i of got the same error? I was thinking of placing a return statement at the end of the swap function with no value ( void ) but your answer did the trick so thanks for that

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Since you're using C++ why not use all benefit that references provide?
    Code:
    #include <iostream>
    using namespace std;
    
    void swap_vars(int& x, int& y);
    int main()
    {
        int x = 10, y = 12;
        swap_vars(x,y);
        cout <<"x = "<< x <<" y = " << y;
        return 0;
    }
    void swap_vars (int& x, int& y)
    {
        int temp = x;
        x = y;
        y = temp;
    }
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Yes, i will try that way thanks micko, i guess ill leave using pointers to memory allocation in classes when i use new and delete. I find thats where they come most handy, managing data on the memory heap ( stack ) thanks for all your help much appreiciated

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> If i had passed the values by reference, would i of got the same error?
    Yes, the error had nothing to do with pointers, it had to do with the return type of the function. The pointers were the function parameters.

    Of course, unless you are just learning you'd want to use the swap function that already exists in C++, does the exact same thing, and might also be optimized for certain datatypes.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by swgh
    thanks, it worked! If i had passed the values by reference, would i of got the same error? I was thinking of placing a return statement at the end of the swap function with no value ( void ) but your answer did the trick so thanks for that
    "return(void);" is not allowed, but "return;" is allowed for returning void. But to return void the function must say that it returns void, not an int.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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