喜 我试图编译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