c++ - 将内联函数从 header 移动到源

标签 c++ inline

我是 C++ 的新手,所以我提前为我的问题的普通性质道歉。

有人要求我将以下内联函数从头文件移动到源文件。但是,我正在努力使源文件中的语法正确。 这是头文件中的内联函数:

class G1FRViewOption
 {
    public:
        enum ViewType {partyView, contraView, unknown} ;
        G1FRViewOption() : m_view     (nullString )
                     , m_viewType (unknown    )
                     , m_isValid  (false      ) {initialise();} 
        G1FRViewOption(const RWCString view) : m_view     (view    )
                                         , m_viewType (unknown )
                                         , m_isValid  (false   ) {initialise();}
        void initialise()
        {
            static RWCString views[] = {"party","contra"} ;
            for (int vt=counterpartyView; vt<unknown; vt++)
                if (m_view.compareTo(views[vt], RWCString::ignoreCase)==0)
                {
                    m_viewType  = (ViewType) vt ;
                    m_isValid   = true          ;
                    break ;
                }
        } 
        RWCString getErrorMessage()
        {
            return "Invalid " + getFieldLabel() + " given. Valid values are party, contra.";
        }
        G1FRViewOption & operator = (const G1FRViewOption & other)
        {
            this->m_view     = other.m_view     ;
            this->m_viewType = other.m_viewType ;
            this->m_isValid  = other.m_isValid  ;
            return *this ;
        } 
              RWBoolean   is        (ViewType viewType) const {return viewType == m_viewType ;} 
              RWBoolean   isValid   () const {return m_isValid ;} 
        const RWCString & toString  ()       {return m_view    ;} 
        static const RWCString       & getFieldLabel         () { static RWCString value = "View"         ; return value ; } 
        static const FieldDefinition & getFieldDefinition    () { static const FieldDefinition fd (getFieldLabel(), 13) ; return fd ; } 
    private:
        RWCString m_view     ;
        ViewType  m_viewType ;
        RWBoolean m_isValid  ;} ;

如有任何帮助,我们将不胜感激。

非常感谢。

C++新手!

最佳答案

如果相应的 cpp 文件不存在,则首先创建相应的 cpp 文件,然后将函数的主体一个一个地复制到该文件中,并在它们的名称前加上类和两个冒号 ::,像这样:

void initialise()
{
    static RWCString views[] = {"party","contra"} ;
    for (int vt=counterpartyView; vt<unknown; vt++)
        if (m_view.compareTo(views[vt], RWCString::ignoreCase)==0)
        {
            m_viewType  = (ViewType) vt ;
            m_isValid   = true          ;
            break ;
        }
}

从标题变成

void initialise();

在标题中,和

void G1FRViewOption::initialise()
{
    static RWCString views[] = {"party","contra"} ;
    for (int vt=counterpartyView; vt<unknown; vt++)
        if (m_view.compareTo(views[vt], RWCString::ignoreCase)==0)
        {
            m_viewType  = (ViewType) vt ;
            m_isValid   = true          ;
            break ;
        }
}

在cpp文件中。

关于c++ - 将内联函数从 header 移动到源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12481138/

相关文章:

c++ - 升级到 Mojave : -lstdc++ not found 后 Pybind11 不工作或 C++ 不编译

c++ - 我是否需要在 C++ 线程中使用整数锁定

c++ - 调用 fread() c++ 时出现段错误

CSS 样式冲突

html - 如何正确显示html音频播放器左侧的图标?

c++ - 定义一个调用其他函数的内联函数是否有意义?

c++ - C++ 中的内联函数与普通函数

C++ Builder 10.2.3 [ilink32 错误] 致命 : Unable to open file 'FORMS.OBJ'

c++ - SIMD 代码?

generics - 使包装的 F# SRTP 函数通用