I'm new to c++
and I'm having difficulties with constructor and classes. So, here is my header file:
#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:
#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?
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:
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...
So, whats the thing with classes having "*" in their name ( for instance, classes without .cpp file dont have asterix, all others do)???
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 t;
test* p;
Here, t
has type test
, and p
as type test*
. We describe test*
as "pointer to 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:
test t;
test* p = &t;
Note that t
is a 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.
However, you can create objects with new test()
, so what's the difference?
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).
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:
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?
Instead, you should prefer to use automatic storage duration. Only use new
if you really really need to. So just do:
test t("rrr", 8);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With