need an answer

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Person
{
    friend Person operator+(Person&,  Person&);

private:
    int age;
    string name;

public:
    void print()const;
    Person *operator ->()
    {
        return this;
    }

    void *operator new(size_t);
    void operator delete(void*);
    void operator delete[](void*);
    Person operator+=(  int);

    Person(string ,int);
    Person();
};

void Person::print()const
{
    cout<< "I am " << name<<" and my age is " << age<< endl;
}



void Person::operator delete(void* obj)
{
    obj = NULL;
}

void Person:: operator delete[](void* obj)
{
    obj = NULL;
}


void Person::operator new(size_t obj)
{
    void *obj;
    obj = new Person [ 5];
     return obj;
}

Person operator+(Person& age,  Person& obj)
{
    age = age + obj;

}
Person Person::operator +=(int y)
{
    Person p;
    *this += y;
}

Person::Person()
{
    name = "";
    age= 0;
}


Person::Person(string y, int x)
{
    name = y;
    age = x;
}

    const int maxSize = 5;

int main()
{
//  The arrow operator
    Person w("Ali Omar", 35);
    w.print();
    w->print();

//  The += operator
    w += 7;
    w.print();
    w->print();

//  The new operator and delete operator
    Person *q;
    q = new Person("Omar Ali", 65);
    q->print();
    delete q;

//  The new [ ] operator and delete operator
    Person *r;
    r = new Person[maxSize];

    for(int i=0; i<maxSize; i++)
        r[i]->print();

    delete [ ] r;

    return 0;
}



I have tried allot but i cant figure out the error in line 49.

49: 'operator new' must return 'void*'

plz help and thank you
Last edited on
I didn't even see your question, but as soon as I saw your definition for the new operator, the first thought that came to mind is "recursive!". To answer your question, change the return type of that function to void *

void * Person::operator new(size_t obj)

As for my observation, you are defining the behavior of the new operator but in the function you are using new to create memory for a Person object. I might be wrong but this could result in an infinite loop when using the operator. You should result to C-level memory allocation if you are going to redefine the behavior of new for your object

Another observation (the same function), your parameter has a value called obj and in the function you have defined a variable of type void *, also called obj. How are you sure which one is being used when you do obj = new Person [ 5];? Because either could be used in that statement
Line 22
wow thanx allot........although i wrote * after the void multiple times but it simply gave me more errors :S

and no it didnt give me an infinite loop but it made the program crash........which is ok at the moment i expected that, i always try to write the whole code and after it gives me any output i start modifying my code so i can get my desired out put ^^

Thanx allot and sorry for this silly question i disturbed you guys with.......much love <3
Topic archived. No new replies allowed.