Array Error

So I fixed my previous code, and it compiles, but now it prints junk values.
UPDATE: Here's my new code

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

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
//Precondition:
//Postcondition:

class PartFilledArray
{
public:
	PartFilledArray(); //create instance
	PartFilledArray(int array_size); //declares the sizes of arrays being added

	//Overloading Assignment Operator
	PartFilledArray operator = (const PartFilledArray& other)
	{
		if(this == &other) return *this; // handling of self assignment
		//delete[] a; // freeing previously used memory
		double a[20];
		int _size = number_used; memcpy(a, other.a, sizeof(int) * _size);
		return *this;
	}
        void add_value(double new_entry);
		void print_array(); 

	//copy constructor
	PartFilledArray::PartFilledArray( const PartFilledArray& other )
	{
		double a[20];
		max_number = other.max_number;
		number_used = other.number_used;
	}

	//deconstructor
	~PartFilledArray()
	{
		//delete [] a; 
	} 

protected:
    double a[20]; //declares first array 
    int max_number;
    int number_used;
};

int main ()
{
  PartFilledArray instance(3);
  instance.print_array();

	//int new_entry = 5;
	//PartFilledArray add_value(new_entry);

	system ("pause");
	return 0;
}

PartFilledArray::PartFilledArray(int array_size) : max_number(array_size), number_used(0) 
{
    double a[20]; //an arbitrary number used as in place of my max_number
} 

void PartFilledArray::add_value(double new_entry)
{
	a[number_used + 1] = new_entry;
	number_used = number_used + 1;
}

void PartFilledArray::print_array()
{
	for (int i = 0; i < 20; i++)
	{
	cout <<  a[i] << endl;
	}
}
Last edited on
If your problem is destructor, then you're probably destroying nullptr.

But tell me, how does it work, if you never declared instance of PartFilledArray? You are supposed to make instance of this class
1
2
3
4
5
int main()
{
  PartFilledArray instance;
  instance.print_array();
}


If you want to use function instead of creating instance, then you have static member functions, but that's probably not something you wanted to achieve.

Also, could you please tell me what your constructor is doing?
Okay, that helps. But when I tried that, it gave me another error message.

 
1>add_value.obj : error LNK2019: unresolved external symbol "public: __thiscall PartFilledArray::PartFilledArray(void)" (??0PartFilledArray@@QAE@XZ) referenced in function _main


My constructor is converting the int value to a const, and when I remove
PartFilledArray(int array_size);
it was giving my another error entirely.
@OP: your code doesn't compile
foo.cpp:25:3: error: extra qualification ‘PartFilledArray::’ on member ‘PartFilledArray’



1
2
3
4
5
6
int main ()
{
	PartFilledArray print_array(); //function declaration

	int new_entry = 5;
	PartFilledArray add_value(new_entry); //creating an instance 



> then you're probably destroying nullptr.
1
2
delete NULL;
delete nullptr;
do nothing

The pointer is not initialized.


1
2
3
4
PartFilledArray::PartFilledArray(int array_size) : max_number(array_size), number_used(0) 
{
    double *a[20]; //declares a variable. There is no relationship with `this->a'
} //that dies here  
The code compiles when I Added the instance, but now I'm getting a new unhandled error and I don't know how I'm deleting the nullptr.
> The code compiles
It shouldn't
line 29 PartFilledArray::PartFilledArray( const PartFilledArray& other )
Also you should #include <cstring> as you are using `memcpy()'


> but now I'm getting a new unhandled error
http://www.cplusplus.com/forum/general/112111/
In your `PartFilledArray(int array_size);' constructor you are leaving the member `a' uninitialized.
Last edited on
Now that I fixed the break error, I'm still getting junk values in my array.
Whenever you declare a variable inside a class method with the same name as a data member, that variable hides the data member. The double a[20]; variables defined insidethe body of your constructors serves no purpose, and the one in operator= means the operator does nothing visible to client code.

print_array should only print the elements of a that have been used in the array.

Your copy constructor is incorrect. It does not copy any of the elements of the array that is to be copied.

What purpose does the member max_number serve?

In C++ you should prefer to use std::copy over std::memcpy. You lose type safety with memcpy (as may be evidenced by your incorrect arguments to the function.) Your operator= does not properly set the number_used or max_number members of your class. Also, operator= should return a reference to PartFilledArray.

The problem, of course, using delete[] in your destructor is that you can't use delete[] on memory which has not been allocated with new[].

I would strongly suggest implementing your type in incremental ways and testing what you've done before you try to many different methods that depend on each other when you aren't sure that any of them work.

http://ideone.com/5rxCEM
Last edited on
Topic archived. No new replies allowed.