【问题标题】:Specifying some template parameters指定一些模板参数
【发布时间】:2014-11-22 10:45:18
【问题描述】:

我想编写一个自己的 Vector 类和一个计算叉积的函数。

我的向量类有

template<class T, int dim, bool isRowVector>

T 是条目的类型,dim 维度,isRowVector 指定向量是行向量还是列向量。

我重载了Vector的一些操作符,特别是

template <class T, int dim, bool isRowVec>
std::ostream& operator<<(std::ostream& os, Vector<T, dim, isRowVec>& vec) // <<
{
    os << "[" << vec[0];

    for(typename std::vector<T>::iterator it = vec.begin()+1; it != vec.end(); it++) {
        os << "," << *it;
    }

    os << "]";

    if(!isRowVec) { os << "^T"; }

    return os;
}

现在函数crossProduct应该只适用于dim = 3的向量。所以我指定了这样的函数:

template <class T, bool isRowVec>
Vector<T, 3, isRowVec> crossProduct (Vector<T, 3, isRowVec> a, Vector<T, 3, isRowVec> b) {
    T arr[3] = { a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0] };
    Vector<T, 3, isRowVec> newVec(arr);
    return newVec;
}

省略函数模板中的 dim 参数,因为不必猜测或陈述。

这似乎不是问题,因为

Vector<float,3,true> a = crossProduct(f,g);

不会产生编译错误,其中 f 和 g 都是

Vector<float,3,true>

但是当我尝试打电话时

std::cout << crossProduct(f, g) << std::endl;

我明白了

error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Vector<float, 3, true>')
  std::cout << crossProduct(f, g) << std::endl;
            ^

有什么想法吗?

【问题讨论】:

    标签: c++ templates vector


    【解决方案1】:
    std::cout << crossProduct(f, g) << std::endl;
                 |
                 |
                 +------Creates a temporary object (rvalue)
    

    右值不能绑定到非常量引用,所以请改用const 引用

    template <class T, int dim, bool isRowVec>
    std::ostream& operator<<(std::ostream& os, const Vector<T, dim, isRowVec>& vec) 
    //                                         ~~~~~
    {
       // ....
    }
    

    【讨论】:

      【解决方案2】:

      在您的operator &lt;&lt; 中,您需要const &amp; vec 而不是&amp; vec

      您不能将临时值传递给非常量引用,因此您不能传递crossProduct 调用的结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-01
        • 2020-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多