[ ] operator requires + operator?

This is a discussion on [ ] operator requires + operator? within the C++ Programming forums, part of the General Programming Boards category; why am i getting: " 'operator +' not implemented in type ... for arguments of type unsigned int' when i ...

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    [ ] operator requires + operator?

    why am i getting: " 'operator +' not implemented in type ... for arguments of type unsigned int'

    when i try to use an overloaded [] operator?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Show the smallest and simplest program that demonstrates the problem.
    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

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Code:
    class spectrum
    {
        private:
            const unsigned int length;
            double* const pdata;
    
        protected:
            __fastcall spectrum(const String _name,double * const _data,const unsigned int tsize) :
            name(_name),
            pdata(_data),
            length(tsize),
        {
        }
    
        public:
            const String name;
            const unsigned __fastcall size(){return length;}
            double &spectrum::operator [](const unsigned int i)
            {
                if(i>=length)
                {
                    throw out_of_range( ("invalid spectrum index: "+(String)i+" max = "+(String)length ).c_str() );
                }
                return pdata[i];
            }
    
    };
    
    template<unsigned int tsize>class tspectrum : public spectrum
    {
        protected:
            double data[tsize];
    
        public:
            __fastcall tspectrum(String _name) :
                spectrum(_name,data,tsize)
            {
                fill(0);
            }
    
    // ...
    };
    
    
    // elsewhere...input is a spectrum&
    
            double lowervalue = input[lowerpixel];
    Last edited by m37h0d; 02-04-2009 at 08:14 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    There is no way I can get your code to compile. You need to provide something that you think should compile, but does not.
    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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Completely side-tracked, but WHY are you using __fastcall? Both of the functions where you do that are highly unlikely to inline directly in the code (as it's only like two-three instruction for the size() function, and about 10 or so for the constructor).

    --
    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.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    I took the source and hacked it so it would compile. I got these warnings...
    Warning 1 warning C4166: illegal calling convention for constructor/destructor d:\w00t\documents\visual studio 2008\projects\temp\temp4.cpp 15
    Warning 2 warning C4180: qualifier applied to function type has no meaning; ignored d:\w00t\documents\visual studio 2008\projects\temp\temp4.cpp 25
    Warning 3 warning C4512: 'spectrum' : assignment operator could not be generated d:\w00t\documents\visual studio 2008\projects\temp\temp4.cpp 35
    Warning 4 warning C4166: illegal calling convention for constructor/destructor d:\w00t\documents\visual studio 2008\projects\temp\temp4.cpp 43
    ...but aside that, it compiled without problems (I did comment out the throw statement).
    You need to provide a small sample that is compilable and reproduces the problem.
    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
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    re: mats

    just habit. i have no idea what the difference is or the implications are between 'passing the arguments as registers' as my documentation says or the stdcall convention. my compiler's docs say "don't use fastcall if you don't want it called fast"... which begs the question 'why would i ever want it called less fast?'... it's really just something i've absorbed from using borland. when the time comes, it will be easy enough to get rid of it.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, first of all, stdcall is not the normal calling convention for class member functons - it's something called "thiscall". Non-member functions (and statics) are called with cdecl calling convention.

    As Elysia's warning list points out, it's not legal to use call modifiers on constructors and destructors (because the code that calls the constructor is not part of your source-code, and it makes assumptions on the calling convention).

    Of course, passing arguments in registers is an advantage in most compilers, but if the function is inlined, it doesn't make any difference - but registers are scarce resources in x86 (32-bit) processors, so using some of the registers for arguments isn't ALWAYS a good idea, because it makes it harder for the compiler to find scratch-registers for temporary results when calculating the arguments - use with care.

    --
    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.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    The C4180 warnings applies to the line
    const unsigned int __fastcall size(){return length;}
    It will stop complaining if you remove const.
    You should also probably put a type in there after unsigned...
    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.

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Code:
    //---------------------------------------------------------------------------
    #include <stdexcept>
    #include <vcl>
    #pragma hdrstop
    using namespace std;
    class spectrum
    {
        private:
            const unsigned int length;
            double* const pdata;
    
        protected:
            spectrum(const string _name,double * const _data,const unsigned int tsize) :
            name(_name),
            pdata(_data),
            length(tsize)
        {
        }
    
        public:
            const string name;
            const unsigned size(){return length;}
            double &spectrum::operator [](const unsigned int i)
            {
                if(i>=length)
                {
                    throw out_of_range("invalid spectrum index");
                }
                return pdata[i];
            }
    
    };
    
    template<unsigned int tsize>class tspectrum : public spectrum
    {
        protected:
            double data[tsize];
    
        public:
            tspectrum(string _name) :
                spectrum(_name,data,tsize)
            {
    
            }
    
    // ...
    };
    
    void test(spectrum &input,unsigned index)
    {
        double value = input[index];
    }
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
        tspectrum<256>s("test");
        test(s,0);
    }
    //---------------------------------------------------------------------------
    doesn't complain here.

    doesn't complain elsewhere when i've used spectrum::[] either.

    i'm so confused

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    argh. got it.

    input was declared const and [] is nonconst. awfully strange and misleading error message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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