Hey guys,
I am trying to create a user-defined dynamic array like the one in this video (https://www.youtube.com/watch?v=jxpa7QEGojw)
I am having a problem completing such an array dynamically. Someone, please show me the right way
Below is my attempt
#include <iostream>
#include <string>
usingnamespace std;
#ifndef Class_h
#define Class_h
class person
{
public:
person();
person(string, string, int);
~person();
void printdetail() const;
private:
string name;
string dob;
int age;
};
#endif
#include "Class.h"
person::person()
{
name = "xxxxx";
dob = "xx- xx- xxxx";
age = 0;
n = new string [0];
}
person::person(string n, string Dob, int g)
{
name = n;
dob = Dob;
age = g;
}
person::~person()
{
}
void person::printdetail() const
{
cout << "THE NAME WAS " << name << endl << "THE DATE OF BIRTH WAS " << dob << endl << "THE AGE WAS " << age << endl;
}
int main()
{
constint n = 5;
person **x;
x = new person*[n];
for (int i = 0; i < 5; i++)
{
x[n] = new person ("John", "13 - 7 - 1999", 12);
}
for (int i = 0; i < 5; i++)
{
cout << x[i] << endl;
}
}
This is the output
0x0
0x0
0x34
0x0
0x100540033
Program ended with exit code: 0
Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream<char>') and 'person')
Candidate function not viable: no known conversion from 'person' to 'const void *' for 1st argument; remove *
You haven't overloaded operator<<.
It looks like you mean to use x[i]->printdetail() instead.
Also, on line 54 above you should use i instead of n.
And line 29 above shouldn't be there at all.