### Problems With Composition (Homework)

I'm have some troubles with a homework assignment for school. I've compared my file line-by-line to a classmates and cannot figure out why his compiles and mine doesn't. I'll try to keep this short, please let me know if I didn't provide enough information.

The assignment has us create a few Ship objects that have a Coordinate object. In the main file, I need to print out the coordinate and also compare them to see if a collision has taken place. Here are the files:

 ``123456789101112131415161718192021`` ``````//Ship.h #ifndef SHIP_H #define SHIP_H #include #include "Coordinate.h" using namespace std; class Ship { public: Ship (Coordinate, double, double, double); Coordinate get_location (); void update_location (); private: Coordinate coord; double x_inertia, y_inertia, z_inertia; }; #endif ``````

 ``123456789101112131415161718192021222324`` ``````//Ship.cpp #include #include "Coordinate.h" #include "Ship.h" using namespace std; Ship::Ship (Coordinate c, double x_int, double y_int, double z_int) : coord (c), x_inertia (x_int), y_inertia (y_int), z_inertia (z_int) { } Coordinate Ship::get_location () { return coord; } void Ship::update_location () { coord.setCoordinate (coord.get_x() + x_inertia, coord.get_y() + y_inertia, coord.get_z() + z_inertia); }``````

 ``12345678910111213141516171819202122232425`` ``````//Coordinate.h #ifndef COORDINATE_H #define COORDINATE_H #include using namespace std; class Coordinate { friend ostream &operator<< (ostream&, Coordinate&); private: double x, y, z; public: Coordinate (double x = 0.0, double y = 0.0, double z = 0.0); void set_coordinate (double, double, double); double get_x (); double get_y (); double get_z (); Coordinate operator+ (Coordinate&); Coordinate operator- (Coordinate&); bool operator== (Coordinate&); bool operator!= (Coordinate&); }; #endif ``````

 ``1234567891011121314151617181920`` ``````//Coordinate.cpp #include "Coordinate.h" #include using namespace std; Coordinate::Coordinate (double x, double y, double z) { set_coordinate (x, y, z); // header file will set coordinates to 0, 0, 0 if no values are // given in the ructor } // code omitted to save room ostream &operator<< (ostream &output, Coordinate &coord) { output << "(" << coord.x << ", " << coord.y << ", " << coord.z << ")"; return output; }``````

 ``1234567891011121314151617181920212223242526272829`` ``````#include "Coordinate.h" #include "Ship.h" #include using namespace std; bool detectCollisions(Ship ships[], int number_of_ships); int main() { Ship ships[4] = { Ship(Coordinate(0,0,0), 1, 1, 1), Ship(Coordinate(5,5,5), 0, 0, 0), Ship(Coordinate(8,8,8), -1, -1, -1), Ship(Coordinate(100,100,100), 0,0,0) }; // code omitted to save room bool detect_collisions(Ship ships[], int number_of_ships) { for (int i = 0; i < number_of_ships; ++i) { for (int j = 0; j < number_of_ships; ++j) { cout << ships[0].get_location(); } } }``````

(so much for keeping it short)

I realize that the detect_collisions in pa10.cpp isn't really doing anything but I'm not to that point yet. The problem lies here though when trying to use any of the overloaded operators in the Coordinate class. Trying to print out the ship's Coordinate throws an error.

pa10.cpp:49: error: no match for ‘operator<<’ in ‘std::cout << ships[0].Ship::get_location()'

Am I missing something? Why isn't it using the overloaded operator in the Coordinate class to print out the location? I wouldn't figure that I would have to make a friend function or a function in the Ship class. Another thing I don't understand is that I can print out Coordinate objects just fine if I throw something like "cout << coord" in Ship.cpp.

There are some parts where I need to compare the two Coordinates, which I have defined in the Coordinate class but again, it gives me an issue.

Any ideas?
Last edited on
closed account (D80DSL3A)
Here operator<< is declared a private method:
 ``12345678`` ``````class Coordinate { friend ostream &operator<< (ostream&, Coordinate&);// will be a private method private: double x, y, z; public: Coordinate (double x = 0.0, double y = 0.0, double z = 0.0); void set_coordinate (double, double, double);``````

Maybe move that declaration down a few lines.
I've tried that, it gives me the same errors. I tried it again just know to make sure I wasn't crazy, but yeah... Same problems.
> Here operator<< is declared a private method:
No. It's a global function, you are just saying that that functions is a friend.

@OP:
You are trying to pass a temporary by non-const reference. You can't do that.
Either pass it by value or by const reference

However, you need to observe const correctness.
By instance
 ``12345678910`` ``````class Ship { public: Ship (Coordinate, double, double, double); Coordinate get_location () const;//you will not modify *this void update_location (); private: Coordinate coord; double x_inertia, y_inertia, z_inertia; };``````
We haven't covered this in class so pardon my confusion.

By adding the const at the end, I'm saying that the Coordinate I'm returning is a const? And without specifying the const at the end of the function means the Coordinate that I am returning is passed by value? I've only heard of these terms when objects or primitives are used as arguments. So I'm still a little unclear as to what you mean.

I did put a const at the end of the get_location function in both the header and cpp file, but it didn't change anything. I'm still getting the same error.
The get_location const qualifier doesn't have anything to do with the issue you're experiencing (make no mistake though, it should be const.)

The const correctness of this function, on the other hand:

`ostream &operator<< (ostream&, Coordinate&);`

is an issue.

If you make it: `ostream&operator<<(ostream&, const Coordinate&);` your problem with binding a temporary to a non-const reference will disappear. Don't forget to update all relevant code (such as the definition and any forward declarations.)
 ``12`` ``````Coordinate Ship::get_location () const; //C++ Coordinate Ship_get_location (const Ship *this); //equivalent C ``````
It simply says, `this method will not modify the state of the object, so allow it to be executed in constant objects'
Kind of wishing it was something more difficult than that.

I added in the const into the ostream function and it worked first time. Thanks for the help. Apparently I need to go back and cover const and when to use them.

Thanks again!
UCD should really get a mascot, lol. Have you tried running in Visual Studio vs. g++ to see what happens? I had almost the same error you are having on PA09 when I ran it in g++, but my code ran fine and compiled perfectly when I ran it in Visual Studio.
Simply because VS has an extension that allows that.
But according to the standard is incorrect.
 Have you tried running in Visual Studio vs. g++ to see what happens?

The code wouldn't compile in VC++.

 Simply because VS has an extension that allows that.

No, it doesn't.
Topic archived. No new replies allowed.