any help plz?

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
#include <iostream>
#include <cstdlib>

using namespace std;
template <class Type>

class Person
{
private:
    int age;
    string name;

public:
    void print()const;
    Person *operator ->();
    void *operator new(size_t);
    void operator delete(void*);
    void operator delete[](void*);

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

template <class Type>
Person Person<Type>::*operator ->()
{
    return this;
}

template <class Type>
void Person<Type>::operator delete(void* obj)
{
    delete obj;
}
template <class Type>
void Person<Type>:: operator delete[](void* obj)
{
  delete [] obj;
}

template <class Type>
void Person<Type>::operator new(size_t obj)
{
    obj = new Person [ 5];
}

template <class Type>
Person<Type>::Person()
{
    name = "";
    age= 0;
}

template <class Type>
Person<Type>::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 some errors which i cannot solve, i need your help...

errors are on line:

25.
42.

66 (2 errors).
67.
76 (2 errors).
77 (2 errors).
79.
82 (2 errors).
83 (2 errors).
88.

some of these errors are the same so this hopefully wont be much of a disturbance for you but plz help me out :/

Thanx in advance :)
1
2
template <class Type>
class Person
¿what do you think that that means?
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
#include <iostream>
#include <cstdlib>
#include <string> // *** required

template <class Type>
class Person // *** why is this a template?
{
private:
    int age = 0 ;
    std::string name = "anonymous" ;

public:
    std::ostream& print( std::ostream& stm = std::cout ) const
    { return stm << "{ " << name << ", " << age << " }" ; }
    
    // (you may ignore the noexcept for now)
    const Person* operator->() const noexcept { return this ; } ; // *** why is this required?
    Person* operator->() noexcept ; // *** why is this required?

    static void* operator new(size_t);
    static void* operator new[] (size_t);
    static void operator delete(void*);
    static void operator delete[] (void*);

    Person(std::string ,int);
    Person() = default ;
};

template <class Type>
Person<Type>* Person<Type>::operator ->() noexcept
{
    return this;
}

template <class Type>
void* Person<Type>::operator new( size_t sz )
{
    std::cout << "Person<>::operator new: size == " << sz << '\n' ;
    return std::malloc(sz) ; // check for nullptr, throw on allocation failure?
}

template <class Type>
void* Person<Type>::operator new[] ( size_t sz )
{
    std::cout << "Person<>::operator new[]: size == " << sz << '\n' ;
    return std::malloc(sz) ; // check for nullptr, throw on allocation failure?
}

template <class Type>
void Person<Type>::operator delete(void* p)
{
    std::cout << "Person<>::operator delete: p == " << p << '\n' ;
    std::free(p) ;
}

template <class Type>
void Person<Type>::operator delete[] (void* p)
{
    std::cout << "Person<>::operator delete[] : p == " << p << '\n' ;
    std::free(p) ;
}

template <class Type>
Person<Type>::Person(std::string y, int x)
{
    name = y;
    age = x;
}

const int maxSize = 5;

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

    // **** The += operator has not been defined
    // w += 7;
    // w.print();
    // w->print();

    //  The new operator and delete operator // **** instead, use std::unique_ptr<>
    Person<int>* q = new Person<int> ("Omar Ali", 65); // use std::unique_ptr<>
    q->print() << '\n' ;
    delete q;

    //  The new [ ] operator and delete operator // **** instead, use std::unique_ptr<>
    Person<double>* r = new Person<double>[maxSize];

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

    delete [] r;
}
I dont know i made this template because when i run the program it gives me an error saying no template found or something like that so i asumed that i need a template.

also ty for your help but i forgot to mention that i can not change the main...is there another way?
This will work without any change to main.
http://coliru.stacked-crooked.com/a/5a8ac3fa2fa956b8

I feel terribly uneasy about this; I get the distinct impression that you are just typing in code without trying to understand what the code is doing. If the intent is to learn C++, it is better to write five lines of code that you understand rather than a hundred lines of code that you somehow got into a compilable state through a combination of blind trial and error and inspired guesswork.

If you do not understand something, read up on it, ask in the programming forum. Above all:
The most effective way to increase your knowledge is to try new problems in a controlled way. Pick one aspect of C++ that you haven't understood before and write a program that, aside from using that one aspect, uses only things that you have already mastered. Then do what it takes to understand what your program is doing - and why. - Andrew Koenig and Barabara Moo in 'Ruminations on C++'.
i have modified my code to this:
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
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Person
{
private:
    int age;
    string name;

public:
    void print()const;
    Person *operator ->();
    void *operator new(size_t);
    void operator delete(void*);
    void operator delete[](void*);
    Person operator +=(const Person&);
    Person operator+(const Person&);

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

Person Person::*operator->()
{
    return this;
}

void Person::operator delete(void* obj)
{

}

void Person:: operator delete[](void* obj)
{

}


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

Person operator+(Person age, const Person& obj)
{
    age += obj;
    return *this;
}
Person Person::operator +=(const Person& x)
{
    return *this;
}

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;
}


Almost all the previous errors are gone but im stuck with a few left.

These are the errors:

line 26,42,44,50, and 80.

plz help fast and thanx :p
Mr. JLBorges Dont worry my intentions is to learn.........im studying computer science so its a big mistake to cheat my self in this :)
I'm not uneasy about your intention to learn; if that was not the case, I wouldn't have posted (again) in this thread. I start with the axiom that anyone who seeks help here genuinely wants to learn C++.

What I'm uneasy about is the way (I think) you are trying to learn.
Can you post the assignment? This is some very weird code you've got. Do you have to override new, delete and operator->?
Mr. JLBorges I know what the codes are supposed to do and what is the purpose of it.

Everyone have his own method of learning, i code twice a week, but still im a student not Bill Gates, so im expected to not know every single letter in c++ and i dont think if someone asks for a bit of help it will make him learn in a bad way :/

I appreciate your concern tho, thanx :)
Isn't that called student obfuscated obfuscation?
Topic archived. No new replies allowed.