help with coordinate adding program(classes)

hello i am pretty new to coding and am having a problem with this code. it's supposed to add two coordinates and is supposed to teach me about overloading operators. any help would be greatly appreciated
every time i compile i get this

main.cpp: In member function `Coordinate Coordinate::operator=(const Coordinate&) const':
main.cpp:26: error: invalid use of `class Coordinate'
main.cpp:27: error: invalid use of `class Coordinate'
main.cpp:28: error: invalid use of `class Coordinate'



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
#include "Coordinate.h"
using namespace std;

    Coordinate::Coordinate (double, double, double)
    {
        double x = 0.0;
        double y = 0.0;
        double z = 0.0;
    }
    Coordinate Coordinate::operator+(const Coordinate & a)const
    {
        return Coordinate(x + a.x , y + a.y, z + a.z);
    }
    Coordinate Coordinate::operator-(const Coordinate& a)const
    {
        return Coordinate(x - a.x, y - a.y, z - a.z);
    }
    Coordinate Coordinate::operator*(const Coordinate& a)const
    {
        return Coordinate(x * a.x, y * a.y, z * a.z);
    }

    Coordinate Coordinate::operator=(const Coordinate& a)const
    {
        x = a.Coordinate;
        y = a.Coordinate;
        z = a.Coordinate;
        return *this;
    }

    bool Coordinate::operator==(const Coordinate& a)const
    {
        return true;
    }
    bool Coordinate::operator!=(const Coordinate& a)const
    {
        return true; // can return (!(*this)==a)
    }
    ostream &operator<<(ostream &output, const Coordinate& a)
    {
        output << endl;
        return output;
    }
    istream &operator>>(istream &input, Coordinate& a)
    {
        return input;
    }


#include<iostream>
using namespace std;
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
    private:
        double x, y, z;
    public:
        friend ostream &operator<<( ostream &, const Coordinate & );
        friend istream &operator>>( istream &, Coordinate &);
        Coordinate (double a = 0.0, double b = 0.0, double c= 0.0);
        Coordinate operator+(const Coordinate & a) const;
        Coordinate operator-(const Coordinate&) const;
        Coordinate operator*(const Coordinate&) const;
        Coordinate operator=(const Coordinate&) const;
        bool operator==(const Coordinate&) const;
        bool operator!=(const Coordinate&) const;
};
#endif


#include "Coordinate.h"
#include <iostream>
using namespace std;
int main()
{
    Coordinate a, b( 4, 8, 16 ), c( 3, 1, -2 ), k;
    cout << "Enter a graphical coordinate in the form: (x, y, z)\n? ";
    cin >> k;
    a = b + c;
    cout << "\na = b + c:\n" << a << " = " << b << " + " << c << '\n';
    a = b - c;
    cout << "\na = b - c:\n" << a << " = " << b << " - " << c << '\n';
    a = b * c;
    cout << "\na = b * c:\n" << a << " = " << b << " * " << c << "\n\n";
    if ( a != k )
    {
        cout << a << " != " << k << '\n';
    }
    cout << '\n';
    a = k;
    if ( a == k )
    {
        cout << a << " == " << k << '\n';
    }
}

You have some issues on lines 26-28.
am i not writing it correctly or is it that i am completely off on overloading the assignment operator
You aren't writing it correctly. What does a.Coordinate mean?
i thought that was how you initialized it to work. Like the a was used to call it
1
2
3
4
5
6
7
 Coordinate Coordinate::operator=(const Coordinate& a)const //<---- you are promising to keep all members constant!
    {
        x = a.Coordinate; //error line
        y = a.Coordinate; //error line
        z = a.Coordinate; //error line
        return *this;
    }


You can remove the const or the error lines marked above.
I'd suggest removing the const, because it wouldn't make much sense to make an assignment operator promise not to change anything.
Last edited on
Topic archived. No new replies allowed.