Overloaded size_t operator

This is a discussion on Overloaded size_t operator within the C++ Programming forums, part of the General Programming Boards category; I am trying to compile a specific library, that apparently compiles fine for the author, but not for me. The ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    7

    Overloaded size_t operator

    I am trying to compile a specific library, that apparently compiles fine for the author, but not for me. The following code demonstrates the problem (at least for me):

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Test
    {
    public:
        inline void operator<<(unsigned long int val) {};
        inline void operator<<(size_t val) {};
    };
    
    int main(void)
    {
    }
    Code:
    Test.cpp:9: error: ‘void Test::operator<<(size_t)’ cannot be overloaded
    Test.cpp:8: error: with ‘void Test::operator<<(long unsigned int)’
    My compiler version details:

    Code:
    Using built-in specs.
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib 
    --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 
    --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-
    linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
    He and I are both using gcc 4.2.3.

    As far as I can tell, the error is because 'size_t' is effectively the same as 'unsigned long'? Should I be able to compile this code, and/or is there some way that I can make it compile (other than removing one of the overloads)? Cheers.

    Joe
    Last edited by hpesoj; 06-30-2008 at 07:59 AM. Reason: Line of code was overly long.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Unfortunately, there is no definition for size_t.
    It is up to the library implementors to define what type size_t is, so you cannot rely on it being unsigned long. It can be unsigned long long, or even unsigned char, as well.
    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.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    But surely if I'm compiling exactly the same code as them, 'size_t' should be defined the same for both of us? I included iostream in the example because it seemed that was where size_t was defined the library code. I suppose it could have been defined elsewhere, but seeing as I get the same error compiling this code as I do the library, it would seem size_t is defined as unsigned long in any case.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Quote Originally Posted by hpesoj View Post
    But surely if I'm compiling exactly the same code as them, 'size_t' should be defined the same for both of us?
    Not if you use different compilers.

    I included iostream in the example because it seemed that was where size_t was defined the library code. I suppose it could have been defined elsewhere, but seeing as I get the same error compiling this code as I do the library, it would seem size_t is defined as unsigned long in any case.
    It depends on the compiler.
    On VC++, std::size_t is defined as unsigned int.
    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.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    He states that they both use gcc 4.2.3

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    I pinpoint the facts. There may be something else amiss, but I would not overload an operator with size_t and then another operator. It can cause problems on other compilers.
    But I'm not an error on GCC errors, so I cannot say what exactly the error is.
    Last edited by Elysia; 06-30-2008 at 08:48 AM.
    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.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Is your friend on x86_64 too?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 11:53 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

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