samedi 26 septembre 2020

Linker failing to find overloaded operator

I'm trying to overload the << operator for one of my classes, but the linker keeps failing to find the overload. Been searching online for anything I've missed on how to declare and implement the operator overload but nothing seems to stand out to me. Any ideas how I can fix this?

Undefined symbols for architecture x86_64:
  "memath::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, memath::Vector3 const&)", referenced from:
      _main in mecli.cxx.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

vector3.h

#include <string>
#include <iostream>

namespace memath {

class Vector3 {

public:
    double x;
    double y;
    double z;

    Vector3();

    std::string to_string() const;

    friend std::ostream& operator<<(std::ostream &strm, const Vector3 &a);

};

};

vector3.cxx

#include <string>
#include <iostream>
#include <sstream>

#include "vector3.h"

using namespace memath;

Vector3::Vector3() : Vector3(0, 0, 0) {}

std::string Vector3::to_string() const {
    std::ostringstream r;
    r << "Vector3" << "(" << this->x << "," << this->y << "," << this->z << ")";
    return r.str();
}

std::ostream& operator<<(std::ostream &strm, const Vector3 &a) {
    strm << a.to_string();
    return strm;
}

mecli.cxx

#include <iostream>
#include <cstdlib>
#include <string>

#include "vector3.h"

int main(int argc, char** argv) {
    memath::Vector3 vec1;
    std::cout << vec1 << std::endl;
}

Aucun commentaire:

Enregistrer un commentaire