why am i getting: " 'operator +' not implemented in type ... for arguments of type unsigned int'
when i try to use an overloaded [] operator?
Printable View
why am i getting: " 'operator +' not implemented in type ... for arguments of type unsigned int'
when i try to use an overloaded [] operator?
Show the smallest and simplest program that demonstrates the problem.
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];
There is no way I can get your code to compile. You need to provide something that you think should compile, but does not.
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
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.
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.
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
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...
doesn't complain here.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 elsewhere when i've used spectrum::[] either.
i'm so confused :(
argh. got it.
input was declared const and [] is nonconst. awfully strange and misleading error message.