简体   繁体   中英

c++ programming problem

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 function julia() before using it. In your code It is defined after kernel()

Declare it before usage. Add

int julia(int x, int y); 

before kernel() definition. You could also move the entire julia() function definition before kernel() 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 ?

cuComplex( float a, float b ) : r(a), i(b){} 

makes use of a C++ concept called Initializer List to initialize your members r & i .
What it essentially does here is:

r = a;
i = b;

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 .

You need to insert int julia(int,int); below your using namespace std; line.

the function prototyping concept is not used here... according to ANSI standard we have to declare the function before its first use or atleast a signature of the function must be clearly stated prior to its use for successful compilation.

Definition can be made available at the time of linking...if the function in some other file the signature should be preeceded with keyword 'extern'...

the code otherwise seems correct.....

You are using julia before it is declared. Add the prototype for it above where you use it in kernel :

int julia(int x, int y);

This will tell the compiler that there will be a function named julia that takes two int parameters.

Alternatively, you could simply move the definition of julia above where you use it in kernel . It's up to you.

As for the struct cuComplex cuComplex( float a, float b ) : r(a), i(b){} line, the stuff after the : is the initializer list . It is a way of setting the values of member variables for an object in a constructor.

To use it, you put the name of the variable and then the value to set it to in parentheses. You should use this whenever you can because it is more efficient then setting the variables inside the constructor itself.

In your example, you're setting r to the value of a and i to the value of b . It's almost like doing

r = a;
i = b;

but better.

Yes, you can use constructors in structs. structs are nothing more than classes with all the member access levels default to public instead of private.

Others have mentioned the issue with the function definition for Julia , so I'll add a note about the code line cuComplex( float a, float b ) : r(a), i(b){} inside of struct cuComplex .

This is merely a default constructor for the structure. The body of the constructor is empty (hence nothing is between the brackets), and it uses a member initialization list to initialize the data member elements of the struct . The initializations in the member initialization list are performed before the body of the constructor is evaluated, so it is often a necessary addition if you are having to initialize a base class from a derived class ... but that's not the case here. As it's used here, it's more a matter of convenience and form. Conceptually it's the same thing as writing

cuComplex(float a, float b)
{
    r = a;
    i = b;
}

except again as noted, the initialization list is evaluated before the constructor body.

Okay, so the error is telling you that when you try to use julia is hasn't been defined -- but you see it there, so what could the problem be?

The answer is that it hasn't been defined yet . C, like a lot of languages, is basically arranged to be a "one pass" langage; that is, the compiler reads the source from top to bottom once , and if you haven't defined something when you first use it, you get an error.

There are two solutinos to this:

  1. You can arrange your source so nothing is ever used before it's defined, or
  2. You can make what's called a "forward declaratino".

In this case, it's easiest to just arrange things in the desired order, so you have

   struct cuComplex {...}
   int julia(int x, int y){...}
   void kernel(unsigned char * ptr){...}

The peculiar syntax

   cuComplex( float a, float b ) : r(a), i(b){} 

defgines a constructor that takes two parameters, a and b. It then initializes member r with the value of a, and member i with the value of b. Having done that, there's nothing left to do, so you have an empty body ().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM