Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help needed for operator= overloading

0 views
Skip to first unread message

Jianhua Sun

unread,
May 25, 1993, 4:57:40 PM5/25/93
to
Hi, there. I have a problem with operator "=" that I have to explicitly use
operator=() instead of "=". I am working on Sun sparc station 10 with AT&T
C++ 2.1. The following is the related headfile and the test program (
Vector is a parameterized class using name2() macro, #define ... etc. were
erased here).


class Vector(type) { \
protected: \
type* v; \
int v_size; \
public: \
Vector(type)(int s) { v = new type[v_size=(s>0? s: 1)]; }\
... \
Vector(type)& operator=(const type ); \ < A
Vector(type)& operator=(const Vector(type)&); \ < B
... \
};


class NumVec(type) : public Vector(type) { \
public: \
NumVec(type)(int s) : Vector(type)(s) { ; } \
... \
};

...
#define vector NumVec

main()
{
int i, n=5;
vector(double) a(n), b(n), c(n);

a = 1.2;
b = 2.3;
c = a + b;
for (i=0; i<n; i++) cout << a[i] << ", " << b[i] << ", " << c[i] << "\n";

}


------
if main() is like this, I was told that

7 %CC -g test.c
"test.c", line 27: warning: argument 1: double passed as int
"test.c", line 28: warning: argument 1: double passed as int

and using dbxtool, I find that the main() try to call the 2nd operator = ( see
line B above) with a null pointer, not the 1st operator =.

if in main() I write

a.operator=(1.2);
b.operator=(2.3);

then the program runs ok.

I don't understand what the problem is. it seems the compiler did not agree to
assign the first = as the operator which should match the type.

Anyone there can give a help? Thanks in advance.

J. Sun

0 new messages