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