喜 我試圖編譯C++程序Julia集我的源代碼是繼C++編程問題
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<cpu_bitmap.h>
#include<book.h>
#define DIM 20
using namespace std;
struct cuComplex{
float r;
float i;
cuComplex(float a, float b) : r(a), i(b){}
float magnitude2(void)
{
return r * r + i * i;
}
cuComplex operator*(const cuComplex& a)
{
return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);
}
cuComplex operator+(const cuComplex& a)
{
return cuComplex(r+a.r, i+a.i);
}
};
void kernel(unsigned char *ptr)
{
for (int y=0; y<DIM; y++)
{
for (int x=0; x<DIM; x++)
{
int offset = x + y * DIM;
int juliaValue =julia(x, y);
ptr[offset*4 + 0] = 255 * juliaValue;
ptr[offset*4 + 1] = 0;
ptr[offset*4 + 2] = 0;
ptr[offset*4 + 3] = 255;
}
}
}
int julia(int x, int y)
{
const float scale = 1.5;
float jx = scale * (float)(DIM/2 - x)/(DIM/2);
float jy = scale * (float)(DIM/2 - y)/(DIM/2);
cuComplex c(-0.8, 0.156);
cuComplex a(jx, jy);
int i = 0;
for (i=0; i<200; i++)
{
a = a * a + c;
if (a.magnitude2() > 1000)
{
return 0;
}
return 1;
}
}
int main(void)
{
CPUBitmap bitmap(DIM, DIM);
unsigned char *ptr = bitmap.get_ptr();
kernel(ptr);
bitmap.display_and_exit();
}
但是當我編譯它,我得到了以下錯誤:
compiling command : g++ -I /usr/local/cuda/include 5.cpp
errors:5.cpp: In function ‘void kernel(unsigned char*)’:
5.cpp:36: error: ‘julia’ was not declared in this scope
我做錯了什麼? 茱莉亞被定義在那裏,爲什麼它顯示問題? 任何人都可以解釋我有關C++函數的作用域?
任何機構都可以解釋我在struct cuComplex中的代碼行 cuComplex(float a, float b) : r(a), i(b){}
這段代碼正在做什麼? 我們可以在結構中做構造函數,或者這個r(a),我(b)在做什麼。 請爲我解釋這段代碼。
如果您可以將示例代碼範圍縮小到產生相同錯誤的最小示例,那麼不僅人們可以更快地幫助您,而且您可能可以自行計算出結果。有時需要很多代碼才能解釋發生了什麼問題,但這不是其中之一。 – 2011-05-13 04:41:07
@Chris它是一個非常明顯的答案,所以它沒有問題。如果他沒有發佈編譯器錯誤,_that_會是一個問題。 – 2011-05-13 04:43:34
@Seth - 這是真的,但之前它一直是個問題,所以我通常在看到大量代碼時關閉。 – 2011-05-13 04:44:57