2012-10-27 19 views
5

代碼:如何聲明和實現一個const和內聯成員函數?

point3f.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

在此先感謝。

回答

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。但是如果你願意的話,你仍然可以指定它(如我在上面的例子中所做的那樣)。

+0

爲了澄清,問題不在於'const'和'inline'的混合(我一直這樣做),但是'inline'必須在頭文件中。 –

+0

按照你所說的,g ++只是引發了另一個警告:'warning:內聯函數'void Point3f :: project2D(ProjType,const Point2i&)const'使用但從未定義過[默認啓用] –

+0

@ComboZhc不要把在.cpp文件中的所有功能。把*只*放在標題中。 –

0

你在cpp文件中聲明爲內聯,因此沒有符號被髮射,在point3f.cpp中它總是內聯的。但是包含標題的其他文件沒有內聯函數的方法,它們需要發射這個符號。我猜這就是這種情況。

1

即時通訊測試這工作得很好! http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap7.html

例24: 這個例子可在viewd對於重載操作/功能常量性

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

希望有幫助!

平安而輕!