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.
You don't need <int>
to call the function in this case. (Thinking about std::vector<int>
, you will write hoge.push_back(0)
instead of hoge.push_back<int>(0)
.)
The arguments of Array::initArray
and Array:;:printArray
are also invalid and unneeded.
Fixed code (emits no compile errors/warnings, but not working properly):
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
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(){
for (int i = 0; i < this->size; ++i){
this->data[i] = i;
}
};
void printArray(){
ostringstream oss;
string answer = "";
for (int i = 0; i < this->size; ++i){
oss << data[i] + " ";
}
answer = oss.str();
cout << answer << endl;
};
private:
int size;
T* data;
};
int main(){
Array<int> *testArray = new Array<int>(5);
testArray->initArray();
testArray->printArray();
return 0;
}
Here is the changed working code:
#include <string>
#include <iostream>
#include <cstdint>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
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(Array<T>* arrayToInit){
for (int i = 0; i < this->size; ++i){
this->data[i] = i;
}
}
void printArray(Array<T>* arrayToPrint){
ostringstream oss;
string answer = "";
//for (int i = 0; i < arrayToPrint->size; ++i){
// oss << data[i] + " ";
answer = oss.str();
cout << answer << endl;
}
private:
int size;
T* data;
};
int main()
{
string s;
double d =9.0;
s=d;
Array<int> *testArray = new Array<int>(5);
testArray->initArray(testArray);
testArray->printArray(testArray);
return 0;
}
Changes carried out:
Template Instantiation happens when you declare the class.No need to again do it for its member functions.
Also no need to append ; after every member function definition, only needed for class definition.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.