samedi 4 avril 2015

When using templates, how do I fix the following compiler error to achieve the desired behavior of my program?

I'm practicing templates in Visual Studio 2013 from a not so well written C++ book and I receive the following compiler errors respectively, "error C4430: missing type specifier - int assumed. Note: C++ does not support default-int" and "error C2143: syntax error : missing ',' before '<'".


Both refer me to line 10 which is...


std::ostream& operator<< (std::ostream&, const Array<T>&);


The desired behavior of my code is to show the creation and destruction of temporary Animal objects using templates.


The troubleshooting steps I have tried so far include substituting std::iostream& with int in my return type and first parameter type of line 10. After doing so the error messages remained the same. This seemed to suggest that the problem may be the second parameter type. I then added the keyword typename to the second parameter of my overloading operator function (operator<<) on line 10 and the error still persisted.


Line 10 can be found in the header file below.


//Array.h




#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
#include "Animal.h"

const int DefaultSize = 3;

template <typename T>
std::ostream& operator<< (std::ostream&, const Array<T>&);

template <typename T> // declare the template and the paramenter
class Array // the class being parameterized
{
public:
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete[] pType; }

// operators
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const { return pType[offSet]; }

// accessors
int GetSize() const { return itsSize; }

// friend function
friend std::ostream& operator<< (std::ostream&, const Array<T>&);

private:
T *pType;
int itsSize;
};

template <typename T>
Array<T>::Array(int size = DefaultSize) :itsSize(size)
{
pType = new T[size];
for (int i = 0; i < size; i++)
pType[i] = static_cast<T>(0);
}

Array<Animal>::Array(int AnimalArraySize) :itsSize(AnimalArraySize)
{
pType = new Animal[AnimalArraySize];
}

template <typename T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.GetSzie();
pType = new T[itsSize];
for (int i = 0; i < itsSize; i++)
pType[i] = rhs[i];
}

template <typename 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 <typename T>
std::ostream& operator<< (std::ostream& output, const Array<T> &theArray)
{
for (int i = 0; i < theArray.GetSize(); i++)
output << "[" << i << "]" << theArray[i] << std::endl;
return output;
}

#endif


Here's the rest of my code from my book for your reference.


//Animal.h




#ifndef ANIMAL_H
#define ANIMAL_H

#include <iostream>

class Animal
{
public:
// constructors
Animal();
Animal(int);
~Animal();

// accessors
int GetWeight() const { return itsWeight; }
void SetWeight(int theWeight) { itsWeight = theWeight; }

// friend operators
friend std::ostream& operator<<(std::ostream&, const Animal&);

private:
int itsWeight;
};

#endif


//Animal.cpp




#include "Animal.h"
#include <iostream>

Animal::Animal() :itsWeight(0)
{
std::cout << "animal() ";
}

Animal::Animal(int weight) : itsWeight(weight)
{
std::cout << "animal(int) ";
}

Animal::~Animal()
{
std::cout << "Destroyed an animal...";
}


//Main.cpp




#include <iostream>
#include "Animal.h"
#include "Array.h"

std::ostream& operator<<(std::ostream&, const Animal&);
void IntFillFunction(Array<int>& theArray);
void AnimalFillFunction(Array<Animal>& theArray);

int main()
{
Array<int> intArray;
Array<Animal> animalArray;
IntFillFunction(intArray);
AnimalFillFunction(animalArray);
std::cout << "intArray...\n" << intArray;
std::cout << "\nanimalArray...\n" << animalArray << std::endl;

std::cin.get();

return 0;
}

std::ostream& operator<<(std::ostream& theStream, const Animal& theAnimal)
{
theStream << theAnimal.GetWeight();
return theStream;
}

void IntFillFunction(Array<int>& theArray)
{
bool Stop = false;
int offset, value;
while (!Stop)
{
std::cout << "Enter an offset (0-9) and a value. ";
std::cout << "(-1 to stop): ";
std::cin >> offset >> value;
if (offset < 0)
break;
if (offset > 9)
{
std::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;
}
}


I've tried my best to keep this question on topic by providing you with the desired behavior of the program, a specific problem or error and the shortest code necessary to reproduce it. All of which are requirements for debugging help as documented in the Help Center page. I'm new to this website, and if my question is still off-topic, please let me know how I can change my question to be more on topic or inform me of a better place to ask my question. - Thank you


Aucun commentaire:

Enregistrer un commentaire