如何修复下面的C ++代码中的C1202错误?

时间:2019-05-05 15:12:00

标签: c++

我正在用C ++写模板四元矩阵,想用第一行搜索行列式,但是它产生了C1202错误(C1202递归类型或函数依赖上下文太复杂了)。

我不知道我使用什么编译器,但是我在带有VS2017社区的Windows上进行了编译。如果您也向我解释了如何检查它,那将是很好的。

#include <iostream>
#include <array>
#include <MatrixNxN.h>

int main()
{
    MatrixNxN<float, 4> m1(
        { 
            3, -3, -5, 8,
            -3, 2, 4, -6,
            2, -5, -7, 5,
            -4, 3, 5, -6,
        });
    auto res = m1.GetDeterminant();
    std::system("pause");
}
template<typename _Type, size_t _Size>
_Type MatrixNxN<_Type, _Size>::GetDeterminant()
{
    if (_Size == 2)
        return operator[](0) * operator[](3) - operator[](2) * operator[](1);

    _Type result = 0;
    for (size_t i = 0; i < _Size; i++)
    {
        if (i & 0)
            result += operator[](i) * GetMinor(i, 0).GetDeterminant();
        else
            result -= operator[](i) * GetMinor(i, 0).GetDeterminant();

    }
    return result;
}

template<typename _Type, size_t _Size>
MatrixNxN<_Type, _Size - 1> MatrixNxN<_Type, _Size>::GetMinor(size_t row, size_t col) const
{
    size_t index = 0;
    MatrixNxN<_Type, _Size - 1> result;
    for (size_t i = 0; i < _Size; i++)
    {
        if (i == col)
            continue;
        for (size_t j = 0; j < _Size; j++)
        {
            if (j == row)
                continue;
            result[index++] = GetElement(j, i);
        }
    }
    return result;
}

输出日志:

1>       1>------ Build started: Project: Matricies, Configuration: Debug Win32 ------
1>main.cpp
1>d:\projects\matricies\res\headers\matrixnxn.h(9): warning C4200: nonstandard extension used: zero-sized array in struct/union
1>d:\projects\matricies\res\headers\matrixnxn.h(9): note: This member will be ignored by a defaulted constructor or copy/move assignment operator
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to class template instantiation 'MatrixNxN<_Type,0>' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(181): note: while compiling class template member function '_Type MatrixNxN<_Type,1>::GetDeterminant(void)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation '_Type MatrixNxN<_Type,1>::GetDeterminant(void)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to class template instantiation 'MatrixNxN<_Type,1>' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(181): note: while compiling class template member function '_Type MatrixNxN<_Type,2>::GetDeterminant(void)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation '_Type MatrixNxN<_Type,2>::GetDeterminant(void)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to class template instantiation 'MatrixNxN<_Type,2>' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(181): note: while compiling class template member function '_Type MatrixNxN<_Type,3>::GetDeterminant(void)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation '_Type MatrixNxN<_Type,3>::GetDeterminant(void)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to class template instantiation 'MatrixNxN<_Type,3>' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(181): note: while compiling class template member function '_Type MatrixNxN<_Type,4>::GetDeterminant(void)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\src\main.cpp(14): note: see reference to function template instantiation '_Type MatrixNxN<_Type,4>::GetDeterminant(void)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\src\main.cpp(13): note: see reference to class template instantiation 'MatrixNxN<float,4>' being compiled
1>d:\projects\matricies\res\headers\matrixnxn.h(9): warning C4307: '*': integral constant overflow
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to class template instantiation 'MatrixNxN<_Type,4294967295>' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(181): note: while compiling class template member function '_Type MatrixNxN<_Type,0>::GetDeterminant(void)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation '_Type MatrixNxN<_Type,0>::GetDeterminant(void)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to class template instantiation 'MatrixNxN<_Type,0>' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(181): note: while compiling class template member function '_Type MatrixNxN<_Type,1>::GetDeterminant(void)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation '_Type MatrixNxN<_Type,1>::GetDeterminant(void)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(17): warning C4307: '*': integral constant overflow
1>d:\projects\matricies\res\headers\matrixnxn.h(48): warning C4307: '*': integral constant overflow
1>d:\projects\matricies\res\headers\matrixnxn.h(47): note: while compiling class template member function 'MatrixNxN<_Type,4294967295>::MatrixNxN(const MatrixNxN<_Type,4294967295> &)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(213): note: see reference to function template instantiation 'MatrixNxN<_Type,4294967295>::MatrixNxN(const MatrixNxN<_Type,4294967295> &)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(199): note: while compiling class template member function 'MatrixNxN<_Type,4294967295> MatrixNxN<_Type,0>::GetMinor(size_t,size_t) const'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation 'MatrixNxN<_Type,4294967295> MatrixNxN<_Type,0>::GetMinor(size_t,size_t) const' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(199): note: while compiling class template member function 'MatrixNxN<_Type,0> MatrixNxN<_Type,1>::GetMinor(size_t,size_t) const'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation 'MatrixNxN<_Type,0> MatrixNxN<_Type,1>::GetMinor(size_t,size_t) const' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(199): note: while compiling class template member function 'MatrixNxN<_Type,1> MatrixNxN<_Type,2>::GetMinor(size_t,size_t) const'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation 'MatrixNxN<_Type,1> MatrixNxN<_Type,2>::GetMinor(size_t,size_t) const' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(199): note: while compiling class template member function 'MatrixNxN<_Type,2> MatrixNxN<_Type,3>::GetMinor(size_t,size_t) const'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation 'MatrixNxN<_Type,2> MatrixNxN<_Type,3>::GetMinor(size_t,size_t) const' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(199): note: while compiling class template member function 'MatrixNxN<_Type,3> MatrixNxN<_Type,4>::GetMinor(size_t,size_t) const'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(189): note: see reference to function template instantiation 'MatrixNxN<_Type,3> MatrixNxN<_Type,4>::GetMinor(size_t,size_t) const' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(54): note: while compiling class template member function 'MatrixNxN<float,4>::MatrixNxN(std::initializer_list<_Type>)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\src\main.cpp(7): note: see reference to function template instantiation 'MatrixNxN<float,4>::MatrixNxN(std::initializer_list<_Type>)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(41): warning C4307: '*': integral constant overflow
1>d:\projects\matricies\res\headers\matrixnxn.h(40): note: while compiling class template member function 'MatrixNxN<_Type,4294967295>::MatrixNxN(void)'
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\headers\matrixnxn.h(201): note: see reference to function template instantiation 'MatrixNxN<_Type,4294967295>::MatrixNxN(void)' being compiled
1>        with
1>        [
1>            _Type=float
1>        ]
1>d:\projects\matricies\res\src\main.cpp(17): fatal error C1202: recursive type or function dependency context too complex
1>MatrixNxN.cpp
1>Generating Code...
1>Done building project "Matricies.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I expected this code will work correct, but something go wrong for now :C

1 个答案:

答案 0 :(得分:0)

非常感谢所有帮助我了解该问题的人!

所有问题都在下一行

undefined

该条件仅在运行时检查。实例化发生在编译时。如果您的编译器支持C ++ 17,则可以将if更改为if constexpr(并将其余函数放在else中),在编译时对其进行评估。 (c)来自stackOverflow的聪明人