Thread: Function Overloading Not working?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Function Overloading Not working?

    Hello,
    When i compile the below given code i get error. But i am not sure as to why such error is coming?i am just trying to overload the strlen() function! Please give me the reason for it

    Code:
    #include <string.h>
    #include <iostream.h>
    class set
    {
     char *a;
       public:
       set(){}
       set(char *b)
       {
    	   a=b;
       }
    	int strlen()
    	{
    		return strlen(a);
    	}
    
    };
    
    int main(void)
    {
     clrscr();
     char *p="abc";
     set a(p);
     cout<<strlen(p)<<endl;
     cout<<a.strlen();
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you could post WHAT error you are getting, instead of us trying to do telepathy on what your compiler is doing. I suspect the problem is that the compiler doesn't know WHICH strlen you are referring to, so you should perhaps use ::strlen(a) in your set::strlen() function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    You should be using the headers without the .h extension. Also, as far as C++ goes, I think strlen is found in <cstring>

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    You are right! matsp but how? i have just overloaded the function and the strlen() function has 1 parameter that is an character type array and my set::strlen() doesn't have any argument so it should work because the input parameters are different in function overloading. Then why is it not working??

    the parameters are clearly different set::strlen() doesn't have any parameter then how come?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    clrscr is not standard

    you are missing std namespace, should be
    std::cout
    std::strlen
    etc

    char* p should be const char* p
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, it seems that you need a newer compiler.

    Code:
    #include <cstring> // 1)
    #include <iostream> // 2)
    using namespace std; // 3) 
    
    class set
    {
     char *a;
       public:
       set(){}
       set(char *b)
       {
    	   a=b;
       }
       int strlen()
       {
          return ::strlen(a); // 4)
       }
    
    };
    
    int main(void)
    {
     //clrscr(); // 5)
     char *p="abc";
     set a(p);
     cout<<strlen(p)<<endl;
     cout<<a.strlen();
    return 0;
    }
    1) Standard headers inherited from C don't have extension (.h) and are prefixed with c.
    2) C++ standard headers don't have extension.
    3) namespaces are a feature of C++ that your compiler apparently hasn't heard of.
    4) This is apparently your direct problem. The compiler cannot tell which strlen you mean. If you insist on using the same name (IMO the naming conventions of C libraries are rather awful) you can tell it that you mean the global strlen by prefixing ::
    5) No such thing as clrscr in standard C++. IMO this is quite unnecessary here too: if the user wants to clear the console, they may type cls before running the program.

    Other than that sharing resources is quite difficult to get right and awkward to use. Your class does not own the string, it only has a pointer to something that is not under its control. If the shared string goes out of scope or is freed, the set class is left with a dangling pointer.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All functions must have a return type in C++.
    And void in the argument list such as, int main(void), is not required either. int main() is fine.
    And for the new GUI, here are some popular ones: http://cpwiki.sf.net/Integrated_Development_Environment
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM