The other file given is the implantation of the first file. This one is called "arrayi.h"
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
|
//cs132 demo/lab streller
//arrayi.h
//simple array class
#ifndef ARRAYI_H_
#define ARRAYI_H_
#include <iostream>
#include <cstdlib>
#include <cassert>
using namespace std;
class Array
{
friend ostream &operator<< <> (ostream &output, const Array<T> &a);
friend istream &operator>> <>(istream &input, Array<T> &a);
public:
Array(int = 10); //constructor
Array(const Array &); //copy constructor
~Array(); //destructor
int getSize() const; //return size
Array &operator = (const Array &) ;
int operator==(const Array &) const;
int operator != (const Array &) const;
int &operator[] (int);
Array operator + (const Array&);
static int getArrayCount(); //get count of existing
//array objects
private:
int *ptr; //ptr to first array element
int size; //size of the array
static int arrayCount; // #of arrays instantiated
};
#endif
|
I was also told to insert 3 templates into my header file:
1 2 3 4 5
|
template<typename T> class Array
template<typename T>
ostream &operator << (ostream& output, const Array<T> &a);
template<typename T>
istream &operator >> (istream& input, Array<T> &a);
|
When I asked what they where for I was told that "they convince the compiler that the 'friends' are templates" and they "need to be inserted into the .h file." I think I know what this means, but I'm not too sure.
So you might be thinking at this point "this guy just wants us to do his homework." Well, I want follow the rules here and I don't want to be lazy, I'm really stuck.
So far I created the project, added in the 2 files given, I created the driver .cpp file that includes the main and a ".t" file for the templates.
My .cpp that has the main in it is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
//========================================================
// Name: (edited out)
// Class: CS132
// Program name: lab2_client_driver.cpp
// Date created: 9/12/2011
// Date last modified:
// Other files included: arrayi.h, arrayi.t, arrayi.cpp
//========================================================
#include <iostream>
#include "arrayi.h"
using namespace std;
int main()
{
return 0;
}
|
And my .t (template) file is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//======================================================
// Name: (edited out)
// Template Name: arrayi.t
// Purpose: provide a template to lab2_client_driver.cpp
// so that elements of the arrays can be either
// floats, ints or doubles.
// Date Created: 9/12/2011
// Date last modified:
//======================================================
#ifndef ARRAYI_T_
#define ARRAYI_T_
#include <iostream>
#endif
|
I follow the link structure I was given at the beginning of the semester. It goes like this:
"driver with main has #include arrayi.h in it," "arrayi.h has #include arrayi.t at the bottom" and "arrayi.cpp has #include arrayi.h in it."
I have all these files in the same project so I know they see each other and there are no errors that say anything to the extend of "file missing" or "invalid file location."
Most of my errors right now are in the "arrayi.cpp" file. They say things like "member function not declared in Array" and other stuff like that. Now I know I have to create templates for every element in the class Array, and I've been tying different templates in the .t file and seeing what works and what doesn't and so far nothing works. I follow examples in my text book and examples I find online of how a template should be set up and I get nothing but errors.
Here are some things I have tried from the text book for the constructor of class Array:(I did modify it in an attempt to get it to work with my project)
1 2 3 4 5 6
|
template <typename T>
class Array
{
Array(int)
}
|
Other than that I really have no clue where else to go.
All help is greatly appreciated
Thanks!