加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

解决在C中调用具有相同名称的函数的方法中的冲突

发布时间:2020-12-16 09:53:56 所属栏目:百科 来源:网络整理
导读:考虑以下用于方形矩阵的(模板化)C结构的精简示例(不需要模拟问题发生): #include array#include complexusing namespace std;double conj (double x) { return x; };template typename T,int Nstruct matrix: arrayT,N*N { void conj() { for (int ij=0; ij
考虑以下用于方形矩阵的(模板化)C结构的精简示例(不需要模拟问题发生):

#include <array>
#include <complex>
using namespace std;

double conj (double &x) { return x; };

template <typename T,int N>
struct matrix: array<T,N*N> {
    void conj() {
        for (int ij=0; ij<100; ++ij) {
            T z = (*this)[ij];
            (*this)[ij] = conj(z);
        }
    }
};

int main() {
    matrix<double,10> a;
    a.conj();
    return 0;
}

我想实现一个执行矩阵复共轭的方法,使用名称.conj()与<中使用的命名系统一致.复杂>图书馆.但是,我收到以下错误:

$g++ example.cpp -std=c++11
example.cpp: In instantiation of ‘void matrix<T,N>::conj() [with T = double; int N = 10]’:
example.cpp:19:12:   required from here
example.cpp:12:26: error: no matching function for call to ‘matrix<double,10>::conj(double&)’
      (*this)[ij] = conj(z);
                          ^
example.cpp:12:26: note: candidate is:
example.cpp:9:10: note: void matrix<T,N>::conj() [with T = double; int N = 10]
     void conj() {
          ^
example.cpp:9:10: note:   candidate expects 0 arguments,1 provided

编译器似乎没有识别在同名方法内部调用的函数conj(double&),并在结构之前定义.相反,它试图调用方法conj().

为什么编译器无法解决此命名冲突,以及保留命名的解决方案是什么?当然,如果我将方法的名称更改为与conj不同的名称,则代码将编译并正常运行.

解决方法

成员函数在更广泛的范围内隐藏同名函数.

使用限定名称:: conj来引用全局命名空间中的函数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读