2016-03-20 17 views
1

名前空間Solidsは、platonic solidsのようないくつかの一般的なソリッドの頂点とエッジ/フェイス接続についての情報を保存するだけです。名前空間における定数構造体、クラス、配列の初期化

だから私は、このヘッダファイル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 

はその後Solids.hが含まれて私のプログラムでは、私はこのようにそれをプロットしたい:私はとき

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

は、しかし、私はundefined reference to Solids::Tetrahedron::vertsを取得します。これはおそらくこのundefined-reference-to-static-const-int号に関連しています。

私は解決策がnamespace{}の外でstruc{}宣言の外に初期化するようなものでなければならないと思いますか?

同様:

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

か:

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

は、任意のよりエレガントな解決策はありませんか?

+0

なぜこの構造体が最初に必要ですか? –

+0

これは、構造体内に配列をパックする必要がないということを意味するなら、それは真です。 'Tetrahedon_verts'の代わりに' Tetrahedon.verts'を書いてください。 –

+0

ヘッダを含むすべてのファイルに 'Solids :: tetrahedron'を再定義しました。これはOne Definition Ruleに違反しています。 –

答えて

0

constexprデータメンバーを使用する代わりに、constexpr静的メンバー関数を使用できます。

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; 
関連する問題