2011-11-12 50 views
1

下面的代碼使用八面體作爲起始3D形狀(我在網上發現它),經過一些調整後,我仍然無法使它工作。C++在函數中傳遞指針地址

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <stddef.h> /* must include for the offsetof macro */ 
#include <GL/glew.h> 
#include <GL/glfw.h> 
#include <glm/glm.hpp> 
#include <glm/gtc/matrix_transform.hpp> 
#include <glm/gtc/type_ptr.hpp> 
#include <stdlib.h> 
#include <vector> 
#include <iostream> 

using namespace std; 

struct XYZ { 
    GLdouble x; 
    GLdouble y; 
    GLdouble z; 
}; 

struct FACET3 { 
    XYZ p1; 
    XYZ p2; 
    XYZ p3; 
}; 


void Normalise(XYZ *p_input) 
{ 
    double magnitude = 0; 
    magnitude = sqrt((p_input.x * p_input.x)+ (p_input.y * p_input.y) + (p_input.z * p_input.z)); 
    p_input.x = p_input.x/magnitude; 
    p_input.y = p_input.y/magnitude; 
    p_input.z = p_input.z/magnitude; 

} 

int CreateNSphere(FACET3 *f,int iterations) 
{ 

    int i,it; 
    double a; 
    XYZ p[6] = {0,0,1, 0,0,-1, -1,-1,0, 1,-1,0, 1,1,0, -1,1,0}; 
    XYZ pa,pb,pc; 
    int nt = 0,ntold; 

    /* Create the level 0 object */ 
    a = 1/sqrt(2.0); 
    for (i=0;i<6;i++) { 
     p[i].x *= a; 
     p[i].y *= a; 
    } 
    f[0].p1 = p[0]; f[0].p2 = p[3]; f[0].p3 = p[4]; 
    f[1].p1 = p[0]; f[1].p2 = p[4]; f[1].p3 = p[5]; 
    f[2].p1 = p[0]; f[2].p2 = p[5]; f[2].p3 = p[2]; 
    f[3].p1 = p[0]; f[3].p2 = p[2]; f[3].p3 = p[3]; 
    f[4].p1 = p[1]; f[4].p2 = p[4]; f[4].p3 = p[3]; 
    f[5].p1 = p[1]; f[5].p2 = p[5]; f[5].p3 = p[4]; 
    f[6].p1 = p[1]; f[6].p2 = p[2]; f[6].p3 = p[5]; 
    f[7].p1 = p[1]; f[7].p2 = p[3]; f[7].p3 = p[2]; 
    nt = 8; 

    if (iterations < 1) 
     return(nt); 

    /* Bisect each edge and move to the surface of a unit sphere */ 
    for (it=0;it<iterations;it++) { 
     ntold = nt; 
     for (i=0;i<ntold;i++) { 
     pa.x = (f[i].p1.x + f[i].p2.x)/2; 
     pa.y = (f[i].p1.y + f[i].p2.y)/2; 
     pa.z = (f[i].p1.z + f[i].p2.z)/2; 
     pb.x = (f[i].p2.x + f[i].p3.x)/2; 
     pb.y = (f[i].p2.y + f[i].p3.y)/2; 
     pb.z = (f[i].p2.z + f[i].p3.z)/2; 
     pc.x = (f[i].p3.x + f[i].p1.x)/2; 
     pc.y = (f[i].p3.y + f[i].p1.y)/2; 
     pc.z = (f[i].p3.z + f[i].p1.z)/2; 
     Normalise(&pa); 
     Normalise(&pb); 
     Normalise(&pc); 
     f[nt].p1 = f[i].p1; f[nt].p2 = pa; f[nt].p3 = pc; nt++; 
     f[nt].p1 = pa; f[nt].p2 = f[i].p2; f[nt].p3 = pb; nt++; 
     f[nt].p1 = pb; f[nt].p2 = f[i].p3; f[nt].p3 = pc; nt++; 
     f[i].p1 = pa; 
     f[i].p2 = pb; 
     f[i].p3 = pc; 
     } 
    } 

    return(nt); 
} 


int main() 
{ 
    FACET3 *facet; 
    int facets = CreateNSphere(facet, 2); 
    printf(" Result: %d", facets); 
} 

我想讓這段代碼運行。我基本上是試圖以產生由當我用G ++編譯它octahedron.but一個球我得到這個座標:

sphere_model.cpp:32:28: error: request for member ‘x’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:32:40: error: request for member ‘x’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:32:54: error: request for member ‘y’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:32:66: error: request for member ‘y’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:32:80: error: request for member ‘z’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:32:92: error: request for member ‘z’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:33:10: error: request for member ‘x’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:33:22: error: request for member ‘x’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:34:10: error: request for member ‘y’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:34:22: error: request for member ‘y’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:35:10: error: request for member ‘z’ in ‘p_input’, which is of non-class type ‘XYZ*’ 
sphere_model.cpp:35:22: error: request for member ‘z’ in ‘p_input’, which is of non-class type ‘XYZ*’ 

爲什麼呢?

回答

5

p_input是一個指針,你應該使用的.

+0

我現在運行的代碼昂得到一個「分段故障」 –

+1

@Trt TRT:這是完全無關的。你有另一個問題,再發一篇文章。 –

+0

gdb在CreateNSphere(FACET3 *,int)()中指出「0x00000000004011df」 –

2

->操盤手這是一個回答你的第二個問題,你已經張貼註釋。

函數CreateNSphere假定它傳遞了一個包含8個FACET3對象的數組。在main()中,您需要初始化facet,或者將其創建爲數組實例而不是指針。無論是這些將工作:

FACET3 facet[8]; 
int facets = CreateNSphere(facet, 2); 

FACET3 *facet = new FACET3[8]; 
int facets = CreateNSphere(facet, 2);