I need help with a program i am writing( i am new to programing). I am writing a program to Coordinates have the form (x, y, z)

Use double variables to represent the private data of the class.
Provide a constructor that enables an object to be initialized when it is declared and has default values of 0.0.
Provide a friend function << for cout
Provide a friend function >> for cin
Provide an overloaded operator + for this class
Provide an overloaded operator - for this class
Provide an overloaded operator * for this class
Provide an overloaded operator = for this class
Provide an overloaded operator == for this class
Provide an overloaded operator != for this class
The output should look as follows:

Enter a graphical coordinate in the form: (x, y, z)
? (3, 4, 5)
a = b + c:
(7, 9, 14) = (4, 8, 16) + (3, 1, -2)

a = b - c:
(1, 7, 18) = (4, 8, 16) - (3, 1, -2)

a = b * c:
(12, 8, -32) = (4, 8, 16) * (3, 1, -2)

(12, 8, -32) != (3, 4, 5)

(3, 4, 5) == (3, 4, 5)

what i have so far are my header file Coordinate.h

#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 x = 0.0, double y = 0.0, double z = 0.0);
        void get_coordinates(double, double, double);
        Coordinate operator+(const Coordinate&) 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

the implementaion file Coordinate.cpp

#include <iostream>
#include "Coordinate.h"
using namespace std;

Coordinate Coordinate::operator+(const Coordinate&)
{
    return Coordinate(x + a.x , y + a.y, z + a.z);
}

Coordinate Coordinate::operator-(const Coordinate&)
{
    return Coordinate(x - a.x, y - a.y, z - a.z);
}

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

Coordinate Coordinate::operator==(const Coordinate&)
{
    return (*this == right);
}

Coordinate Coordinate::operator!=const Coordinate&)
{
    return;
}
ostream &operator<<(ostream &output, const Coordinate&)
{
    output << endl;
    return output;
}

istream &operator>>(istream &input, Coordinate&)
{
    input << endl;
    return input;
}

and the main function

#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';
    }
}

when i try to compile i get crazy errors. i'm pretty sure my mistakes are in the Coordinate.cpp file. Any help or advice would be greatly appreciated

Recommended Answers

All 14 Replies

What are the "crazy errors"? I suspect that it's an error in a templated function (they tend to give very verbose error messages). It's probably to do with this function:

istream &operator>>(istream &input, Coordinate&)
{
    input << endl;
    return input;
}

This will also give you an error:

Coordinate Coordinate::operator!=const Coordinate&)
{
    return;
}

should i just copy the errors on to here or just sum them up?

and so for the istream and ostream i am lost on how to use any tips or thoughts?

and fot the != operator would it be something like

return !(this->operator == (Coordinate&));

Here's an example of your mistake:

Coordinate Coordinate::operator+(const Coordinate&)
{
    return Coordinate(x + a.x , y + a.y, z + a.z);
}

Should be: ( notice the "a" and the const )

Coordinate Coordinate::operator+(const Coordinate & a)const
{
    return Coordinate(x + a.x , y + a.y, z + a.z);
}

Notice that this is your .h file: do this fix for all functions and it should work

Coordinate operator+(const Coordinate &) const;

and here is the complete fixed code ( to compile ) with comments

Coordinate.cpp

#include "Coordinate.h"
using namespace std;
Coordinate Coordinate::operator+(const Coordinate & a)const // added const and "a"
{
    return Coordinate(x + a.x , y + a.y, z + a.z);
}
Coordinate Coordinate::operator-(const Coordinate& a)const // added const and "a"
{
    return Coordinate(x - a.x, y - a.y, z - a.z);
}
Coordinate Coordinate::operator*(const Coordinate& a)const // added const and "a"
{
    return Coordinate(x * a.x, y * a.y, z * a.z);
}
bool Coordinate::operator==(const Coordinate& a)const // added const and "a"
{
    return true;
    //return (*this == a); // should return this.x == a.x && .y==y && .z==z - 
    //because return (*this)==a just goes back into this function recursivley and OMG
}
bool Coordinate::operator!=(const Coordinate& a)const // added const and "a"
{
    return true; // can return (!(*this)==a)
}
ostream &operator<<(ostream &output, const Coordinate& a)
{
    output << endl;
    return output;
}
istream &operator>>(istream &input, Coordinate& a)
{
    //input >> a.x; ( this is the cin operator , you read from it not to it ) 
    return input;
}




Coordinate.h

#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 x = 0.0, double y = 0.0, double z = 0.0);
        void get_coordinates(double, double, double);
        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



Main.cpp

#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';
    }
}

also instead of ifndef you can use
#pragma once
at the top of each file.

good luck

ok i get what i missed now. Thanks. But when i tried the code out it gives the error

undefined reference to 'Coordinate::Coordinate(double, double, double)'

on pretty much every overloaded operator. should i take out the
void get_coordinates(double, double, double);
in the header or is the code not implementing them right so it gives me the error?

You've declared the Coordinate::Coordinate(double, double, double) constructor. But you haven't provided a definition for this function. You need to do that if you want to actually call the function in your code (which you do, multiple times).

This goes for the get_coordinates function too (although I don't see a call to that, so it shouldn't be giving you any trouble (yet)).

i see so would i write something like

Coordinate::Coordinate(double, double, double)
{
    double x = 0.0;
    double y = 0.0
    double z = 0.0
}

in the Coordinate.cpp file above the overloaded operators?

also i noticed i did not write a function for the overloaded = operator. would that look like this

Coordinate::Coordinate operator=(const Coordinate&) const
{
    x = Coordinate.x 
    y = Coordinate.y
    z = Coordinate.z
}

i see so would i write something like...

Almost, but not quite. Yes, you need to add something, but this:

Coordinate::Coordinate(double, double, double)
{
    double x = 0.0;
    double y = 0.0
    double z = 0.0
}

won't do it. In this function definition you don't give your double input variables names, so you can't use them inside the function (the constructor, in this case). Secondly, you have assigned 0.0 to three doubles x, y & z, but they're probably not the x, y & z that you want. Since you have done double x = 0.0;, you're declaring a local variable called x and assigning 0.0 to it. This will leave the x in your Coordinate with a random, uninitialised value.

Looking at your code in these posts, it looks like you might have mis-understood the effect of Coordinate (double x = 0.0, double y = 0.0, double z = 0.0); on line 14 of the header in your original post. The values that you specify here are called default values (which you probably already know). However, they do not assign values to the member variables of the Coordinate class.

The input variables to a function (like the constructor) are local to the function. So, they (and their values) only exist for the duration of the function. You still need to define a body to the function to assign the values of the input variables to your member variables:

Coordinate::Coordinate( double x_in = 0.0, double y_in = 0.0, double z_in = 0.0 )
{
    x = x_in;
    y = y_in;
    z = z_in;
}

This is great, but the constructor is a special kind of function and you can take advantage of a feature called an initialiser list to give member variables the desiged value at the time when they're constructed. This last bit is an important distinction. When you do something like I showed above, what happens is that the constructor for all the member variables is first called and then, once that's done, the body of the constructor is exectuted. In this case, that's not too much of an issue, since constructing doubles isn't expensive. This isn't true for all possible member variables though. The initialiser list gives you the opportunity to pass values to the constructors of the member variables directly, which can be much more efficient.

So, the syntax looks like this:

Coordinate::Coordinate( double x_in = 0.0, double y_in = 0.0, double z_in = 0.0 ) : x(x_in), y(y_in), z(z_in)
{
}

The initialiser list is the : x(x_in), y(y_in), z(z_in) part. And, you can see that the body of the function now doesn't contain anything.

One further note. It might not be great to have the default values of for the variables as you have here. Would you want people to be able to do something like:

Coordinate c(1);

Something like this is probably a mistake. It's perfectly fine to define multiple constructors for a class. In this case, I'd probably go for a "default" one and one that takes exactly three arguments and use initialiser lists to set the values in both cases:

Coordinate::Coordinate() : x(0), y(0), z(0) {}   // Default construction

Coordinate::Coordinate( double x_in, double y_in, double z_in ) :
    x(x_in), y(y_in), z(z_in)
{
}

This way you can do both

Coordinate c;

and

Coordinate c(1.0,2.0,3.0);

But not

Coordinate c(1.2, 2.3);

Hope that helps.

Awesome thank you very much. So does adding the _in allow the function to take in values? And sorry to be a bother but how does my overloaded assignment operator function look? Am I in the right track?

So does adding the _in allow the function to take in values?

No, you just need to give names to the variables that you're passing into a function in order to use them in the function. The way you had it:

Coordinate::Coordinate( double, double, double )

You have no way to refer to the things that you pass in when you're writing the code for the actual function. There's nothing special about the names that you chose for the variables. I just put _in on the end so that they wouldn't have the same name as the member variables (otherwise you might run into some problems). I could have called them pretty much anything:

Coordinate::Coordinate( double ss127hssqh_32s, double daediun78230__, double sozcjne )

would have worked just as well from the point of view of the compiler. The choice of name is just for people :) In this case, I wanted to express that these are input values so I put an _in on the end of each.

how does my overloaded assignment operator function look

Sort of. You have the same issue here - you need to give the function argument a name. You have:

Coordinate::Coordinate operator=(const Coordinate&) const

but you haven't give a name to the input argument (you've just said that it's a const reference to a Coordinate), so you won't be able to use it inside the function. Give it a name and use that inside instead of Coordinate and you should have more luck.

(also take a look at Despairy's earlier answer above)

i see so does adding the x_in, y_in, and z_in assign the x,y,z i had in my class to the functions? and so the in the = opperator i added an a like Despairy said then put

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

then got the error

Coordinate.cpp:37: error: invalid use of `class Coordinate'
Coordinate.cpp:38: error: invalid use of `class Coordinate'
Coordinate.cpp:39: error: invalid use of `class Coordinate'

line 37 -39 being the x=, y=, and z= lines

then got the error...

The compiler's right, this is indeed an invalid use of Coordinate. Think about what you want the program to do. a is a Coordinate and you want to assign its x value to the x value of the class that we're currently in:

x = a.x;

i see so does adding the x_in, y_in, and z_in assign the x,y,z i had in my class to the functions?

I don't understand this question.

Ok I get it now. I was thinking you had to compare x to the value that was imputted, which i thought was a but a is like the name used to call the asignment operator. then the coordinate part was just to show that it goes in the class coordinate.
And I meant that when you initiate x_in and so on does it assign those values to the double I have in the private part of my class file.

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.