I have a struct coord
defined in the namespace saw
. In the same namespace, I have also overloaded the operator<<
for coord
. In the buildSAW()
method of SAWBuilder
, I successfully create a coord
but when I try to use the overloaded <<
I get Undefined symbols for architecture x86_64:
when I try to compile. The program is able to compile successfully however when I use std::out << coord.toString() << std::endl
. How can I successfully access the overloaded operator<<
for coord
within the method for SAWBuilder
?
// saw.hpp
#ifndef __SAW_HPP__
#define __SAW_HPP__
#include <iostream>
#include <sstream>
#include <string>
using std::string;
using std::ostream;
namespace saw
{
class SAWBuilder
{
public:
SAWBuilder() {}
int buildSAW() const;
static const int SIZE_M = 100;
};
struct coord
{
int x;
int y;
int z;
string toString();
};
ostream& operator<<(ostream& os, const coord& c);
}
#endif
The implementation file:
// saw.cpp
#include "saw.hpp"
#include <iostream>
#include <sstream>
#include <string>
using std::string;
using std::ostringstream;
using std::endl;
namespace saw
{
int SAWBuilder::buildSAW() const
{
int visited[SIZE_M][SIZE_M][SIZE_M] = {}; // initalize to zero
coord starting_coord = coord{SIZE_M/2, SIZE_M/2, SIZE_M/2};
std::cout << visited[0][0][0] << std::endl;
std::cout << starting_coord << std::endl; // <- problem here!
return 0;
}
string coord::toString()
{
ostringstream out;
out << "[" << x << ", " << y << ", " << z << "]";
return out.str();
}
ostream& operator<<(ostream& os, coord& c)
{
os << c.toString();
return os;
}
}
The main file:
// main.cpp
#include "saw.hpp"
using saw::SAWBuilder;
int main(int argc, char **argv)
{
SAWBuilder saw;
int a = saw.buildSAW();
return a;
}
The Makefile
:
CXX = g++
CXXFLAGS = -Wall -pedantic -std=c++17 -g
Saw: main.o saw.o
${CXX} ${CXXFLAGS} -o $@ $^
main.o: saw.hpp
saw.o: saw.hpp