Templates &引用;没有匹配的函数调用“;模板内函数
在尝试编写模板函数时,我不断遇到以下错误:Templates &引用;没有匹配的函数调用“;模板内函数,templates,c++11,function-templates,Templates,C++11,Function Templates,在尝试编写模板函数时,我不断遇到以下错误: main.cpp|17|error: no matching function for call to ‘dotproduct(vector<float, 3u>&, vector<float, 3u>&)’| template<class vector_t> typename vector_t::element_t dotproduct(const vector_t &vector0, c
main.cpp|17|error: no matching function for call to ‘dotproduct(vector<float, 3u>&, vector<float, 3u>&)’|
template<class vector_t>
typename vector_t::element_t dotproduct(const vector_t &vector0, const vector_t &vector1)
{
typename vector_t::element_t result_(0);
for(size_t index_ = 0; index_ < vector_t::rows * vector_t::colums; ++index_){
result_ += vector0[index_] * vector1[index_];
}
return result_;
}
向量:
template<class element_t, size_t size>
class vector
: public matrix<element_t, size, 1>
{
//...
};
模板
类向量
:公共矩阵
{
//...
};
我的职能:
main.cpp|17|error: no matching function for call to ‘dotproduct(vector<float, 3u>&, vector<float, 3u>&)’|
template<class vector_t>
typename vector_t::element_t dotproduct(const vector_t &vector0, const vector_t &vector1)
{
typename vector_t::element_t result_(0);
for(size_t index_ = 0; index_ < vector_t::rows * vector_t::colums; ++index_){
result_ += vector0[index_] * vector1[index_];
}
return result_;
}
模板
typename vector\u t::element\u t点积(常量向量与向量0、常量向量与向量1)
{
类型名向量:元素结果(0);
对于(大小索引=0;索引
致电:
int main(int count, char *arguments[])
{
typedef vector<float, 3> vec3;
vec3 a = {1.0f, 2.0f, 3.0f}, b = {3.0f, 2.0f, 1.0f};
std::cout << dotproduct(a, b) << std::endl;
std::cin.get();
}
int main(int count,char*参数[])
{
typedef向量vec3;
vec3a={1.0f,2.0f,3.0f},b={3.0f,2.0f,1.0f};
std::cout模板参数的名称只能在模板参数列表所对应的类(或函数)模板内使用。这很有意义-标准不保证模板参数名称;即使在两个声明之间,它们也可能不同
考虑:
template <typename U, typename T>
class A;
template <typename T, typename U>
class A
{ };
int main()
{
A<int, char>::T some_variable; // which type parameter to use?
}
dotproduct()函数似乎不参与重载解析。向量类是否确实有元素?它没有typedef,它是从基类“matrix”派生的非类型模板参数。当我使用typedef G++时,dotproduct()会函数的返回类型确实需要向量类型中的元素类型定义。否则,SFINAE将起作用,并在重载解析期间丢弃该函数。