Xcode实例化后的显式专业化

时间:2018-08-15 06:28:52

标签: c++ xcode

我有一些这样的c ++代码

头文件a.h

#ifndef A_H
#define A_H
template <class Real>
class GridGraph2
        {
                protected:
                class Vertex
                {
                    void SetWeight (int iDX, int iDY, Real fWeight)
                    {
                        m_afWeight[GridGraph2<Real>::ms_aaiIndex[iDY+1][iDX+1]] = fWeight;
                    }

                    Real GetWeight (int iDX, int iDY) const
                    {
                        return m_afWeight[GridGraph2<Real>::ms_aaiIndex[iDY+1][iDX+1]];
                    }
                    private:
                    Real m_afWeight[8];
                };
                friend class Vertex;
                static const int ms_aaiIndex[3][3];  // index[dy][dx]
        };
typedef GridGraph2<float> GridGraph2f;
#endif

代码文件a.cpp

#include "a.h"
template
class GridGraph2<float>;

template<>
const int GridGraph2<float>::ms_aaiIndex[3][3] =
        {
                {0,1,2}, {3,-1,4}, {5,6,7}
        };

但是,当我尝试构建clion时,出现一些错误: error: explicit specialization of 'ms_aaiIndex' after instantiation const int GridGraph2<float>::ms_aaiIndex[3][3] = implicit instantiation first required here m_afWeight[GridGraph2<Real>::ms_aaiIndex[iDY+1][iDX+1]] = fWeight;

我想知道应该在哪里修改代码才能成功构建,以及为什么编译器在实例化后说ms_aaiIndex显式专门化。

我使用xcode命令工具来构建它。但是当我使用mingw在Windows中构建它时没有错误。

1 个答案:

答案 0 :(得分:0)

#include "a.h"
/*
 * remove this
 *
template
class GridGraph2<float>;
 *
 */

template<>
const int GridGraph2<float>::ms_aaiIndex[3][3] =
        {
                {0,1,2}, {3,-1,4}, {5,6,7}
        };