template could not be deduced
I am writing a program where I read text file and input the lines into arrays.
After that I have to find largest value in each array.
I am using template and I get this error:
'MyNewDataType maxValue(MyNewDataType [])' : could not deduce template argument for 'MyNewDataType []' from 'int'
I searched for this error but I still don't get what's wrong with my code :/
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 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
template <typename MyNewDataType>
MyNewDataType maxValue(MyNewDataType arr[])
{
MyNewDataType maxValue = 0;
MyNewDataType temp = 0;
for (int i = 0; i < 5; i++)
{
if (a[i] > temp)
{
temp = a[i];
}
}
cout << "The largest value in this array is: " << temp;
}
int main()
{
//arrays
int intarray[5];
float floatarry[5];
string stringarray[5];
//read file and insert into arrays
string line;
ifstream myfile("data.txt");
if (myfile.is_open())
{
myfile >> intarray[0] >> intarray[1] >> intarray[2] >> intarray[3] >> intarray[4];
myfile >> floatarry[0] >> floatarry[1] >> floatarry[2] >> floatarry[3] >> floatarry[4];
myfile >> stringarray[0] >> stringarray[1] >> stringarray[2] >> stringarray[3] >> stringarray[4];
}
//print largest value
int z; //largest value in int array
maxValue(intarray[5]) = z;
cout << z << endl;
system("Pause");
return 0;
}
|
Last edited on
floatarry is the array.
floatarry[5] is the sixth element of the array.
I fixed the errors, but my code just breaks when it reaches line 51.
Also, it prints the largest value in intarray as 130, the answer should be 13.
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
template <typename MyNewDataType>
MyNewDataType maxValue(MyNewDataType arr[])
{
MyNewDataType maxValue = 0;
MyNewDataType temp = 0;
for (int i = 0; i < 5; i++)
{
if (arr[i] > temp)
{
temp = arr[i];
}
}
cout << "The largest value in this array is: " << temp;
return 0;
}
int main()
{
//arrays
int intarray[5];
float floatarry[5];
string stringarray[5];
//read file and insert into arrays
string line;
ifstream myfile("data.txt");
if (myfile.is_open())
{
myfile >> intarray[0] >> intarray[1] >> intarray[2] >> intarray[3] >> intarray[4];
myfile >> floatarry[0] >> floatarry[1] >> floatarry[2] >> floatarry[3] >> floatarry[4];
myfile >> stringarray[0] >> stringarray[1] >> stringarray[2] >> stringarray[3] >> stringarray[4];
}
int z;
z = maxValue(intarray);
cout << z << endl;
float i;
i = maxValue(floatarry);
cout << i << endl;
string e;
e = maxValue(stringarray);
cout << e << endl;
system("Pause");
return 0;
}
|
Last edited on
1 2
|
maxValue(intarray) = z;
///change to z=maxValue (intarray);
|
line 42 you are assigning to a function wow .
Plus your funct should be rreturning temp on line 21;
Last edited on
@andy1992,
yeah I fixed that.
If I use void, I get errors.
"a value of type void cannot be assigned to an entity of type int"
Oh I re-edited the post
Thing is you shouldn't change the return type but your funct should return temp on line 21
Topic archived. No new replies allowed.