help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to separate the implementation of inline functions?


From: E. Robert Tisdale
Subject: Re: how to separate the implementation of inline functions?
Date: Fri, 17 Sep 2004 20:31:42 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040803

Sean McManus wrote:

I can get inline to work successfully
but only when it's in the .h file along with the class declarations.
For example:

class myClass { // this works
    inline void myFunc() { ... } //... do something inline
};

// the code below does not work in g++, but it should as far as I know
class myClass { // in the .h
    inline void myFunc();

Don't use the inline qualifier inside the class definition!

};

void myClass::myFunc() { // in .cpp
    // ...do something inline
}

It gives the warning message
warning: inline function `void myClass::myFunc()' used but never defined

undefined reference to `myClass::myFunc()'

It looks like this is causing a linker error.

You can do this:

        > cat myClass.h
        #ifndef GUARD_MYCLASS_H
        #define GUARD_MYCLASS_H 1

        #include <iostream>

        // The code below does not work in g++,
        // but it should as far as I know
        class myClass {
        public:
          void myFunc(void);
          };

        #endif//GUARD_MYCLASS_H

        > cat myClass.cpp
        #include "myClass.h"

        void myClass::myFunc(void) {
          std::cout << "Do something." << std::endl;
          }

        > cat main.cpp
        #include "myClass.h"

        int main(int argc, char* argv[]) {
          myClass object;
          object.myFunc();
          return 0;
          }

        > g++ -Wall -ansi -pedantic -o main main.cpp myClass.cpp
        > ./main
        Do something.

Or you can do this:

        > cat myClass.h
        #ifndef GUARD_MYCLASS_H
        #define GUARD_MYCLASS_H 1

        #include <iostream>

        // The code below does not work in g++,
        // but it should as far as I know
        class myClass {
        public:
          void myFunc(void);
          };

        inline
        void myClass::myFunc(void) {
          std::cout << "Do something." << std::endl;
          }

        #endif//GUARD_MYCLASS_H

        > cat main.cpp
        #include "myClass.h"

        int main(int argc, char* argv[]) {
          myClass object;
          object.myFunc();
          return 0;
          }

        > g++ -Wall -ansi -pedantic -o main main.cpp
        > ./main
        Do something.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]