[TriLUG] OT: C++ Question

Warren Myers volcimaster at gmail.com
Tue Jan 31 11:02:04 EST 2006


I personally prefer #ifndef .. #endif clauses for making sure they're only
defined once.

On 1/30/06, Randy Barlow <rpbarlow at ncsu.edu> wrote:
>
> Sorry for the OT post, but this problem is driving me nuts!  (If anyone
> knows of a *good* mailing list that this question would be more
> appropriate for, please let me know!)
>
> The problem: I'm getting a compiler error for the following class
> definition:
>
> #pragma once
> #include "vector3d.h"
>
> class point3d {
>     public:
>         double x, y, z;
>         point3d(double, double, double);
>         point3d(void);
>         ~point3d(void);
>         vector3d operator+ (vector3d);
>         point3d operator+ (vector3d);
>         vector3d operator- (point3d);
>         double* operator [] (const int &);
> };


If I recall correctly, since + is a binary operator, you need to have both
arguments in the function definition:
  point3d operator+ (point3d &lhs, &vector3d &rhs);
The minus sign (-) can be either unary or binary, so you need to make sure
that if you're trying to do a subtraction you have both arguments in the
function definition.

The compiler doesn't like the operator overloading statements,
> complaining about syntax, and that it doesn't know what the vector3d
> identifier is.  However, I've included the vector3d.h file where this
> type (another similar class) is defined.  Am I doing something
> incorrectly here?  Thanks!


If the classes reference each other, they eed to be in the same header file.
Though I would  ask how you plan to add a vector to a point and get a point
back... so it might make the most sense to put all of the vector operators
in the vector class, and the point-only operators in the point class.

--
> Randy Barlow
> Research Assistant
> Department of Electrical and Computer Engineering
> North Carolina State University, Raleigh, NC
> rpbarlow at ncsu.edu
> http://www.electronsweatshop.com
>
> --
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ  : http://trilug.org/faq/
> TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
>



--
http://warrenmyers.com
"God may not play dice with the universe, but something strange is going on
with the prime numbers." --Paul Erdős
"It's not possible. We are the type of people who have everything in our
favor going against us." --Ben Jarhvi, Short Circuit 2


More information about the TriLUG mailing list