Structs within structs problem Struct within struct is giving error

Hi Everyone,

I’m hoping someone can shed some light on an error I am receiving from cudafe.exe - it crashes and I get a

"1> internal error: can’t generate code for non empty constructors or destructors on device

1>1 catastrophic error detected in the compilation of “C:\Users\SEBAST~1\AppData\Local\Temp/tmpxft_0000096c_00000000-6_cuda_code.cpp1.ii”." error.

#ifndef __POINT3D__

#define __POINT3D__

struct Point3D {

    struct { float x, y, z; };

    __device__ Point3D() {}

    __device__ Point3D(const float a, const float b, const float c) : x(a), y(b), z(c) {}

    __device__ Point3D operator+(const Point3D rhs) const { return Point3D(x+rhs.x, y+rhs.y, z+rhs.z); }

    __device__ Vector3D operator-(const Point3D rhs) const { return Vector3D(x-rhs.x, y-rhs.y, z-rhs.z); }

    __device__ Point3D operator+(const float rhs) const { return Point3D(x+rhs, y+rhs, z+rhs); }

    __device__ Point3D operator-(const float rhs) const { return Point3D(x-rhs, y-rhs, z-rhs); }

    __device__ Point3D operator*(const float rhs) const { return Point3D(x*rhs, y*rhs, z*rhs); }

    __device__ Point3D operator/(const float rhs) const { return Point3D(x/rhs, y/rhs, z/rhs); }

 __device__ float   d_squared(const Point3D& p) const 


  	return ((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)	+	(z - p.z) * (z - p.z));


  __device__ float   distance(const Point3D& p) const 


  	return (sqrt((x - p.x) * (x - p.x)+ (y - p.y) * (y - p.y)+	(z - p.z) * (z - p.z)));




#ifndef __CAMERA__

#define __CAMERA__

struct Camera{

	Point3D lookat, eye;

	float  ra;

	Vector3D  u, v, w;

	Vector3D  up;

	float   exposure_time;

	float  d, zoom;

	__device__ Camera(){}

	__device__ Camera(const int test) :

  eye(0, 0, 500),

  lookat(0, 0, 0),


  up(0, 1, 0),

  u(1, 0, 0),

  v(0, 1, 0),

  w(0, 0, 1),



	__device__ void set_eye(const float x, const float y, const float z)


  eye.x = x; eye.y = y; eye.z = z;




Ok, thats my code above. I’m creating a camera in const memory using this:

__constant__ Camera cam;

That’s when the error is generated. Why is there a problem with have my Point3D structs within my Camera struct? I removed the structs so that the only members were floats and it compiled fine. Please, someone help me!


It’s because you’re declaring it const. The thing is that a class has to run a constructor to initialize itself, but const memory has to be set up statically during compilation. Sure the compiler can probably host and run the constructor itself during compilation to get the final object state, but it doesn’t want to do that. So you can’t use a constructor in a constant object.

In your code, it looks like your parameterless constructors for Camera and Point are empty. I think if you just delete them, it will compile fine. Let me know.

Thanks Alex, that was a great help. I actually fixed it before receiving the post, was just trying loads of different things whilst pulling my hair out (i’m partially bald now). Yeah, turning out removing the constructor from the Camera struct worked. Kept the constructors in for Vectors and Points, because it was complaining about no having a default constructor.

Thanks again for your explaination!