gpt4 book ai didi

c++ - 矩阵加法 : Ambiguous overload for ‘operator+’

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:25:06 24 4
gpt4 key购买 nike

我有一个矩阵类,我希望能够:

1) 添加 2 个矩阵,C = A + B;

2) 添加一个矩阵和一个标量,C = a + B;C = A + b;

我收到一条警告/错误(取决于编译器标志)说函数调用不明确:

g++ test.cpp -std=c++11

test.cpp: In function ‘int main()’:
test.cpp:111:16: error: ambiguous overload for ‘operator+’ (operand types are ‘matrix<double>’ and ‘matrix<int>’)
auto C = A + B;
^
test.cpp:111:16: note: candidates are:
test.cpp:56:55: note: matrix<decltype ((declval<T>() + declval<U>()))> matrix<T>::operator+(const matrix<U>&) const [with U = int; T = double; decltype ((declval<T>() + declval<U>())) = double]
matrix<decltype(std::declval<T>()+std::declval<U>())> matrix<T>::operator+(const matrix<U> &B) const
^
test.cpp:76:57: note: matrix<decltype ((declval<V>() + declval<U>()))> operator+(const U&, const matrix<V>&) [with V = int; U = matrix<double>; decltype ((declval<V>() + declval<U>())) = matrix<double>]
matrix<decltype(std::declval<V>() + std::declval<U>())> operator+(const U &a, const matrix<V> &B)
^
test.cpp:90:57: note: matrix<decltype ((declval<V>() + declval<U>()))> operator+(const matrix<U>&, const V&) [with V = matrix<int>; U = double; decltype ((declval<V>() + declval<U>())) = matrix<double>]
matrix<decltype(std::declval<V>() + std::declval<U>())> operator+(const matrix<U> &A, const V &b)

下面是一个MWE:

#include <iostream>
#include <vector>
#include <array>

template<class T>
class matrix
{
public:
matrix();
matrix(const size_t &d1, const size_t &d2);
T& operator()(const size_t &i, const size_t &j);
T operator()(const size_t &i, const size_t &j) const;

template<class U>
matrix<decltype(std::declval<T>()+std::declval<U>())> operator+(const matrix<U> &B) const;

size_t size(const size_t &n) const;

private:
std::vector<T> mData;
std::array<size_t,2> mSize;
};

template<class T>
matrix<T>::matrix() : mData()
{
for(size_t ii = 0; ii < 2; ii++)
{
mSize[ii] = 0;
}
}

template<class T>
matrix<T>::matrix(const size_t & d1, const size_t & d2) : mData()
{
mSize[0] = d1;
mSize[1] = d2;

mData.resize(d1*d2);
}

template<class T>
T& matrix<T>::operator()(const size_t &i, const size_t &j)
{
return mData[j*mSize[0] + i];
}

template<class T>
T matrix<T>::operator()(const size_t &i, const size_t &j) const
{
return mData[j*mSize[0] + i];
}

template<class T>
template<class U>
matrix<decltype(std::declval<T>()+std::declval<U>())> matrix<T>::operator+(const matrix<U> &B) const
{
matrix<decltype(std::declval<T>()+std::declval<U>())> C(mSize[0],mSize[1]);
for(size_t ii = 0; ii < mSize[0]; ii++)
{
for(size_t jj = 0; jj < mSize[1]; jj++)
{
C(ii,jj) = this->mData[jj*mSize[0] + ii] + B(ii,jj);
}
}
return C;
}

template<class T>
size_t matrix<T>::size(const size_t &n) const
{
return mSize[n-1];
}

template<class V, class U>
matrix<decltype(std::declval<V>() + std::declval<U>())> operator+(const U &a, const matrix<V> &B)
{
matrix<decltype(std::declval<V>() + std::declval<U>())> C(B.size(1),B.size(2));
for(size_t ii = 0; ii < B.size(1); ii++)
{
for(size_t jj = 0; jj < B.size(2); jj++)
{
C(ii,jj) = a + B(ii,jj);
}
}
return C;
}

template<class V, class U>
matrix<decltype(std::declval<V>() + std::declval<U>())> operator+(const matrix<U> &A, const V &b)
{
matrix<decltype(std::declval<V>() + std::declval<U>())> C(A.size(1),A.size(2));
for(size_t ii = 0; ii < A.size(1); ii++)
{
for(size_t jj = 0; jj < A.size(2); jj++)
{
C(ii,jj) = A(ii,jj) + b;
}
}
return C;
}

int main()
{
matrix<double> A(3,3);
A(0,0) = 1.5;

matrix<int> B(3,3);
B(0,0) = 1;

auto C = A + B;

std::cout << C(0,0) << std::endl;
}

问题:为什么我会收到此警告/错误(取决于编译器标志)?如何在不更改所需界面的情况下解决此问题?

最佳答案

好像是成员函数

template<class U>
matrix<decltype(std::declval<T>()+std::declval<U>())> operator+(const matrix<U> &B) const;

与函数是多余的

template<class V, class U>
matrix<decltype(std::declval<V>() + std::declval<U>())> operator+(const matrix<U> &A, const matrix<V> &B)

删除其中一个。

关于c++ - 矩阵加法 : Ambiguous overload for ‘operator+’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20573853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com