Prev: help on round robin tournament
Next: sizeof: Why parentheses needed when used with data types (when not required with variables)
From: silentway on 7 Nov 2009 22:52 I am trying to create a simple vector class with overloaded operators for assignment, +=, -=, +, *, etc My code for the overloaded operators worked OK, until I tried operator+ () The problem is that I can not return a local variable. This is fair enough, but I am sure that this used to work ok 10 years ago, when I was using Zortech compiler. I am back to C++ after an outrageously long break without doing any codng. I am now using g++ compiler. Here is my code #include <iostream> class vec { public: vec(float x = 0.0) : mx(x) { printf("in constructor\n"); }; vec(vec &rv) : mx(rv.mx) { printf("in copy constr\n"); }; virtual ~vec() {}; void setx(float x) {mx = x;}; vec & operator=(const vec &rv); vec & operator+=(const vec &rv); const vec operator+(const vec &A) /*const*/; void print(char *msg); private: float mx; }; vec & vec::operator=(const vec &rv) { printf("in vec & vec::operator=(const vec &rv)\n"); if (this != &rv) mx = rv.mx; return *this; } vec & vec::operator+=(const vec &rv) { printf("in vec & vec::operator=(const vec &rv)\n"); mx += rv.mx; return *this; } // this compiles ok, but produces error if overloaded + is used in code const vec vec::operator+(const vec &A) /*const*/ { printf("in vec vec::operator+(const vec &A)\n"); vec result(*this); result += A; return result; } void vec::print(char *msg) { printf("%s %f\n", msg, mx); } int main () { vec A(0.5); A.print("A:"); vec B(0.1); B.print("B:"); vec C(B); C.print("C:"); vec D = A; D.print("D:"); vec E; E.print("E:"); E = A; E.print("E:"); B += A; B.print("B:"); // vec F = A + B; // problem!! //error: no matching function for call to 'vec::vec(vec)' return 0; } The class and test code works OK except for the line vec F = A + B; I am compiling from the command line as follows g++ -g vec.cpp -o vec -lm The error message is error: no matching function for call to 'vec::vec<const vec>' I have seen lots of examples that use the above code (or similar), but they don't seem to work for me I am starting to suspect that gcc / g++ does not support something here, but that sounds too crazy Can someone point me in the right direction? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: tohava on 8 Nov 2009 03:18 On Nov 8, 5:52 pm, silentway <andybolso...(a)yahoo.co.uk> wrote: > The error message is > error: no matching function for call to 'vec::vec<const vec>' Weird, you have no templates, did you mean () instead of <>. I would guess this error results from lack of copy constructor that accepts a const reference. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas J. Gritzan on 8 Nov 2009 03:17 silentway schrieb: > I am trying to create a simple vector class with overloaded operators > for assignment, +=, -=, +, *, etc > My code for the overloaded operators worked OK, until I tried operator+ > () > The problem is that I can not return a local variable. Let's see... > class vec > { > public: > vec(float x = 0.0) : mx(x) { printf("in constructor\n"); }; > vec(vec &rv) : mx(rv.mx) { printf("in copy constr\n"); }; Your copy constructor takes a non-const reference. > const vec vec::operator+(const vec &A) /*const*/ { Your operator+ returns a temporary. If you do vec v = a + b; the temporary returned by op+ cannot be used to copy-construct v, because temporaries can only be bound to const references. So change your copy ctor to vec(const vec &rv) and it should work. -- Thomas [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: SG on 8 Nov 2009 03:16 On 8 Nov., 16:52, silentway wrote: > I am trying to create a simple vector class with overloaded operators > for assignment, +=, -=, +, *, etc > My code for the overloaded operators worked OK, until I tried > operator+() [snip] > class vec > { > public: > vec(float x = 0.0) : mx(x) { printf("in constructor\n"); }; > vec(vec &rv) : mx(rv.mx) { printf("in copy constr\n"); }; > virtual ~vec() {}; > void setx(float x) {mx = x;}; > vec & operator=(const vec &rv); > vec & operator+=(const vec &rv); > const vec operator+(const vec &A) /*const*/; > void print(char *msg); > private: > float mx; > }; Your copy constructor takes a reference-to-NON-CONST. This probably explains the errors you're getting. You should get rid of your copy ctor, assignment operator *and* destructor (unless you want it to be virtual for some reason). The compiler-generated ones are correct and should be preferred. > vec & vec::operator=(const vec &rv) { > printf("in vec & vec::operator=(const vec &rv)\n"); > if (this != &rv) > mx = rv.mx; > return *this; > } No need for a custom assignment operator. Get rid of it. > // this compiles ok, but produces error if overloaded + is > // used in code > const vec vec::operator+(const vec &A) /*const*/ { > printf("in vec vec::operator+(const vec &A)\n"); > vec result(*this); > result += A; > return result; > } should be a const member function. > vec F = A + B; // problem!! This is an attempted copy initialization. Your operator+ returns a 'const vec' but vec's copy ctor takes a reference-to-non-const. > The error message is > error: no matching function for call to 'vec::vec<const vec>' ....which is true since your copy ctor doesn't take a 'const vec'. Cheers, SG -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: marcin.sfider on 8 Nov 2009 03:16
On Nov 8, 4:52 pm, silentway <andybolso...(a)yahoo.co.uk> wrote: > I am trying to create a simple vector class with overloaded operators > for assignment, +=, -=, +, *, etc > My code for the overloaded operators worked OK, until I tried operator+ > () > The problem is that I can not return a local variable. This is fair > enough, but I am sure that this used to work ok 10 years ago, when I > was using Zortech compiler. I am back to C++ after an outrageously > long break without doing any codng. I am now using g++ compiler. > > Here is my code > > #include <iostream> > > class vec > { > public: > vec(float x = 0.0) : mx(x) { printf("in constructor\n"); }; > vec(vec &rv) : mx(rv.mx) { printf("in copy constr\n"); }; Copy constructor should have parameter passed by const reference like this: vec(const vec& rv) : mx(rv.mx) { /* wathever */ } > virtual ~vec() {}; > void setx(float x) {mx = x;}; > vec & operator=(const vec &rv); > vec & operator+=(const vec &rv); > const vec operator+(const vec &A) /*const*/; There is no need (I can think of) for returning const value and yes, this method should rather be const (you do not whant to change value in this vector while calculating its sum with A) > void print(char *msg); > private: > float mx; > > }; > > int main () { > vec A(0.5); A.print("A:"); > vec B(0.1); B.print("B:"); > vec C(B); C.print("C:"); > vec D = A; D.print("D:"); > vec E; E.print("E:"); > E = A; E.print("E:"); > B += A; B.print("B:"); > // vec F = A + B; // problem!! > //error: no matching function for call to 'vec::vec(vec)' > return 0; The error occures because you do not have a means to copy from const value (look at your copy constructor) and the addition operator returns const value (look at your addition operator). > > } > > The class and test code works OK except for the line > vec F = A + B; > > I am compiling from the command line as follows > g++ -g vec.cpp -o vec -lm > > The error message is > error: no matching function for call to 'vec::vec<const vec>' > > I have seen lots of examples that use the above code (or similar), but > they don't seem to work for me > I am starting to suspect that gcc / g++ does not support something > here, but that sounds too crazy > Can someone point me in the right direction? Cheers Sfider -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |