C++ 没有合适的构造函数可以将“test *”转换为“test”,构造函数,
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15637556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
no suitable constructor exists to convert from "test *" to "test", constructor,
提问by klo
I'm new to c++
and I'm having difficulties with constructor and classes. So, here is my header file:
我是新手,c++
并且在构造函数和类方面遇到困难。所以,这是我的头文件:
#pragma once
#include <string>
using namespace std;
class test
{
private:
string name;
int number;
public:
test();
test(string i,int b);
};
This is cpp file:
这是cpp文件:
#include "test.h"
#include <string>
using namespace std;
test::test(){}
test::test(string i,int b){
this->name=i;
this->number=b;
}
now, when I try to call
现在,当我尝试打电话时
test t=new test("rrr",8);
I get:
我得到:
1 IntelliSense: no suitable constructor exists to convert from "test *" to "test"
So, whats the thing with classes having *
in their name ( for instance, classes without .cpp file don't have asterix, all others do)? And what do I do wrong?
那么,类*
的名称中有什么(例如,没有 .cpp 文件的类没有星号,其他所有类都有)?我做错了什么?
回答by Oliver Charlesworth
I imagine you're coming from a Java/C# background. t
is not a reference type here, it's a value type. new
returns a pointer to an object. So you need any of the following:
我想你来自 Java/C# 背景。 t
这里不是引用类型,它是值类型。 new
返回一个指向对象的指针。因此,您需要以下任何一项:
test t = test("rrr", 8);
test t("rrr", 8);
test *t = new test("rrr", 8);
If you're not yet familiar with pointers, then definitely don't use the last one! But understanding the semantics of pointers is fairly critical; I recommend reading the relevant chapter(s) in your textbook...
如果你还不熟悉指针,那么绝对不要使用最后一个!但是理解指针的语义是相当关键的。我建议您阅读教科书中的相关章节...
回答by Joseph Mansfield
So, whats the thing with classes having "*" in their name ( for instance, classes without .cpp file dont have asterix, all others do)???
那么,名称中带有“*”的类是怎么回事(例如,没有 .cpp 文件的类没有星号,其他所有类都有)???
You definitely need to learn about pointers. test *
and test
are two completely different types in C++. Here's two variables with those types:
你肯定需要了解指针。test *
和test
是 C++ 中两种完全不同的类型。这是具有这些类型的两个变量:
test t;
test* p;
Here, t
has type test
, and p
as type test*
. We describe test*
as "pointer to test
".
在这里,t
有 typetest
和p
as type test*
。我们将其描述test*
为“指向test
”。
You can often think of a pointer as being the memory address of an object. So in p
, since it is a pointer, we could store the memory address of t
, which is a test
. To get the address of an object, we use the unary &
operator, like so:
您通常可以将指针视为对象的内存地址。所以在 中p
,由于它是一个指针,我们可以存储 的内存地址t
,也就是test
。要获取对象的地址,我们使用一元运算&
符,如下所示:
test t;
test* p = &t;
Note that t
isa test
object. You didn't need to say new test()
. This is where C++ differs from other languages that you might have used, like C# and Java. In the above C++ code, t
is a test
object.
请注意,这t
是一个test
对象。你不用说new test()
。这就是 C++ 与您可能使用过的其他语言(如 C# 和 Java)不同的地方。在上面的C++代码中,t
是一个test
对象。
However, you cancreate objects with new test()
, so what's the difference?
但是,您可以使用 来创建对象new test()
,那么有什么区别呢?
test t;
creates a test
object with automatic storage duration. This means it is destroyed at the end of its scope (often the function is being declared within).
test t;
创建test
具有自动存储持续时间的对象。这意味着它在其作用域结束时被销毁(通常是在其中声明函数)。
new test()
creates a test
object with dynamic storage duration. This means you have to destroy the object manually, otherwise you'll have a memory leak. This expression returns a pointer and so you can initialise a pointer object with it:
new test()
创建一个test
具有动态存储持续时间的对象。这意味着您必须手动销毁该对象,否则会出现内存泄漏。这个表达式返回一个指针,所以你可以用它初始化一个指针对象:
test* p = new test();
So now let's look at your problem:
所以现在让我们看看你的问题:
test t=new test("rrr",8);
We now know that new test("rrr", 8)
returns a pointer to test
(a test*
). However, you're trying to assign it to a test
object. You simply can't do this. One of them is an address and the other is a test
. Hence the compiler says "no suitable constructor exists to convert from test *
to test
." Makes sense now, doesn't it?
我们现在知道new test("rrr", 8)
返回一个指向test
(a test*
)的指针。但是,您正在尝试将其分配给一个test
对象。你根本无法做到这一点。其中一个是地址,另一个是test
. 因此编译器说“不存在合适的构造函数来从 转换test *
为test
。” 现在说得通,不是吗?
Instead, you should prefer to use automatic storage duration. Only use new
if you really really need to. So just do:
相反,您应该更喜欢使用自动存储持续时间。仅new
在您确实需要时才使用。所以只需这样做:
test t("rrr", 8);
回答by Kiril Kirov
test t=new test("rrr",8);
must be
必须是
// v
test* t=new test("rrr",8);
So, whats the thing with classes having "*" in their name
那么,名称中带有“*”的类是怎么回事
*
is used to indicate a pointer, it's not in the name of the class. But it's a big topic, so you should do some reseach on this.
*
用于指示指针,它不在类的名称中。但这是一个很大的话题,所以你应该对此进行一些研究。
回答by Spook
*
is not part of the name, it's a modifier denoting, that the object is a pointer. A pointer is a variable holding address to some place in memory, where the actual object is stored. Some basics:
*
不是名称的一部分,它是一个修饰符,表示对象是一个指针。指针是一个变量,它保存着指向内存中某个地方的地址,在那里存储了实际的对象。一些基础知识:
int i = 5;
int * pI = &i;
int * pI
means, that you want to declare a pointer to place in memory, where an int is held. &i
means, that you want to retrieve a pointer to variable. So now pI holds address in memory, where i is stored. Now you can dereferencea pointer - get to value of the pointer:
int * pI
意味着,您要声明一个指向内存中的指针,其中保存了一个 int。&i
意味着,您要检索指向变量的指针。所以现在 pI 在内存中保存地址, i 存储在那里。现在您可以取消引用指针 - 获取指针的值:
int j = *pI;
Now you tell the compiler, that it should go to the address pointed to by pI and retreive its contents (since pI is a pointer to int, compiler will assume, that there's an int there).
现在你告诉编译器,它应该去 pI 指向的地址并检索它的内容(因为 pI 是一个指向 int 的指针,编译器会假设那里有一个 int)。
Now, back to your example. new
operator allocates memory dynamically for an object, so:
现在,回到你的例子。new
运算符为对象动态分配内存,因此:
new test("rrr", 8);
results in allocating a memory for test class, calling its constructor with parameters "rrr" and 8 and returning a pointer to allocated memory. That's why you cannot assign it to test
variable: new operator in this case returns a test *
.
导致为测试类分配内存,使用参数“rrr”和 8 调用其构造函数并返回指向已分配内存的指针。这就是为什么您不能将其分配给test
变量的原因:在这种情况下 new 运算符返回一个test *
.
Try this code:
试试这个代码:
test * t = new test("rrr", 8);
回答by bash.d
You did not define t
as a pointer:
您没有定义t
为指针:
test* t=new test("rrr",8);
Or just
要不就
test t = test("rrr",8);
回答by 0x499602D2
T* t = new T;
// ^^^
When new is used in this object construction, it denotes the creation of a pointer. What are doing is dynamically allocating memory which I'm sure you didn't mean to do. The Rather, typical stack allocated object construction is done simply like this:
当在这个对象构造中使用 new 时,它表示创建了一个指针。正在做的是动态分配内存,我相信你不是故意的。相反,典型的堆栈分配对象构造是这样完成的:
T t;
Even if you had intended on creating a pointer and allocating the memory, you did it the wrong way. A pointer is created with the *
symbol, which you lacked in your code. Secondly, when you're done using the memory you created, you must remember to delete
/delete[]
your code. delete[]
is used on dynamically allocated arrays. So this is how it would look for your pointer:
即使您打算创建一个指针并分配内存,您也做错了。使用*
符号创建了一个指针,而您的代码中缺少该符号。其次,当您使用完您创建的内存时,您必须记住delete
/delete[]
您的代码。delete[]
用于动态分配的数组。所以这就是它寻找你的指针的方式:
delete t;