Help with increasing the size of an array of objects

I have gone on two other websites for help, but with no luck. I am bad at communicating what I need help with so please bare with me.

I cannot use vector or any functions that is not made by me such as the copy function from std because this is an assignment.

I have looked up forms online and they mention the rules of three, but I have an assignment operator, deconstructor and a copy constructor.

But, I still get core dumps and I am assuming this is because I have a dangling pointer.

I am trying to expand my array of Set objects by one each time I need more room in the array (in the function optionA.

I would be so grateful. I have been looking at this problem for 5 hours.


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
111
112
113
114
115
116
117
118
119
120
Set::Set(const Set& s):pSize(s.pSize)
{       set_name(s.name);
        numElements=s.numElements;
        
        set= new int[s.pSize];
        
        set_ptrset(s.set, s.pSize);
 
}
// In source file

Set::~Set()
{       if(set!=NULL)
        {       delete []set;
                set=NULL;
        }

        //cout<< "Hello!" << endl;
}
  void Set::operator=(const Set& s)
{       delete []set;
    
        set= new int[s.pSize]; 
        name=s.name;    
        numElements=pSize=s.pSize;
        
        for(int r=0; r<s.pSize; r++)
                set[r]=s.set[r];

// In driver file

main()
{       
        
        char option;
        Set *setptr;
        setptr= new Set[1];
        
        optionA(0, setptr);
        
        do{     
                menu(option, setptr);
        }while(option!='G');
}

void menu(char& option, Set &*setptr)
{       cout<< "Choose one of the option: " << endl;
        cout<< "[A] Create a new set " << endl;
        cout<< "[B] Delete an existing set " << endl;
        cout<< "[C] Add an element to an "
            << "existing set " << endl;
        cout<< "[D] Delete an element from an "
            << "existing set " << endl;
        cout<< "[E] Operation (+,-,^) " << endl;
        cout<< "[F] Display set " << endl;
        cout<< "Any other letter to exit " << endl;
        cin >> option;

        do_menu(option, setptr);

        cout<< endl;
}
void do_menu(char& option, Set &*setptr)
{       static int count=1;

        switch(option)
        {       case 'A':
                        optionA(count, setptr);
                        count++;
                        break;
                case 'B':
                        optionB(setptr, count);
                        count--;
                        if(count<=0)
                                optionA(count, setptr);
                                count++;
                        break;
                case 'C':
                        optionC(setptr, count);
                        break;
                case 'D':
                        optionD(setptr, count);
                        break;
                case 'E':
                        if(count>1)
                        {       optionE(count, setptr);
                                count++;
                        }
                        else
                        {       cout<< "Not enough sets!"
                                    << endl;
                        }
                        break;
                case 'F':
                        optionF(setptr, count);
                        break;
                default:
                        cout<< "Good Bye!" << endl;
                        option='G';
        }
}

// Here is the important part:
void optionA(int count, Set &*setptr)
{       if(count>0) 
        {       
                Set *temp= new Set[count+1];
             
                for(int a=0; a<count; a++)
                        temp[a]=setptr[a];

                delete []setptr;
                
                setptr= temp;
        }

        cin >> setptr[count];

}
Last edited on
Consider this
1
2
3
4
5
6
7
8
9
10
#include <iostream>
void foo(int n){
   n = 54;
}

int main(){
   int n=42;
   foo(n);
   std::cout << n << '\n';
}
It will output 42 because you are modifying a copy of the parameter passed.

That's what you are doing on `optionA()'
I edited the code on top to pass the pointers by reference, but I am still getting core dumps. I thought the reason that I was getting this error was that I was accessing memory that was deleted already. am I wrong?

Am I still missing something important?
Last edited on
1
2
3
4
5
6
7
template<const int N>
void foo( int (&arr)[N] )
{
	for( int i{ }; i < N; i++ ) {
		std::cout << arr[i] << "  ";
	}
}


http://stackoverflow.com/questions/5724171/passing-an-array-by-reference
Last edited on
Arrays are passed by reference already? But is that the same as a passing by reference a pointer that points an array?

What is the temple? We have not learned that yet.

I get this error:
Set_Driver.cpp:62:23: error: invalid initialization of reference of type ‘Set (&)[1]’ from expression of type ‘Set*’
Last edited on
Topic archived. No new replies allowed.