C++ 为与类中的类在同一命名空间中定义的结构调用重载运算符

C++ 为与类中的类在同一命名空间中定义的结构调用重载运算符,c++,operator-overloading,linker-errors,C++,Operator Overloading,Linker Errors,我在命名空间saw中定义了一个结构coord。在同一名称空间中,我还重载了操作符 这要求coord::toString是一个常量函数,这可能是缺少const的原因:编译了const-更少的版本,愚弄提问者认为它是正确的 那么除了 ostream& operator<<(ostream& os, const coord& c) // added const { os << c.toString(); return os; } 后来在

我在命名空间
saw
中定义了一个结构
coord
。在同一名称空间中,我还重载了
操作符
这要求
coord::toString
是一个常量函数,这可能是缺少
const
的原因:编译了
const
-更少的版本,愚弄提问者认为它是正确的

那么除了

ostream& operator<<(ostream& os, const coord& c) // added const
{
    os << c.toString();
    return os;
}
后来在实施中

string coord::toString() const // added const
{
    ostringstream out;

    out << "[" << x << ", " << y << ", " << z << "]";

    return out.str();
}
string坐标::toString()常量//添加常量
{
ostringstream out;

除其他任何东西外,在C和C++中,如α-SaWyHpppGy等名称都是保留的,不应在自己的代码中创建它们。
// main.cpp    
#include "saw.hpp"

using saw::SAWBuilder;

int main(int argc, char **argv)
{
    SAWBuilder saw;

    int a = saw.buildSAW();

    return a;
}
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
ostream& operator<<(ostream& os, const coord& c);
ostream& operator<<(ostream& os, coord& c)
os << c.toString();
ostream& operator<<(ostream& os, const coord& c) // added const
{
    os << c.toString();
    return os;
}
struct coord
{
    int x;
    int y;
    int z;

    string toString() const; // added const
};
string coord::toString() const // added const
{
    ostringstream out;

    out << "[" << x << ", " << y << ", " << z << "]";

    return out.str();
}