hi
I’m trying to compile a c++ , program for julia set my source code is following
#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();
}
but when i compile it i got following error:
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
what i’m doing wrong ?
julia is defined there so why it is showing problem ?
can anybody explain me about the scope of function in c++?
can any body explain me the code line in struct cuComplex
cuComplex( float a, float b ) : r(a), i(b){} what this code is doing ?
can we make constructor in structure or what is this r(a), i(b) is doing.
please explain this code for me .
The error is telling you that the
kernel()needs to know the declaration of the functionjulia()before using it. In your code It is defined afterkernel()Declare it before usage. Add
before
kernel()definition. You could also move the entirejulia()function definition beforekernel()to avoid the error.Can any body explain me the code line in struct cuComplex cuComplex( float a, float b ) : r(a), i(b){} what this code is doing ?
makes use of a C++ concept called
Initializer Listto initialize your membersr&i.What it essentially does here is:
Can we make constructor in structure?
Yes, You can have a constructor in Structures. There is no difference between a C++ structure & Class except the default access specifiers, which is private in case of a class but public in case of a structure.