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:
- Fa
const and inline member function
senso? - Come dichiarare un
const and inline member function
?
Grazie in anticipo.
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. –
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]' –
@ComboZhc Non mettere la funzione nel file .cpp. Mettilo * solo * nell'intestazione. –