2016-03-20 9 views
1

Ich möchte Namespace Solids machen, die nicht mehr als speichert Informationen über Scheitelpunkte und Kante/Gesicht Konnektivität für mehrere gemeinsame Festkörper wie platonic solids.Initialisierung von konstanten Strukturen, Klassen und Arrays im Namespace

Also habe ich diese Header-Datei Solids.h:

#ifndef Solids_h 
#define Solids_h 

#include "Vec3.h" // this is my class of 3D vector, e.g. class Vec3d{double x,y,z;} 

namespace Solids{ 

    struct Tetrahedron{ 
     const static int nVerts = 4; 
     const static int nEdges = 6; 
     const static int nTris = 4; 
     constexpr static Vec3d verts [nVerts] = { {-1.0d,-1.0d,-1.0d}, {+1.0d,+1.0d,-1.0d}, {-1.0d,+1.0d,+1.0d}, {+1.0d,-1.0d,+1.0d} }; 
     constexpr static int edges [nEdges][2] = { {0,1},{0,2},{0,3}, {1,2},{1,3},{2,3} }; 
     constexpr static int tris [nTris ][3] = { {0,1,2},{0,1,3},{0,2,3},{1,2,3} }; 
    } tetrahedron; 

}; 

#endif 

Da ist in meinem Programm, das Solids.h ich es wie folgt darstellen möchten beinhaltet:

void drawTriangles(int nlinks, const int * links, const Vec3d * points){ 
    int n2 = nlinks*3; 
    glBegin(GL_TRIANGLES); 
    for(int i=0; i<n2; i+=3){ 
     Vec3f a,b,c,normal; 
     convert(points[links[i ]], a); // this just converts double to float verion of Vec3 
     convert(points[links[i+1]], b); 
     convert(points[links[i+2]], c); 
     normal.set_cross(a-b, b-c); 
     normal.normalize(); 
     glNormal3f(normal.x, normal.y, normal.z); 
     glVertex3f(a.x, a.y, a.z); 
     glVertex3f(b.x, b.y, b.z); 
     glVertex3f(c.x, c.y, c.z); 
    } 
    glEnd(); 
}; 

// ommited some code there 
int main(){ 
    drawTriangles(Solids::tetrahedron.nTris, (int*)(&Solids::tetrahedron.tris[0][0]), Solids::tetrahedron.verts); 
} 

aber wenn ich tun, ich undefined reference to Solids::Tetrahedron::verts bekommen. Was wahrscheinlich mit diesem undefined-reference-to-static-const-int Problem verbunden ist.

Also ich denke, die Lösung sollte so etwas wie außerhalb von namespace{} und außerhalb der struc{} Deklaration initialisieren?

Like:

Solids::Tetrahedron::tris [Solids::Tetrahedron::nTris ][3] = { {0,1,2},{0,1,3},{0,2,3},{1,2,3} }; 

oder:

Solids::Tetrahedron.tris [Solids::Tetrahedron.nTris ][3] = { {0,1,2},{0,1,3},{0,2,3},{1,2,3} }; 

Gibt es keine elegantere Lösung?

+0

Warum brauchen Sie diese Struktur in erster Linie? –

+0

Wenn Sie meinen, dass es nicht notwendig ist, die Arrays innerhalb von struct zu packen, ist das wahr. Im Grunde war es nur, um die Namen systematischer und lesbarer zu machen: 'Tetrahedon.verts' anstelle von' Tetrahedon_verts' –

+0

Sie haben 'Solids :: tetraeder' in jeder Datei, die den Header enthält, neu definiert, was gegen die Eine-Definition-Regel verstößt. –

Antwort

0

Anstelle von consExpr-Datenelementen können Sie die statischen cstexpr-Elementfunktionen verwenden.

struct Tetrahedron{ 
    constexpr static int nVerts = 4; 
    constexpr static int nEdges = 6; 
    constexpr static int nTris = 4; 
    constexpr static Vec3d[nverts] verts() { return { {-1.0d,-1.0d,-1.0d}, {+1.0d,+1.0d,-1.0d}, {-1.0d,+1.0d,+1.0d}, {+1.0d,-1.0d,+1.0d} }; } 
    constexpr static int[nEdges][2] edges() { return { {0,1},{0,2},{0,3}, {1,2},{1,3},{2,3} }; } 
    constexpr static int[nTris ][3] tris() { return { {0,1,2},{0,1,3},{0,2,3},{1,2,3} }; } 
} tetrahedron; 
Verwandte Themen