2012-10-27 23 views
5

Codice:Come dichiarare e implementare una funzione membro const e inline?

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

chiamata a questa funzione genera un errore in fase di compilazione:

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

Ho provato ogni caso senza e con il simbolo inline:

inline nell'intestazione, non in 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 nell'intestazione, anche in 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 non nell'intestazione, ma in cpp:

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

inline non nell'intestazione, né in cpp:

It works but that's not what I want 

Domanda:

  1. Fa const and inline member function senso?
  2. Come dichiarare un const and inline member function?

Grazie in anticipo.

risposta

5

La funzione è const non ha nulla a che fare con esso. Se lo si desidera inline, è necessario definirlo nel file di intestazione anziché in point3f.cpp. Esempio:

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

In questo caso, la parola chiave inline non è necessario a tutti. Se si definisce la funzione all'interno della definizione della classe, inline è l'impostazione predefinita. Ma puoi ancora specificarlo, se lo desideri (come ho fatto nell'esempio sopra).

+0

Per chiarire, il problema non è nel mix di 'const' e' inline' (lo faccio tutto il tempo), ma nel fatto che 'inline' deve essere nel file di intestazione. –

+0

In seguito a ciò che hai detto, g + + solleva un altro avvertimento che 'avviso: funzione inline 'void Point3f :: project2D (ProjType, const Point2i &) const' usato ma mai definito [abilitato di default]' –

+0

@ComboZhc Non mettere la funzione nel file .cpp. Mettilo * solo * nell'intestazione. –

0

Lo hai dichiarato come inline nel file cpp, quindi non viene emesso alcun simbolo, in point3f.cpp è sempre in linea. Ma altri file che includono l'intestazione non hanno modo di evidenziare la funzione, hanno bisogno di questo simbolo per essere emessi. Immagino che sia il caso qui.

1

Im test questo e funziona bene! questo esempio può essere viewd a: http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap7.html

Esempio 24: Il sovraccarico di un operatore/funzione rispetto const-ness

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

speranza con l'aiuto!

pace e luce!

Problemi correlati