operator overloadings!

errrors;-no matches found for (vec)(int[3]) in lines
v1(x);
v2(y);
what may be the possible reasons!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include <iostream>
using namespace std;


class vec{
    int *v;
    int siz;

    public:
        vec(int m){
        siz=m;
        v=new int[siz];
        }
        vec(int *a)
        {
        for(int i=0;i<siz;i++)
        {
        v[i]=a[i];
        }
        }
        void operator*(vec &y){
        int i;
        int temp[siz];
        for(i=0;i<siz;i++){

        temp[i]=(*this).v[i]*y.v[i];
        }
        for(i=0;i<siz;i++)
        cout<<v[i]<<" "<<endl;
        }


};
int main()
{
int x[3]={1,2,3};
int y[3]={4,5,6};
vec v1(3);
vec v2(3);
v1(x);
v2(y);
v1*v2;
    return 0;
}
Last edited on
closed account (z05DSL3A)
What Are you trying to do at line 40 and 41?
yes!
and i also tried v1=x;
v2=y;
giving the same errors!@
Last edited on
on line 40/41: what are you trying to do here?
The objects are created hence no constructor will be called (if that is your intention)
i mean can we call more than one constructor using one object also want to remember the output of first constructor!
@coder777
than can we not call constructor more than one time...?(with single object)
The constructor is only called once.

It is not standard c++ for you to create arrays the way you are.
1
2
int x = 10;
int array[x]; // should not compile 
You seem to have misunderstood what a constructor is. Your constructor doesn't "output" anything - it sets the initial state of the object, when the object is created.

So on lines 38 and 39, you create two objects, and your constructors automatically set the initial states of those objects, by setting siz allocating the array v.

Lines 40 and 41 are meaningless. Those objects are already created - you can't call the constructor on them again.

Your operator* is weird. It doesn't follow the usual semantics of a * operator at all.
Last edited on
the constructor will be called during the construction of the object (as the name implies).

you can overload other operators to provide new data to the object:

operator=(...)

or to make lines 40/41 valid:

operator()(...)


By the way: the constructor on line 14 is bad. It uses the uninitialized members siz and v.
The behavior is undefined
i mean can we call more than one constructor using one object also want to remember the output of first constructor!

No. This is where other methods fit in.

1
2
vec v1(3);
v1.set_data(x); // does what you char* constructor does 


But it would be better is your set_data method (and constructor) also took a size parameter, e.g. vec(int *a, int size)

Then you can't accidentally do

1
2
3
4
int x[3]={1,2,3};

vec v1(16);
vec.set_data(x); // problems! 


vec(int *a, int size) and set_data(int *a, int size) give you the chance to check the new size against the size of the allocated array and either terminate the call or reallocate memory.

Andy
Last edited on
@LowestOne
the second thing you are saying i am not agre with that i have created a function like that and that is working well! here it is-
#include <iostream>

using namespace std;

int main()
{
int x,i;
cin>>x;
cout<<endl;
int a[x];
for(i=0;i<x;i++)
{
cin>>a[i];
}
for(i=0;i<x;i++){
cout<<a[i];
}
return 0;
}
the second thing you are saying i am not agre with that i have created a function like that and that is working well!

It may work on your compiler, but it's not standard C++. It must be an extension.
These calls

v1(x);
v2(y);

are calls of the function call operator. You did not define the corresponding operator function with for example the following declaration

T operator ()( int *a ) const;

where T is some return type ( you may use any return type you want).

So as you did not define this function call operator the compiler issues an error.

Topic archived. No new replies allowed.