Problem with Array with VECTOR

targetver.h
1
2
3
4
#include <iostream>
#include <vector>
using namespace std;
#include "Array.h"   


Array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once

class Array
{
private:
    float *a;
public:
    friend istream& operator >> (istream& is, vector<float> &a);
    friend ostream& operator << (ostream& os, vector<float> &a);
    
    void Sort(vector<float> a);


    Array () {
        a = NULL;
    }

};  


Array.cpp
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 "StdAfx.h"
#include "Array.h"


istream& operator >> (istream& is, vector<float> &a) {
    
    float temp;
    a.resize(0);

    while (is >> temp) {
        a.push_back(temp);
    }
    
    return is;
}

ostream& operator << (ostream& os, vector<float> &a) {
    

    for (int i = 0; i < a.size(); i++) {
        os << a[i] << " ";
        

    }
    return os;

}

/* Sort */
void Array::Sort(vector<float> a) {

    int i, j;
    int temp;
    for (i = 0; i < a.size(); i++) {
        for (j = i + 1; i < a.size() - 1; j++) {
            if (a[i] > a[j])
            {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
        
    }
}  


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"


void main()
{
    vector<float> a;
    Array arr;
    int n;
    cout << "click Ctrl + A and then Enter => Result" << endl << endl;
    cout << endl;

    cout << "Input: ";
    cin >> a;

    cout << "Result: ";
    cout << a << endl;
    
    Array temp = arr;
    temp.Sort(a);
    cout << temp << endl;
    
}  


The problem is when I call the "Sort", this error appear and I don't know how to work it out. Help me for this error

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Array' (or there is no acceptable conversion)
You haven't defined:
std::ostream &operator<<(std::ostream&, const Array &);
but you've called it with:
1
2
    Array temp = arr;
    cout << temp << endl;

Could U give me more detail or code something ?
You wrote a function to output vectors but you have not written a function to output objects of class Array.
Your Array class doesn't do anything.

It has one member, a pointer to a double called a which is initialised to NULL.

Line 7: Declares an Array called arr.
Line 18: Declares an Array temp and intialises it with arr.
Line 19: Calls Array::Sort, which sort the vector<double> passed in. However, because it's passed by value, nothing really changes.
Line 20: Call a.operator<<(std::ostream&, const Array &), but you haven't defined it, so compile fails.

Finally, void main() should be int main() and you should return a zero at the end.
Last edited on
Topic archived. No new replies allowed.