简体   繁体   English

C ++编程问题

[英]c++ programming problem

hi I'm trying to compile a c++ , program for julia set my source code is following 嗨,我正在尝试编译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();
}

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 ? 茱莉亚(Julia)被定义在那里,为什么它显示问题? can anybody explain me about the scope of function in c++? 有人可以向我解释一下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 ? 谁能cuComplex( float a, float b ) : r(a), i(b){}我解释struct cuComplex cuComplex( float a, float b ) : r(a), i(b){}的代码行cuComplex( float a, float b ) : r(a), i(b){}该代码在做什么? can we make constructor in structure or what is this r(a), i(b) is doing. 我们可以构造结构的构造函数还是r(a),i(b)在做什么。 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. 错误告诉您kernel() julia()在使用之前必须知道函数julia()的声明。 In your code It is defined after kernel() 在您的代码中,它是在kernel()之后定义的

Declare it before usage. 使用前请声明。 Add

int julia(int x, int y); 

before kernel() definition. kernel()定义之前。 You could also move the entire julia() function definition before kernel() to avoid the error. 您也可以将整个julia()函数定义移至kernel()之前,以避免出现错误。

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 ? 谁能向我解释struct cuComplex cuComplex(float a,float b)中的代码行:r(a),i(b){}该代码在做什么?

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

makes use of a C++ concept called Initializer List to initialize your members r & i . 利用称为Initializer List的C ++概念初始化成员ri
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 . 除了默认的访问说明符外,C ++结构和类之间没有区别,默认访问说明符在类的情况下是私有的 ,而在结构的情况下是公共的。

You need to insert int julia(int,int); 您需要插入int julia(int,int); below your using namespace std; 在您using namespace std;下面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. 函数原型概念不在此处使用...根据ANSI标准,我们必须在首次使用函数之前声明函数,或者在成功使用函数之前必须明确声明函数的签名。

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'... 链接时可以使用定义...如果函数在其他文件中,则签名应以关键字'extern'开头...

the code otherwise seems correct..... 该代码否则似乎是正确的.....

You are using julia before it is declared. 您在声明之前使用了julia Add the prototype for it above where you use it in kernel : 将其原型添加到您在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. 这将告诉编译器将存在一个名为julia的函数,该函数带有两个int参数。

Alternatively, you could simply move the definition of julia above where you use it in kernel . 另外,您可以简单地将julia的定义移到您在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 . 至于struct cuComplex cuComplex( float a, float b ) : r(a), i(b){}行, :之后的内容是初始化列表 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 . 在您的示例中,将r设置为a的值, a i设置为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. struct只是类,所有成员访问级别默认为public而不是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 . 其他人所说的与函数定义中的问题Julia ,所以我会添加有关代码行记cuComplex( float a, float b ) : r(a), i(b){}内部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 . 构造函数的主体为空(因此括号之间没有任何内容),并且它使用成员初始化列表来初始化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? 好的,所以错误告诉您,当您尝试使用julia未定义-但您在那里看到它,那可能是什么问题?

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; 像许多语言一样,C基本上被安排成一种“单程”语言。 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. 定义一个采用两个参数a和b的构造函数。 It then initializes member r with the value of a, and member i with the value of b. 然后,它用a的值初始化成员r,用b的值初始化成员i。 Having done that, there's nothing left to do, so you have an empty body (). 完成此操作后,无需执行任何操作,因此您有一个空的正文()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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