Like Tree1Likes
  • 1 Post By baxy

Function overload

This is a discussion on Function overload within the C++ Programming forums, part of the General Programming Boards category; Hi, Can anyone explain to me why is this not working. This is suppose to be an example of function ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    176

    Function overload

    Hi,

    Can anyone explain to me why is this not working. This is suppose to be an example of function overload and as far as i got it , as long as i have something (number of variables, type ...) that can distinguish my two finctions i should be ok. but ...

    Code:
    using namespace std;
    #include <iostream>
    
    
    void print(char &x){
      cout << x <<endl;
    }
    void print(int x){
      cout << x <<endl;
    }
    
    int main(){
      int x = 7;
      char k[]="look at me :)";
      print(k);
      print(x);
      
      return 0;
    }
    i get the following error:

    13.cpp: In function ‘int main()’:
    13.cpp:15:10: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
    13.cpp:8:6: error: initializing argument 1 of ‘void print(int)’ [-fpermissive]

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Well, your first function takes an argument of type "char&", which isn't the same as "char*"
    Devoted my life to programming...

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The functions are overloaded.
    But you want to handle a string and you do it like you are in C.

    You are on C++ . Learn how to handle The string class of C++ now . Sooner or later you will need it

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    176
    aha, so the reference to a variable is not the same as the pointer to the variable.... thank you

    baxy


    PS
    aha...

    Code:
    using namespace std;
    #include <iostream>
    #include <string>
    
    void print(string &x){
      cout << x <<endl;
    }
    void print(char *x){
      cout << x <<endl;
    }
    void print(int x){
      cout << x <<endl;
    }
    
    int main(){
      int x = 7;
      string s = "HHHHHHHHH";;
      char k[]="look at me :)";
      print(s);
      print(k);
      print(x);
      
      return 0;
    }


    But how wasetfull is this, regarding the memory... because it remainds me of Perl and i know that perl has a hudge overhead when storing strings ?? Is it (memory-wise) the same as a "string" in c? is it slow to retrieve a particular symbol or is it constant time process? for example if i want to get the 10000000th simbol. In c this is constant time , but here ??? What is the price i pay for storring strings this way ??
    Last edited by baxy; 11-25-2012 at 12:51 PM. Reason: PS
    Waras likes this.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,884
    Quote Originally Posted by baxy
    Is it (memory-wise) the same as a "string" in c?
    I would expect slightly more memory usage in general, but it depends on the implementation and your code.

    Quote Originally Posted by baxy
    is it slow to retrieve a particular symbol or is it constant time process?
    operator[] and the at() member functions run in constant time.

    By the way, do not place using directives before header file inclusions unless you have very good reasons for doing so as the using directive could change the meaning of the code included.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overload function help
    By Rob31645 in forum C++ Programming
    Replies: 6
    Last Post: 03-03-2012, 03:08 AM
  2. Function template overload/specialisation
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2008, 01:11 PM
  3. ambiguity error caused by function overload?
    By Elhaz in forum C++ Programming
    Replies: 9
    Last Post: 10-02-2004, 07:38 PM
  4. Function Overload in C
    By Cikotic in forum C Programming
    Replies: 6
    Last Post: 07-05-2004, 03:54 PM
  5. Function Pointer Assigned To Map Overload Resolution Problem!
    By DeepBlackMagic in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2004, 05:41 AM

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