1

I am new to C++ so I apologise in advance for the mundane nature of my question.

I have been asked to move the following inline function from a header file to a source file. However I am struggling to get the syntax right in the source file. This is the inline function in the header file:

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  ;} ;

Any assistance would be greatly appreciated.

Many thanks in advance.

C++Newbie!

4
  • 3
    That's not a function. That's an entire class. Commented Sep 18, 2012 at 16:26
  • 1
    What's the question and that's not a function. Commented Sep 18, 2012 at 16:27
  • 1
    For this kind of basic question, please replace your code with a simple example. This avoids posting your code and expecting people to read. Commented Sep 18, 2012 at 16:28
  • 1
    You ask a very simple question yet post a whole load of code. You can illustrate the problem with a five-line example. Commented Sep 18, 2012 at 16:34

1 Answer 1

5

You move the functions out of the header into a cpp file by first creating the corresponding cpp file if it does not exist, and then copying the bodies of your functions into that file one by one, prefixing their names with the name of the class and two colons ::, like this:

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 ;
        }
}

from the header becomes

void initialise();

in the header, and

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 ;
        }
}

in the cpp file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.