c++编程问题

发布于 2024-11-07 03:10:03 字数 1851 浏览 0 评论 0原文

你好 我正在尝试编译一个 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

我做错了什么? Julia 是在那里定义的,那么为什么它会出现问题呢? 谁能解释一下c++中函数的作用域?

任何人都可以向我解释 struct cuComplex 中的代码行吗 cuComplex( float a, float b ) : r(a), i(b){} 这段代码在做什么? 我们可以在结构体中创建构造函数吗?或者这个 r(a), i(b) 正在做什么。 请为我解释一下这段代码。

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 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

春花秋月 2024-11-14 03:10:03

该错误告诉您 kernel() 在使用函数 julia() 之前需要知道它的声明。在你的代码中它是在 kernel() 之后定义的,

在使用之前声明它。添加

int julia(int x, int y); 

kernel() 定义之前。您还可以将整个 julia() 函数定义移到 kernel() 之前以避免错误。

任何人都可以向我解释 struct cuComplex cuComplex( float a, float b ) : r(a), i(b){} 中的代码行这段代码在做什么吗?

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

使用名为 Initializer List 的 C++ 概念来初始化您的成员 r &
它在这里的本质是:

r = a;
i = b;

我们可以在结构中创建构造函数吗?
是的,您可以在结构中拥有构造函数。 C++ 结构和 C++ 结构之间没有区别。类,但默认访问说明符除外,如果是 class,则默认访问说明符为 private,但 public(如果是结构)。

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.

坠似风落 2024-11-14 03:10:03

您需要在 using namespace std; 行下方插入 int julia(int,int);

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

日记撕了你也走了 2024-11-14 03:10:03

这里没有使用函数原型概念...根据 ANSI 标准,我们必须在首次使用函数之前声明该函数,或者至少在使用该函数之前必须明确声明该函数的签名才能成功编译。

定义可以在链接时可用...如果其他文件中的函数签名应以关键字“extern”开头...

否则代码看起来是正确的...

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.....

椵侞 2024-11-14 03:10:03

您在声明之前就使用了 julia。在上面在 kernel 中使用它的地方添加它的原型:

int julia(int x, int y);

这将告诉编译器将有一个名为 julia 的函数,它需要两个 int > 参数。

或者,您可以简单地将 julia 的定义移至 kernel 中使用它的位置。由你决定。

至于 struct cuComplex cuComplex( float a, float b ) : r(a), i(b){} 行,: 之后的内容是 初始化列表。它是一种在构造函数中设置对象的成员变量值的方法。

要使用它,您可以将变量的名称和要设置的值放在括号中。您应该尽可能使用它,因为它比在构造函数本身内设置变量更有效。

在您的示例中,您将 r 设置为 a 的值,将 i 设置为 b 的值。这几乎就像做

r = a;
i = b;

但更好。

是的,您可以在结构中使用构造函数。结构只不过是所有成员访问级别默认为公共而不是私有的类。

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.

灼痛 2024-11-14 03:10:03

好的,所以错误告诉您,当您尝试使用 julia 时,尚未定义 - 但您在那里看到它,那么问题可能是什么?

答案是它尚未定义。尚未。 C 与许多语言一样,基本上被安排为“一次性”语言;也就是说,编译器从上到下读取源代码一次,如果您在第一次使用它时没有定义某些内容,则会出现错误。

对此有两个解决方案:

  1. 您可以安排源代码,以便在定义之前不会使用任何内容,或者
  2. 您​​可以制作所谓的“前向声明”。

在这种情况下,最简单的方法是按照所需的顺序排列事物,因此您可以使用

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

特殊的语法

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

定义一个带有两个参数 a 和 b 的构造函数。然后它用 a 的值初始化成员 r,用 b 的值初始化成员 i。完成此操作后,就没有什么可做的了,所以你的身体是空的()。

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 ().

今天小雨转甜 2024-11-14 03:10:03

其他人提到了 Julia 的函数定义问题,因此我将添加有关代码行 cuComplex( float a, float b ) : r(a), i(b ){} 位于 struct cuComplex 内。

这只是该结构的默认构造函数。构造函数的主体是空的(因此括号之间没有任何内容),并且它使用成员初始化列表来初始化struct的数据成员元素。成员初始化列表中的初始化是在构造函数的主体被求值之前执行的,因此如果您必须从派生类初始化基类,那么它通常是必要的添加...但这里的情况并非如此。当它在这里使用时,更多的是方便和形式的问题。从概念上讲,它与编写相同,

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

只是再次指出,初始化列表在构造函数主体之前进行评估。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文