Member function not declared when using templates in classes (C++) - c++

I am using the following classes in my code
class pos2d
{
float x, y;
public:
float getx() const;
float gety() const;
//int getz() const; //How to avoid??
};
class pos3d
{
float x, y, z;
public:
float getx() const;
float gety() const;
float getz() const;
};
template<class T, class P>
class entity
{
T type;
P position;
public:
void print() const;
};
My print function in class entity is as follows
template<class T, class P>
void entity<T, P>::print() const
{
if (type == 1 || type == 'A')
cout << "Object is of type " << type << " and has coordinates as (" << position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n"; //Avoid getz in pos2d
else if (type == 2 || type == 'B')
cout << "Object is of type " << type << " and has coordinates as (" << position.getx() << ", " << position.gety() << ")\n\n";
}
Note, Type value changes depending of whether class is pos2d or pos2d.
During compilation I get the following error:
Error C2039 'getz': is not a member of 'pos2d' Project1
I am aware that using a common get() function would solve this but i wish to be able to use getz() in my code without having it as part of another common function.

The problem is if you pass pos2d as template parameter the line
cout << "Object is of type " << type << " and has coordinates as (" << position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n";
is still instantiated (if is done at runtime), the function cannot be resolved.
The usual way is to use specializations (Type parameter can be omitted):
template<class P>
class entity
{
P position;
public:
void print() const;
};
template<>
void entity<pos3d>::print() const
{
cout << "Object is of type pos3d and has coordinates as ("
<< position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n";
}
template<>
void entity<pos2d>::print() const
{
cout << "Object is of type pos2d and has coordinates as ("
<< position.getx() << ", " << position.gety() << ")\n\n";
}

I see 2 simple ways to implement this:
- Make print() a member function of posX class.
- Specialize print() function in entity class.
#include <iostream>
class pos2d
{
float x, y;
public:
float getx() const { return x; }
float gety() const { return y; }
void print() const
{
std::cout << "Object is of type and has coordinates as (" << getx() << ", " << gety() << ")\n\n";
}
};
class pos3d
{
float x, y, z;
public:
float getx() const { return x; }
float gety() const { return y; }
float getz() const { return z; }
void print() const
{
std::cout << "Object is of type and has coordinates as (" << getx() << ", " << gety() << ", " << getz() << ")\n\n";
}
};
template <class P>
class entity
{
P position;
public:
void print() const { position.print(); }
void print_specialized() const;
};
template <>
void entity<pos3d>::print_specialized() const {
std::cout << "Object is of type and has coordinates as (" << position.getx() << ", " << position.gety() << ", " << position.getz() << ")\n\n";
}
template <>
void entity<pos2d>::print_specialized() const {
std::cout << "Object is of type and has coordinates as (" << position.getx() << ", " << position.gety() << ")\n\n";
}
int main() {
entity<pos2d> e;
e.print();
e.print_specialized();
entity<pos3d> e2;
e2.print();
e2.print_specialized();
return 0;
}

Related

Proper handling of member function pointers

I wrote a generic class for handling and executing a function pointer. This is a simplified equivalent of std::function and std::bind. To handle member functions I use cast to internal EventHandler::Class type. Question: is it ok to cast it that way? Will it work in all cases when invoking handled function?
template <typename ReturnType, typename... Arguments>
class EventHandler
{
class Class {};
ReturnType (Class::*memberFunction)(Arguments...) = nullptr;
union {
Class *owner;
ReturnType(*function)(Arguments...) = nullptr;
};
public:
EventHandler() = default;
EventHandler(EventHandler &&) = default;
EventHandler(const EventHandler &) = default;
EventHandler &operator=(EventHandler &&) = default;
EventHandler &operator=(const EventHandler &) = default;
EventHandler(ReturnType (*function)(Arguments...)) :
function(function)
{
}
template <typename Owner>
EventHandler(Owner *owner, ReturnType (Owner::*memberFunction)(Arguments...)) :
memberFunction((ReturnType (Class::*)(Arguments...)) memberFunction),
owner((Class *) owner)
{
}
template <typename Owner>
EventHandler(const Owner *owner, ReturnType (Owner::*memberFunction)(Arguments...) const) :
memberFunction((ReturnType (Class::*)(Arguments...)) memberFunction),
owner((Class *) owner)
{
}
ReturnType operator()(Arguments... arguments)
{
return memberFunction ?
(owner ? (owner->*memberFunction)(arguments...) : ReturnType()) :
(function ? function(arguments...) : ReturnType());
}
};
The implementation provides handle for a global function, a member function and a const member function. Obviously there is volatile and const volatile that is not show here for clarity.
EDIT
All the code below is just a representation of all of kinds of supported functions.
class Object
{
public:
double y = 1000;
Object() = default;
Object(double y) : y(y) {}
static void s1(void) { std::cout << "s1()" << std::endl; }
static void s2(int a) { std::cout << "s2(a:" << 10 + a << ")" << std::endl; }
static void s3(int a, float b) { std::cout << "s3(a:" << 10 + a << ", b:" << 10 + b << ")" << std::endl; }
static int s4(void) { std::cout << "s4(): "; return 10 + 4; }
static Object s5(int a) { std::cout << "s5(a:" << 10 + a << "): "; return Object(10 + 5.1); }
static float s6(int a, Object b) { std::cout << "s6(a:" << 10 + a << ", b:" << 10 + b.y << "); "; return 10 + 6.2f; }
void m1(void) { std::cout << "m1()" << std::endl; }
void m2(int a) { std::cout << "m2(a:" << y + a << ")" << std::endl; }
void m3(int a, float b) { std::cout << "m3(a:" << y + a << ", b:" << y + b << ")" << std::endl; }
int m4(void) { std::cout << "m4(): "; return ((int) y) + 4; }
Object m5(int a) { std::cout << "m5(a:" << y + a << "): "; return Object(y + 5.1); }
float m6(int a, Object b) { std::cout << "m6(a:" << y + a << ", b:" << y + b.y << "); "; return ((int) y) + 6.2f; }
void c1(void) const { std::cout << "c1()" << std::endl; }
void c2(int a) const { std::cout << "c2(a:" << y + a << ")" << std::endl; }
void c3(int a, float b) const { std::cout << "c3(a:" << y + a << ", b:" << y + b << ")" << std::endl; }
int c4(void) const { std::cout << "c4(): "; return ((int) y) + 4; }
Object c5(int a) const { std::cout << "c5(a:" << y + a << "): "; return Object(y + 5.1); }
float c6(int a, Object b) const { std::cout << "c6(a:" << y + a << ", b:" << y + b.y << "); "; return ((int) y) + 6.2f; }
};
void f1(void) { std::cout << "f1()" << std::endl; }
void f2(int a) { std::cout << "f2(a:" << a << ")" << std::endl; }
void f3(int a, float b) { std::cout << "f3(a:" << a << ", b:" << b << ")" << std::endl; }
int f4(void) { std::cout << "f4(): "; return 4; }
Object f5(int a) { std::cout << "f5(a:" << a << "): "; return Object(5.1); }
float f6(int a, Object b) { std::cout << "f6(a:" << a << ", b:" << b.y << "); "; return 6.2f; }
Here is the usage example for all of the above functions
int main()
{
std::cout << "=== Global functions" << std::endl;
EventHandler ef1(f1); ef1();
EventHandler ef2(f2); ef2(2);
EventHandler ef3(f3); ef3(3, 3.1f);
EventHandler ef4(f4); std::cout << ef4() << std::endl;
EventHandler ef5(f5); std::cout << ef5(5).y << std::endl;
EventHandler ef6(f6); std::cout << ef6(6, Object(6.1)) << std::endl;
std::cout << std::endl;
std::cout << "=== Member static functions" << std::endl;
EventHandler es1(Object::s1); es1();
EventHandler es2(Object::s2); es2(2);
EventHandler es3(Object::s3); es3(3, 3.1f);
EventHandler es4(Object::s4); std::cout << es4() << std::endl;
EventHandler es5(Object::s5); std::cout << es5(5).y << std::endl;
EventHandler es6(Object::s6); std::cout << es6(6, Object(6.1)) << std::endl;
std::cout << std::endl;
std::cout << "=== Member functions" << std::endl;
Object object(20);
EventHandler em1(&object, &Object::m1); em1();
EventHandler em2(&object, &Object::m2); em2(2);
EventHandler em3(&object, &Object::m3); em3(3, 3.1f);
EventHandler em4(&object, &Object::m4); std::cout << em4() << std::endl;
EventHandler em5(&object, &Object::m5); std::cout << em5(5).y << std::endl;
EventHandler em6(&object, &Object::m6); std::cout << em6(6, Object(6.1)) << std::endl;
std::cout << std::endl;
std::cout << "=== Member const functions" << std::endl;
const Object constObject(30);
EventHandler ec1(&constObject, &Object::c1); ec1();
EventHandler ec2(&constObject, &Object::c2); ec2(2);
EventHandler ec3(&constObject, &Object::c3); ec3(3, 3.1f);
EventHandler ec4(&constObject, &Object::c4); std::cout << ec4() << std::endl;
EventHandler ec5(&constObject, &Object::c5); std::cout << ec5(5).y << std::endl;
EventHandler ec6(&constObject, &Object::c6); std::cout << ec6(6, Object(6.1)) << std::endl;
system("pause");
return 0;
}
Finally - to the point - here an example that shows how much easier in use is the EventHandler I prepared when compared to std::function interface. And actually the reason of such approach.
EventHandler<float, int, Object> example;
example = f6;
example(7, Object(7.1));
example = EventHandler(&object, &Object::m6);;
example(8, Object(8.1));
It’s undefined behavior to call a function through a function pointer(-to-member) of a different type. (Some practical reasons for this rule are that the object’s address might need to be adjusted to call a member function of a base class or that a vtable might be involved.) You can use type erasure to allow calling member functions on objects of different types (which is what std::bind does), or you can (restrict to member functions and) add the class type as a template parameter.
Of course, the usual answer is to just use std::function with a lambda that captures the object in question and calls whatever member function. You can also take the C approach and define various functions with a void* parameter that cast that parameter to a known class type and call the desired member function.

A "pointer" to any type of function

is there any mechanism with elegant API to handle functions of any type?
I mean a class that automagically detects type of a function (its return type, arguments, if it is a class member, a const etc), something that I could easily use to handle any kind of events, like in the example below:
class Abc
{
public:
void aFunc() { std::cout << "a()" << std::endl; }
void cFunc(int x, char y) { std::cout << "c(" << x << ", " << y << ")" << std::endl; }
};
void bFunc(int x) { std::cout << "b(" << x << ")" << std::endl; }
int main()
{
Abc abc;
EventHandler a = abc.aFunc;
EventHandler b = bFunc;
EventHandler c = abc::cFunc;
a();
b(123);
c(456789, 'f');
std::cout << "Done." << std::endl;
return 0;
}
The std::function and std::bind can be used internally, but the bind should be done automatically.

Try to pass an object to a same template class to copy

I work actually to understand the concept of template and implement a simple one. I manage to almost cases execpt one to code a constructor to copy an instance of same class.
#include <iostream>
// template <typename T>
template <class T>
class vec2 {
private:
static int instance;
T const x;
T const y;
public:
vec2() : x(0), y(0) {
std::cout << "Default constructor" << std::endl;
vec2<T>::instance++;
return;
}
vec2(T const &x, T const &y) : x(x), y(y) {
std::cout << "Parametric constructor" << std::endl;
vec2<T>::instance++;
return;
}
vec2(vec2<T> const & src) {
*this = src;
std::cout << "Copy constructor" << std::endl;
vec2<T>::instance++;
return;
}
~vec2(){
std::cout << "Destructor" << std::endl;
vec2<T>::instance--;
return;
}
vec2 & operator=(vec2 const & rhs) {
this->x = rhs.get_x();
this->y = rhs.get_y();
return *this;
}
// get
static int get_instance() {
return vec2<T>::instance;
}
T get_x() const {
return this->x;
}
T get_y() const {
return this->y;
}
};
template <class T>
std::ostream & operator<<(std::ostream & out, vec2<T> const & rhs) {
out << "[ " << rhs.get_x() << ", " << rhs.get_y() << " ]";
return out;
}
template <class T>
int vec2<T>::instance = 0;
int main() {
vec2<float> a;
vec2<int> b(21, 42);
vec2<float> c(21.21f, 42.42f);
vec2<bool> d(true, false);
vec2<int> e(b);
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
std::cout << d << std::endl;
std::cout << e << std::endl;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
std::cout << "b.get_x(): " << b.get_x() << std::endl;
std::cout << "b.get_y(): " << b.get_y() << std::endl;
std::cout << "c.get_x(): " << c.get_x() << std::endl;
std::cout << "c.get_y(): " << c.get_y() << std::endl;
std::cout << "d.get_x(): " << d.get_x() << std::endl;
std::cout << "d.get_y(): " << d.get_y() << std::endl;
return (0);
}
here the error message, but I'm not expert te read it, and i don't understand what i must change in my code. So if anybody I have an idea to help a newbee in C++, that's can be awesome.
clang++ -std=c++11 -Wconversion *.cpp && ./a.out
main.cpp:24:2: error: constructor for 'vec2<int>' must explicitly initialize the
const member 'x'
vec2(vec2<T> const & src) {
^
main.cpp:72:12: note: in instantiation of member function 'vec2<int>::vec2'
requested here
vec2<int> e(b);
^
main.cpp:9:10: note: declared here
T const x;
^
main.cpp:24:2: error: constructor for 'vec2<int>' must explicitly initialize the
const member 'y'
vec2(vec2<T> const & src) {
^
main.cpp:10:10: note: declared here
T const y;
^
main.cpp:38:11: error: cannot assign to non-static data member 'x' with
const-qualified type 'const int'
this->x = rhs.get_x();
~~~~~~~ ^
main.cpp:25:9: note: in instantiation of member function 'vec2<int>::operator='
requested here
*this = src;
^
main.cpp:72:12: note: in instantiation of member function 'vec2<int>::vec2'
requested here
vec2<int> e(b);
^
main.cpp:9:10: note: non-static data member 'x' declared const here
T const x;
~~~~~~~~^
main.cpp:39:11: error: cannot assign to non-static data member 'y' with
const-qualified type 'const int'
this->y = rhs.get_y();
~~~~~~~ ^
main.cpp:10:10: note: non-static data member 'y' declared const here
T const y;
~~~~~~~~^
4 errors generated.
As #Sam Varshavchik said in the comments, the problem is that you don't initialize your const members in your copy-constructor. Here is the correct implementation:
vec2(vec2<T> const & src) : x(src.get_x()), y(src.get_y()) { //<-- initialization of const members
std::cout << "Copy constructor" << std::endl;
vec2<T>::instance++;
return;
}
Also, *this = src; does feel all kinds of wrong.
here my original class code where i make a copy with *this = src and the result work great. So the question why that's work with regular class and not with the template ?
Plus I imagine the behavior with the regular class, the adress stay the same.
And the method to init all member can be complicated if the is a lot members variables in the class ?
main.c
#include "Vec2.hpp"
#include <iostream>
int main() {
Vec2 a;
Vec2 b(21,42);
Vec2 c(a);
std::cout << a << std::endl;
std::cout << "c.get_x(): " << c.get_x() << std::endl;
std::cout << "c.get_y(): " << c.get_y() << std::endl;
std::cout << "b.get_x(): " << b.get_x() << std::endl;
std::cout << "b.get_y(): " << b.get_y() << std::endl;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
a = b;
std::cout << "a.get_x(): " << a.get_x() << std::endl;
std::cout << "a.get_y(): " << a.get_y() << std::endl;
return 0;
}
Vec2.cpp
#ifndef VEC2_H
# define VEC2_H
#include <iostream>
class Vec2 {
public:
Vec2(); // canonical
Vec2(float const x, float const y);
Vec2(Vec2 const &); // canonical
~Vec2(); // canonical
Vec2 & operator=(Vec2 const & rhs); // canonical
static int get_instance();
float get_x() const ;
float get_y() const ;
private:
static int instance;
float x;
float y;
};
std::ostream & operator<< (std::ostream & out, Vec2 const & rhs);
#endif
vec2.hpp
#include "Vec2.hpp"
#include <iostream>
Vec2::Vec2() : x(0), y(0) {
std::cout << "Default constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::Vec2(float const x, float const y) : x(x), y(y) {
std::cout << "Parametric constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::Vec2(Vec2 const & src) {
*this = src;
std::cout << "Copy constructor" << std::endl;
Vec2::instance++;
return;
}
Vec2::~Vec2() {
std::cout << "Destructor" << std::endl;
Vec2::instance--;
return;
}
Vec2 & Vec2::operator=(Vec2 const & rhs) {
this->x = rhs.get_x();
this->y = rhs.get_y();
return *this;
}
int Vec2::get_instance(){
return Vec2::instance;
}
float Vec2::get_x() const {
return this->x;
}
float Vec2::get_y() const {
return this->y;
}
std::ostream & operator<< (std::ostream & out, Vec2 const & rhs) {
out << "[ " << rhs.get_x() << ", " << rhs.get_y() << " ]";
return out;
}
int Vec2::instance = 0;

Linking errors 2005, and 1169 when trying to overload the << operator

I get a linking error when i try to compile this code. I need to overload the output operator to display a three dimensional vector class I am not sure where to go from here any help is appreciated.
Vect3D.h
#ifndef VECT3D_H
#define VECT3D_H
#include <iostream>
class Vect3D
{
public:
Vect3D();
Vect3D(double xVal, double yVal, double zVal);
double getX() const { return x; }
double getY() const { return y; }
double getZ() const { return z; }
double magnitude() const { return sqrt(x*x + y*y + z*z); }
friend ostream& operator<<(ostream& os, const Vect3D& out);
void setX(double xVal) { x = xVal; }
void setY(double yVal) { y = yVal; }
void setZ(double zVal) { z = zVal; }
private:
double x;
double y;
double z;
};
ostream& operator<<(ostream& os, const Vect3D& out)
{
os << "(" << out.x << ", " << out.y << ", " << out.z << ")";
return os;
}
#endif
Vect3D.cpp
using namespace std;
#include "Vect3D.h"
Vect3D::Vect3D()
: x(0), y(0), z(0)
{ } // empty body
Vect3D::Vect3D(double xVal, double yVal, double zVal)
: x(xVal), y(yVal), z(zVal)
{ } // empty body
TestCode.cpp
#include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;
#include "Vect3D.h"
int main()
{
Vect3D v;
const Vect3D zero;
Vect3D v1(1, 2, 3), v2(7.5, 8.5, 9.5);
const Vect3D con(4, 5, 6);
cout << "Testing overload of << operator" << endl;
cout << v1;
cout << endl;
cout << "Should be:" << endl;
cout << "(" << v1.getX() << ", " << v1.getY() << ", " << v1.getZ() << ")" << endl << endl;
cout << "Testing chaining of overload of << operator" << endl;
cout << v1 << endl;
cout << "Should be:" << endl;
cout << "(" << v1.getX() << ", " << v1.getY() << ", " << v1.getZ() << ")" << endl << endl;
cout << "Testing overload of << operator for const Vect3D's" << endl;
cout << con << endl;
cout << "Should be:" << endl;
cout << "(" << con.getX() << ", " << con.getY() << ", " << con.getZ() << ")" << endl << endl;
cout << "Testing ostream parameter passing for the << operator" << endl;
stringstream sout;
sout << con;
string s = sout.str();
cout << s << endl;
cout << "Should be: " << endl;
cout << "(4, 5, 6)" << endl << endl;
cout << endl << endl;
return 0;
}
Errors:
Error 1 error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Vect3D const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABVVect3D###Z) already defined in TestCode.obj G:\overloadAssignment\overloadAssignment\Vect3D.obj
Error 2 error LNK1169: one or more multiply defined symbols found G:\overloadAssignment\Debug\overloadAssignment.exe 1
This directive has been used assiduously in other sections of this tutorial. When the preprocessor finds an #include directive it replaces it by the entire content of the specified header or file. See more information here.
So you have ostream& operator<<(ostream& os, const Vect3D& out) definition in Vect3D.h and you include this file in both TestCode.cpp and Vect3D.cpp. Thus you compile this function twice in Vect3D.obj and TestCode.obj. And when you try to link program, linker says that you have multiple definitions, and linker does not know what definition is right.
You need to put your implementation in Vect3D.cpp to compile it just once.
Vect3D.h
#ifndef VECT3D_H
#define VECT3D_H
#include <iostream>
class Vect3D
{
public:
Vect3D();
Vect3D(double xVal, double yVal, double zVal);
double getX() const { return x; }
double getY() const { return y; }
double getZ() const { return z; }
double magnitude() const { return sqrt(x*x + y*y + z*z); }
friend ostream& operator<<(ostream& os, const Vect3D& out);
void setX(double xVal) { x = xVal; }
void setY(double yVal) { y = yVal; }
void setZ(double zVal) { z = zVal; }
private:
double x;
double y;
double z;
};
ostream& operator<<(ostream& os, const Vect3D& out);
#endif
Vect3D.cpp
using namespace std;
#include "Vect3D.h"
Vect3D::Vect3D()
: x(0), y(0), z(0)
{ } // empty body
Vect3D::Vect3D(double xVal, double yVal, double zVal)
: x(xVal), y(yVal), z(zVal)
{ } // empty body
ostream& operator<<(ostream& os, const Vect3D& out)
{
os << "(" << out.x << ", " << out.y << ", " << out.z << ")";
return os;
}

C++: Simple error I cannot spot! C2146

Pretty new to C++, I have been following the intermediate tutorials at 3DBuzz.com, and trying to experiment with their tasks.
Current tutorial is on classes: http://www.3dbuzz.com/vbforum/sv_showvideo.php?v=37
I am trying to overload the &operator << to output my 'Point' as a stream when I want. The relevant part of the video starts at 39:00.
As far as I can tell my code is syntactically identical (though I am new so I'm probably missing something) but I get the error:
1>c:\users\jack\documents\visual studio 2010\projects\myfirstgame\myfirstgame\main.cpp(88): error C2146: syntax error : missing ';' before identifier 'myPoint
I realise that I declare the instance Point &myPoint in the operator overload function.. but I don't know where else I could do it so the compiler knows what it is.. if that makes sense.
Any help is appreciated! Thanks
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(float f_x = 0.0, float f_y = 0.0, float f_z = 0.0);
~Point();
void SetXYZ(float X, float Y, float Z);
void SetX(float X);
void SetY(float Y);
void SetZ(float Z);
void GetXYZ(float &X, float &Y, float &Z);
float GetX();
float GetY();
float GetZ();
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "Constructor with ARGUMENTS!" << endl;
x = f_x;
y = f_y;
z = f_z;
}
void Point::GetXYZ(float &X, float &Y, float &Z)
{
X = GetX();
Y = GetY();
Z = GetZ();
}
float Point::GetX()
{
return x;
}
float Point::GetY()
{
return y;
}
float Point::GetZ()
{
return z;
}
void Point::SetXYZ(float X,float Y, float Z)
{
SetX(X);
SetY(Y);
SetZ(Z);
}
void Point::SetX(float X)
{
x = X;
}
void Point::SetY(float Y)
{
y = Y;
}
void Point::SetZ(float Z)
{
z = Z;
}
Point::~Point()
{
cout << "We're in the destructor" << endl;
}
ostream &operator <<(ostream &stream, Point &myPoint)
{
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
return stream;
}
void main()
{
float x, y, z; //Declaring floats for use in GetXYZ()
Point myLocation (1,2,-1); //Creating instance and using Point(...) function
cout << myLocation.GetX() << myLocation.GetY() << myLocation.GetZ() <<endl; // Getting xyz values and printing
myLocation.SetXYZ(2,3,-4); //Testing SetXYZ function
cout << myLocation.GetX() << myLocation.GetY() << myLocation.GetZ() <<endl; // Getting xyz values and printing
myLocation.GetXYZ(x, y, z);
cout << x << " " << y << " " << z << endl;
cout << myLocation;
system("PAUSE");
}
EDIT: Unbelievable response! Love this site already. Thanks everyone who spotted this ^^
You are missing << in :
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
^^
At line 88 you should add << before myPoint.GetZ();
Your code:
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
Correction:
stream << myPoint.GetX() << " " << myPoint.GetY() << " " << myPoint.GetZ();
See the difference in between these two?
stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ();
stream << myPoint.GetX() << " " << myPoint.GetY() << " " << myPoint.GetZ();
Here is the problem: stream << myPoint.GetX() << " " << myPoint.GetY() << " " myPoint.GetZ(); You are missing << between the last " " and the last myPoint.
Here you go:
ostream &operator <<(ostream &stream, Point &myPoint) { stream << myPoint.GetX() << " " << myPoint.GetY() << " "<< myPoint.GetZ(); return stream; }
Note the extra "<<" before myPoint.GetZ();