Error in code
Jul 13, 2013 at 11:47am UTC
I am getting error in line 76 :default argument given for parameter 1 of `Array<T>::Array(int) and in line 54: after previous specification in `Array<T>::Array(int)'. Can someone please explain me what is wrong with this code . it is a code from my book and obviously it has mistakes.. Thank you anyway!!
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#include <iostream.h>
const int DefaultSize = 3;
// trivijalna klasa za dodavanje nizovima
class Animal
{
public :
// konstruktori
Animal(int );
Animal();
~Animal();
// metodi pristupa
int GetWeight() const { return itsWeight; }
void SetWeight(int theWeight) { itsWeight = theWeight; }
// prijateljski operatori
friend ostream& operator << (ostream&, const Animal&);
private :
int itsWeight;
};
// operator ekstrakcije za stampanje zivotinja
ostream& operator <<
(ostream& theStream, const Animal& theAnimal)
{
theStream << theAnimal.GetWeight();
return theStream;
}
Animal::Animal(int weight):
itsWeight(weight)
{
cout << "animal(int) " ;
}
Animal::Animal():
itsWeight(0)
{
cout << "animal() " ;
}
Animal::~Animal()
{
cout << "Destroyed an animal..." ;
}
template <class T> // deklarise sablon i parametar
class Array // klasa koja se parametarizuje
{
public :
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete [] pType; }
// operatori
Array& operator =(const Array&);
T& operator [](int offSet) { return pType[offSet]; }
const T& operator [](int offSet) const
{ return pType[offSet]; }
// metode pristupa
int GetSize() const { return itsSize; }
// prijateljska funkcija
friend ostream& operator << (ostream&, const Array<T>&);
private :
T *pType;
int itsSize;
};
template <class T>
Array<T>::Array(int size = DefaultSize):
itsSize(size)
{
pType = new T[size];
for (int i = 0; i<size; i++)
pType[i] = (T)0;
}
template <class T>
Array<T>& Array<T>::operator =(const Array &rhs)
{
if (this == &rhs)
return *this ;
delete [] pType;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i<itsSize; i++)
pType[i] = rhs[i];
return *this ;
}
template <class T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i<itsSize; i++)
pType[i] = rhs[i];
}
template <class T>
ostream& operator << (ostream& output, const Array<T>& theArray)
{
for (int i = 0; i<theArray.GetSize(); i++)
output << "[" << i << "] " << theArray[i] << endl;
return output;
}
Array<Animal>::Array(int AnimalArraySize):
itsSize(AnimalArraySize)
{
pType = new Animal[AnimalArraySize];
}
void IntFillFunction(Array<int >& theArray);
void AnimalFillFunction(Array<Animal>& theArray);
enum BOOL {FALSE, TRUE};
int main()
{
Array<int > intArray;
Array<Animal> animalArray;
IntFillFunction(intArray);
AnimalFillFunction(animalArray);
cout << "intArray...\n" << intArray;
cout << "\nanimalArray...\n" << animalArray << endl;
return 0;
}
void IntFillFunction(Array<int >& theArray)
{
BOOL Stop = FALSE;
int offset, value;
while (!Stop)
{
cout << "Enter an offset (0-9) and a value. " ;
cout << "(-1 to stop): " ;
cin >> offset >> value;
if (offset < 0)
break ;
if (offset > 9)
{
cout << "***Please use values between 0 and 9.***\n" ;
continue ;
}
theArray[offset] = value;
}
}
void AnimalFillFunction(Array<Animal>& theArray)
{
Animal * pAnimal;
for (int i = 0; i<theArray.GetSize(); i++)
{
pAnimal = new Animal(i*10);
theArray[i] = *pAnimal;
delete pAnimal;
}
}
Jul 13, 2013 at 12:06pm UTC
Default argument shall be specified only once either in the function declaration or in the function definition. Remove the default argument for example in the constructor definition.
Jul 13, 2013 at 12:14pm UTC
As it is said in the C++ Standard
A default argument shall not be redefined by a later declaration (not even to the same value ).
Jul 13, 2013 at 5:45pm UTC
but then im getting two errors: undefined reference from lines 132 and 133
Jul 13, 2013 at 6:43pm UTC
Thank you vlad I solved it!!!
Topic archived. No new replies allowed.