Thread: Function overload

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

    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
    2,738
    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
    222
    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 01:51 PM. Reason: PS

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 04:08 AM
  2. Function template overload/specialisation
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2008, 02: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. Replies: 1
    Last Post: 05-01-2004, 05:41 AM