Sep 18, 2015 at 8:18pm UTC
I have overloaded the operator<< but what it gets as a argument is a non-const...I think if the function of operator<< is just display the information it should be a const value...i have tried to do it like that but , I dont get it done....any help?? thanks
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <stdio.h>
#include <iostream>
using namespace std;
class Vector{
int sz;
double * elem;
public :
Vector(const int a):sz{a},elem{new double [a]}{}
Vector(const Vector& a):sz{a.sz},elem{ new double [a.sz]}{
for (int i = 0;i != a.sz;i++){
elem[i] = a.elem[i];
}
}
~Vector(){delete [] elem;}
int get_sz () const {
double temp = sz;
return temp;
}
double * get_elem(){
double * ptr = elem;
return ptr;
}
};
void rellenador(Vector& a){
double * ptr = a.get_elem();
for (int i = 0;i != a.get_sz();i++){
cout<<"Enter value " <<endl;
int temp;
cin>>temp;
ptr[i] = temp;
}
}
ostream& operator <<(ostream& os,Vector& a){
double * ptr;
ptr = a.get_elem ();
for (int i = 0;i != a.get_sz();i++){
cout<<*ptr<<endl;
ptr++;
}
return os;
}
int main()
{
Vector test{3};
rellenador(test);
Vector test1{test1};
}
Last edited on Sep 18, 2015 at 8:28pm UTC
Sep 18, 2015 at 9:02pm UTC
Change your overloaded operator 2nd parameter to const Vector &a
Sep 18, 2015 at 9:08pm UTC
nope , it discards qualifiers....
Sep 18, 2015 at 9:18pm UTC
You should change the get_elem() function to be const, by declaring it double * get_elem() const
Sep 18, 2015 at 9:22pm UTC
so , I can pass the value from a const to a non-cosnt??, because I was thinking it was that...
Sep 18, 2015 at 9:30pm UTC
A const function just means that it doesn't modify any of the member functions of the class. It returns a value just like any other function and so ptr can be assigned to the return of get_elem()
Sep 19, 2015 at 12:30pm UTC
yes, it returns a rvalue, it's what i though but i was getting that mistake and I was getting confused, the error was in other part, thanks!!