5

代码:

点3f.h

Class Point3f {
     ...
     inline void project2D(ProjType p, const Point2i& view) const;
};

point3f.cpp

inline void Point3f::project2D(ProjType p, const Point2i& view) const {
    switch(p) {
        case PROJ_XY:
            glVertex2f(x * view.x, y * view.y);
            break;
        case PROJ_YZ:
            glVertex2f(y * view.x, z * view.y);
            break;
        case PROJ_XZ:
            glVertex2f(x * view.x, z * view.y);
            break;
        default:
            break;
    }
}

调用此函数会在编译时引发错误:

    undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'

我尝试了没有和有inline符号的每个案例:

inline在标题中,而不是在 cpp 中:

 Warning: inline function ‘void Point3f::project2D(ProjType, const Point2i&) const’ used but never defined [enabled by default
 undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|

inline在标题中,也在 cpp 中:

 Warning: inline function ‘void Point3f::project2D(ProjType, const Point2i&) const’ used but never defined [enabled by default
 undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|

inline不在标题中,而是在 cpp 中:

 undefined reference to `Point3f::project2D(ProjType, Point2i const&) const'|

inline不在标头中,也不在 cpp 中:

 It works but that's not what I want

问题:

  1. const and inline member function有意义吗?
  2. 如何申报const and inline member function

提前致谢。

4

3 回答 3

5

存在的功能const与它无关。如果你想要它inline,你必须在头文件中定义它而不是在point3f.cpp. 例子:

class Point3f {
    ...
    inline void project2D(ProjType p, const Point2i& view) const
    {
        switch(p) {
        case PROJ_XY:
            glVertex2f(x * view.x, y * view.y);
            break;
        case PROJ_YZ:
            glVertex2f(y * view.x, z * view.y);
            break;
        case PROJ_XZ:
            glVertex2f(x * view.x, z * view.y);
            break;
        default:
            break;
        }
    }
};

在这种情况下,inline根本不需要关键字。如果在类定义里面定义函数,inline是默认的。但是如果你愿意,你仍然可以指定它(就像我在上面的例子中所做的那样。)

于 2012-10-27T21:19:30.373 回答
1

我对此进行了测试并且工作正常!此示例可在以下位置查看:http: //www.doc.ic.ac.uk/lab/cplus/c++.rules/chap7.html

示例 24:重载关于 const 的运算符/函数

   #include <iostream.h>
   #include <string.h>
   static unsigned const cSize = 1024;
   class InternalData {};

   class Buffer
   {
      public:
         Buffer( char* cp );

         // Inline functions in this class are written compactly so the example
         // may fit on one page. THIS is NOT to be done in practice (See Rule 21).

         // A. non-const member functions: result is an lvalue
         char& operator[]( unsigned index ) { return buffer[index]; }
         InternalData& get() { return data; }

         // B. const member functions: result is not an lvalue
         char operator[]( unsigned index ) const { return buffer[index]; }
         const InternalData& get() const { return data; }

      private:
         char buffer[cSize];
         InternalData data;
   };

   inline Buffer::Buffer( char* cp )
   {
      strncpy( buffer , cp , sizeof( buffer ) );
   }

   main()
   {
      const Buffer cfoo = "peter";// This is a constant buffer
      Buffer foo = "mary";// This buffer can change

      foo[2]='c';// calls char& Buffer::operator[](unsigned)
      cfoo[2] = 'c' // ERROR: cfoo[2] is not an lvalue.

      // cfoo[2] means that Buffer::operator[](unsigned) const is called.

      cout << cfoo[2] << ":" << foo[2] << endl; // OK! Only rvalues are needed

      foo.get() = cfoo.get();
      cfoo.get() = foo.get(); // ERROR: cfoo.get() is not an lvalue
   }

希望有帮助!

和平与光明!

于 2012-10-27T21:39:17.457 回答
0

您在 cpp 文件中将其声明为内联,因此不会发出任何符号,在 point3f.cpp 中它始终是内联的。但是包含头文件的其他文件无法内联函数,它们需要发出这个符号。我猜这里就是这种情况。

于 2012-10-27T21:22:24.877 回答