[英]no default constructor exists for class x, operator new inheritence
I was trying to implement the Unified Vidrtual Memory example given on the CUDA blog: http://devblogs.nvidia.com/parallelforall/unified-memory-in-cuda-6/ 我正在尝试实施CUDA博客上给出的统一虚拟内存示例: http ://devblogs.nvidia.com/parallelforall/unified-memory-in-cuda-6/
Visual Studio 2013 gives me an error : no default constructor exists for class "Term" Visual Studio 2013给我一个错误:类“ Term”不存在默认构造函数
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <iostream>
#define NUM_ATTRIBUTES 10
class Managed {
public:
void *operator new(size_t len) {
void *ptr;
cudaMallocManaged(&ptr, len);
std::cout << "custom new for size " << len << '\n';
return ptr;
}
void *operator new[] (size_t len) {
void *ptr;
cudaMallocManaged(&ptr, len);
std::cout << "custom new for size " << len << '\n';
return ptr;
}
void operator delete(void *ptr) {
cudaFree(ptr);
}
};
class Attribute : public Managed {
public:
int num_levels;
int num_active_levels;
int active_levels;
};
class Term : public Managed {
public:
int num_active_attributes;
int active_attributes;
Attribute attribute[NUM_ATTRIBUTES];
// Unified memory copy constructor allows pass-by-value
Term (const Term &orig) {
num_active_attributes = orig.num_active_attributes;
active_attributes = orig.active_attributes;
cudaMallocManaged((void **)&attribute,NUM_ATTRIBUTES * sizeof(Attribute));
memcpy(attribute, orig.attribute, NUM_ATTRIBUTES * sizeof(Attribute));
}
};
int main()
{
Term * A = new Term[10];
getchar();
return 0;
}
Doesn't the class Term inherit from the new operator defined in the parent class Managed ? Term类不是从父类Managed中定义的新运算符继承吗?
This is unrelated to CUDA. 这与CUDA无关。 The compiler isn't generating a default constructor because you have already provided another constructor (the copy one).
编译器不会生成默认构造函数,因为您已经提供了另一个构造函数(副本1)。
Just explicitly request for a default constructor: 只是显式地请求默认构造函数:
class Term : public Managed {
public:
...
// C++11
Term() = default;
// Pre C++11 alternative (although not strictly equivalent)
Term() {}
...
};
Also note that operator new
and constructors are two different things: new
is for allocating storage, constructors are for initializing that storage into a meaningful state (roughly). 还要注意,
operator new
和构造函数是两个不同的东西: new
用于分配存储,构造函数用于将存储初始化为有意义的状态(大致)。
You need to define a default constructor which is required for elements of arrays 您需要定义数组元素所需的默认构造函数
class Term : public Managed {
public:
//...
Term () {} // default c-tor
Term() = default; // since C++11, note however that there are some
// differences between Term() = default; and Term() {}
// they are not equal when value initialization
// is considered
};
You don't have one because explicitly defined copy-ctor Term( const Term&)
prohibited generation of default constructor by compiler. 您没有一个,因为显式定义的copy-ctor
Term( const Term&)
禁止编译器生成默认构造函数。 Regarding differences between =default
and {}
in terms of default constructor you may look at this: 关于默认构造函数方面
=default
和{}
之间的区别,您可以看一下:
https://stackoverflow.com/a/23698999/1141471 https://stackoverflow.com/a/23698999/1141471
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.