Hi guys, I'm trying to write a simple class using templates, but it's not really working out so well. I wonder if someone could tell me where I'm going wrong?
#include <cmath>
//CONSTRUCTORS
//Default constructor, creates an empty array
template <class T>
MyArray<T>::MyArray()
{
_size = 0;
_capacity = 1;
_data = NULL; //empty array, contains nothing
}
//Creates an array with minimum capacity with value in the first cell
template <class T>
MyArray<T>::MyArray( const T& value )
{
_size = 1;
_capacity = 1;
T* new_array = new T[_capacity];
_data = new_array;
_data[0] = value;
}
//Creates an array with size sz, a large enough capacity, then put value in all the cells up to sz.
template <class T>
MyArray<T>::MyArray( int sz, const T& value )
{
_size = sz;
_capacity = (int)pow(2,(double)ceil(log((double)_size)/log(2.0))+1);
T* new_array = new T[capacity];
_data = new_array;
for(int i = 0; i != _size; ++i)
{
_data[i] = value;
}
}
//Copy constructor
template <class T>
MyArray<T>::MyArray( MyArray<T>& x)
{
_size = x.size;
_capacity = x.capacity;
T* new_array = new T[capacity];
for(int i = 0; i != _size; ++i)
{
new_array[i] = x._data[i];
}
}
//do this later
template <class T>
MyArray<T>::MyArray( MyArrayPointer<T> first, MyArrayPointer<T> last )
{
}
//DESTRUCTOR
template <class T>
MyArray<T>::~MyArray()
{
delete [] _data;
}
//OPERATOR OVERLOAD
template <class T>
MyArray::MyArray<T>& operator= (const MyArray<T>& x)
{
//do later
}
template <class T>
T& MyArray<T>::operator[](int index) const
{
return _data[index];
}
Here are the error messages:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
**** Build of configuration Debug for project PA2 ****
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oMyArray.o ..\MyArray.cpp
..\MyArray.cpp:7:1: error: 'MyArray' does not name a type
..\MyArray.cpp:16:1: error: expected unqualified-id before 'template'
..\MyArray.cpp:29:1: error: 'MyArray' does not name a type
..\MyArray.cpp:44:1: error: expected unqualified-id before 'template'
..\MyArray.cpp:60:1: error: 'MyArray' does not name a type
..\MyArray.cpp:66:1: error: expected unqualified-id before 'template'
..\MyArray.cpp:74:1: error: 'MyArray' does not name a type
..\MyArray.cpp:79:1: error: expected unqualified-id before 'template'
Build error occurred, build is stopped
Time consumed: 138 ms.
Using the Eclipse Helios IDE with g++ on Win7.
I'm totally stuck. I would really appreciate someone's help on this. Thanks in advance!
Ah, sorry. I was supposed to be implementing MyArray.cpp, MyArray.h is already given. I only just started, so I cut out the parts that I hadn't started yet. I accidentally missed out a brace and forgot to remove the parts wth MyArrayPointer, which isn't done yet.
He has another issue here because in his h file he has an #include "MyArray.cpp" directive
but he(or his IDE/project management software) is also trying to compile MyArray.cpp
as a seperate file as well.
Hi, I'm not very familiar with the Eclipse IDE, so I'm not sure how to change that.
However, I did try compiling just with this line: g++ main.cpp
And I still get this error:
1 2
..\MyArray.cpp:74:1: expected unqualified-id before 'template
..\MyArray.cpp:79:1: error: 'MyArray' does not name a type
Well if your assignment says that the MyArray should be in two parts (h file and cpp file) in the way that you have them, and you have them in an eclipse project -
then you can exclude the MyArray.cpp file from the build process -
you do this by right-click on MyArray.cpp file in the project explorer - select properties - and make sure the exclude resource from build checkbox is ticked (on the the C/C++ Build ->Tool Chain Editor page).
That should stop Eclipse from trying to compile MyArray.cpp as a seperate file..
Also make sure none of your other files (except MyArray.h) include MyArray.cpp.
(They should include MyArray.h).
Should work - I just tried it.
That should just leave you with the C++ syntax and other errors in the code you posted.
Wow! thanks!
Well, for some reason Eclipse is bunging up on me right now (it generally does) but once I figure out what's wrong I'm sure that'll fix it! Thanks!