Copy constructor problem

Hi all,

I'm writing a program that makes a class Array. It creates an array of objects of any type - whether it be an array of ints, doubles, strings or chars. At this stage, I'm merely trying to set up a class of int Arrays. I'll turn it into a template afterwards.

I'm getting a strange error when I try to compile. This is my first time working with copy constructors, and I am certain that I am doing something wrong, but I don't see what.

Here is my code (trimmed down a bit):

**Note, this is my .h and .cpp all merged into one .h file. It is structured in this way because my ultimate goal is to make a template out of this program.
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
//The Header File of class Array

//prevent multiple inclusions of header file
#ifndef Array_h
#define Array_h

#include<iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

class Array
{
        friend ostream & operator << (ostream &, const Array &);
        friend istream & operator >> (istream &, Array &);

     public:
        Array(int = DEFAULTSIZE); // default constructor
        Array(const Array&); // copy constructor

        ~Array(); // destructor

     private:
        static const int DELIM = -999; // example of using a delimiter to signal end of input
        static const int DEFAULTSIZE = 10; // default array size

        bool overflow;
        int size;
        int *arr;
};
#endif

Array::Array(s = DEFAULTSIZE)
{
        if(s <= 0)
                size = DEFAULTSIZE;
        else
                size = s;

        arr = new int[size];

        if(!arr)
        {
                cerr << "error\n";
                exit(EXIT_FAILURE);
        }

}

Array::Array(const Array & copy)
{
        arr = copy.arr;
}

~Array::Array()
{
        delete arr;
        arr = NULL;
}


The error occurs on line 33. It says this:
Array.h:59:13: error: expected constructor, destructor, or type conversion before â(â token

I find this confusing because the compiler is expecting a constructor and in doing so preventing me from writing the constructor!
Your shall specify the parameter list in the function definition

Array::Array(s = DEFAULTSIZE)

What is 's' here?

I thonk you forgot to specify int before s. Moreover you may not specify a default argument two times for the same parameter in the same scope.
Last edited on
Topic archived. No new replies allowed.