When trying to compile the following code:
int main(){
Array<int> *testArray = new Array<int>(5);
testArray->initArray<int>(testArray);
testArray->printArray<int>(testArray);
return 0;
}
Using this template:
template<typename T>
class Array{
public:
Array(int size){
size = size;
data = new T[size];
};
Array<T> addData(T dataToAdd){
Array <T> *tmp = new Array <T> (this->size);
tmp = this->data;
Array <T> *newData = new Array<T> (this->size + 1);
for (int i = 0; i < this->size + 1; ++i){
if (i < this->size){
newData->data[i] = tmp[i];
}
else{
newData->data[i] = dataToAdd;
}
}
return newData;
};
void initArray(T arrayToInit){
for (int i = 0; i < this->size; ++i){
this->data[i] = i;
}
};
void printArray(T arrayToPrint){
ostringstream oss;
string answer = "";
for (int i = 0; i < arrayToPrint->size; ++i){
oss << arrayToPrint[i] + " ";
}
answer = oss.str();
cout << answer << endl;
};
private:
int size;
T* data;
};
I get the following error in my int main() :
"expected primary-expression before ‘int’"
for both of my function calls (initArray and printArray). I am fairly new to C++ and have little experience with templates in particular, any advice would be greatly appreciated. Thank you for your time.
EDIT: Thank you everyone for the responses and constructive criticism, I have made a great amount of progress thanks to everybody's help.