I'm trying to overload the shriek (!) operator to return the length of a vector like so: (! because it kind of looks like a lowercase "L", and I wanted a unary operator for the sake of brevity and readability.)
// Vector class in Vector.h
class Vector {
public:
double x,y,z;
/* ... */
double operator ! (); /* Vector length */
/* ... */
}
// Function definition in Vector.cpp
double Vector::operator ! (){
return sqrt(x*x+y*y+z*z);
}
// Main function in main.cpp
#include "vector.h"
int main (int argc, char** argv){
Vector a = Vector(1,2,3);
cout << !a << endl;
}
And the error code that I'm getting from gcc is:
$ gcc -lstdc++ main.cpp
Undefined symbols for architecture ********:
"Vector::operator!()", referenced from:
_main in ********.o
ld: symbol(s) not found for architecture ********
collect2: ld returned 1 exit status
(Note: I couldn't find the answer to this question either due to inexperience with C++ or because I'm trying to do something that violates a very basic tenant of the language that I skipped over due to self-education.)