STL と名前空間


#include <iostream>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cassert>
#include "Vec.h"  // 研究室メンバ共用のライブラリのヘッダファイル

double  EPS = 1.0e-10;  // Vec.h 内で使用する定数
double  DET_EPS = EPS;  // 同上

// 比較演算子 (<)
// (数学的な大小を定義しているわけではない.あくまで <algorithm> 用.)
bool  operator < (const Lab::VecC& lhs, const Lab::VecC& rhs)
{
    assert(lhs.size() == rhs.size());

    if (lhs == rhs)
        return false;

    for (int i = 0; i < lhs.size(); i++)
        if (lhs[i] < rhs[i])
            return true;

    return false;
}

int  main(void)
{
    const Lab::VecC  vectors1[] = {
        Lab::VecC(1,2,3), Lab::VecC(4,5,6), Lab::VecC(7,8,9)
    };
    const Lab::VecC  vectors2[] = {
        Lab::VecC(2,1,3), Lab::VecC(5,6,4), Lab::VecC(8,9,7)
    };

    std::set_union(vectors1, vectors1 + 3, vectors2, vectors2 + 3,
        std::ostream_iterator<Lab::VecC>(std::cout, "  ");

    return EXIT_SUCCESS;
}
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_algo.h: In function '_OutputIterator std::set_union(_InputIterator1, _InputIterator1, _InputIterator2, _InputIterator2, _OutputIterator) [with _InputIterator1 = const Lab::VecC*, _InputIterator2 = const Lab::VecC*, _OutputIterator = std::ostream_iterator<Lab::VecC, char, std::char_traits<char> >]':
main.cc:40:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_algo.h:4248: error: no match for 'operator<' in '* __first1 < * __first2'
main.cc:40:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_algo.h:4253: error: no match for 'operator<' in '* __first2 < * __first1'

何でやねん ! ちゃんと bool operator < (const Lab::VecC&, const Lab::VecC&) を定義しとるやんけ ! としばらく頭を抱えてた.

試行錯誤する中で,何とはなしに次のように書き換えた.


namespace Lab
{
    bool  operator < (const VecC& lhs, const VecC& rhs)
    {
        // 同上
    }
}

そしたらコンパイルできた.終わってみれば たったこれだけの事で一体何時間費やしてしまったんだ….疲れた….