**this is the code and it keeps giving me a red line under (z=v2*v1),,please help me finding the mistake :( :(
thanks ^_^

#pragma once
using std::cout;
using std::cin;
using std::ostream;
using std::istream;

class vector{
public:
    friend ostream& operator<< (ostream&,vector&);
    friend istream& operator>> (istream&,vector&);
    friend vector operator*(vector &,vector &);
    vector(void);
    vector(int);
    ~vector(void);
    private:
int *my_vector;
int n;
};
#include<iostream>
#include "vector.h"
using namespace std ;
vector::vector(){
    n=2;    
    my_vector=new int[n];
}
vector::vector(int n1) {
    n=n1;
    my_vector=new int[n];
}
vector::~vector(){}
istream& operator>>(istream& in,vector& v){
    cout<<"please enter the values of the vector"<<endl;
    for(int i=0;i<(v.n);i++){
        in>>v.my_vector[i];
    }
    return in;
}
ostream& operator<<(ostream& out,vector& v ){
for (int j = 0; j <(v.n); j++){
        out<<" "<<v.my_vector[j];   
    }
    return out;
}

vector operator*(vector &v,vector &v1){
    double z;
    cin>>z;
    for(int m=0;m<v.n;m++){
        z=v.my_vector[m]*v1.my_vector[m];
    }
    return z;
 }
 #include<iostream>
#include "vector.h"
using namespace std ;
int main (){
    vector v1(3);
    vector v2(3),v3(3);
    double z;
cin>>v1;
    cin>>v2;
    z=v2*v1;
    cout<<"V1 :"<<v1<<endl;
        cout<<"V2 :"<<v2<<endl;
        cout<<"The addition :"<<v3<<" "<<"v1 :"<<v1<<endl;

        getchar();
getchar();
return 0; 
}

Recommended Answers

All 2 Replies

The return type of your * operator is vector. So when you return a double from that operator, it gets automatically converted to vector (via your constructor - if you don't want implicit conversion from double to vector, mark your constructor as explicit). However there's no implicit conversion from vector to double, so trying to assign the vector v2*v1 to the double z is an error.

Since there's no reason for the result of multiplication operator to be a vector, you should probably just change the return type to double.

Thank U so much ^_________________^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.