User Defined Dynamic Array

Mar 15, 2019 at 5:46pm
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
using namespace 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()
{
    const int 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


Thank in advance
Last edited on Mar 15, 2019 at 5:46pm
Mar 15, 2019 at 5:53pm
In line 58, change x[i] to *x[i]. x[i] is a pointer to a person. You want to print the person itself.
Mar 15, 2019 at 6:05pm
When I do so, this error occurs

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 *
Mar 15, 2019 at 6:11pm
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.
Mar 15, 2019 at 6:14pm
You'll need to either overload the insertion operator<< for the class or print each individual data item.

Mar 15, 2019 at 7:40pm
It worked!! Thank you guys so much
I'm just really curious to know how it is done using an operator overload. Can you guys help me, please?
Topic archived. No new replies allowed.