Hi, I am learning how to use template for the first time so that this question might seem stupid for some of you.
Please bear with me because I can't find the solution to this problem anywhere else.
First, I want to ask if it is technically valid to create an array of string?
In my textbook, they only talk about array of numbers and characters.
I tried the array of strings alone in my Xcode compiler and it worked fine.
Now, if the usage of array with strings is technically correct, then my problem would be with template.
The short description of the problem is:
When I used template function with array, it didn't work with C++ build-in functions from the library <array>.
My Xcode compiler is returning the error whenever it used it.
At the same time, (I think it's a separate problem) it turned out I couldn't also pass the array to that function, which was giving me an error.
I put that template function in a separate file with header file and accessed from main file. But, I tried with everything in the main file and it still didn't work. That main file is pasted below.
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
|
#include <iostream>
#include <array>
#include <string>
using namespace std;
template <class T, class U>
bool fine(T toCheck, U OK[], U notOK[])
{
if(!OK.empty())
{
for(int i = 0; i < OK.size(); i++)
{
if(toCheck == OK[i])
return false;
}
}
if(!notOK.empty())
{
int tracker = 0;
for(int j = 0; j < notOK.size(); j++)
{
if(toCheck != notOK[j])
tracker++;
}
if(tracker == notOK.size())
return false;
}
return true;
}
int main()
{
char char2Check = 'x';
char OKchar[] = {};
char notOKchar[] = {'|', '-', '_'};
if(fine(char2Check, OKchar[], notOKchar[]))
cout << "Correct Char!" << endl;
else
cout << "Wrong Char!" << endl;
string str2Check = "Comp";
string OKstr[] = {};
string notOKstr[] = {"Computer", "computer", "Comp", "comp"};
if(fine(str2Check, OKstr[], notOKstr[]))
cout << "Correct Str!" << endl;
else
cout << "Wrong Str!" << endl;
return 0;
}
|
I built the above code. I also ran the code directly. Both times, it gave the same error.
Wherever I used functions from <array> library, it gave the error below:
Member reference base type 'U *' is not a structure or union |
.
And, when I called the template function, it gave the error below:
I got the above errors from when I used Xcode Version 7.3 (7D175).
When I ran it here on the website by clicking the wedge icon at the corner of code block, it didn't give the first error type, but only the second with different error message.
My purpose with the above template function is to write a general function to check user input whether it is string or char with custom specification (valid or not which will be set by programmer, of course).
I am trying to simplify things using template but am I making it more complicated?
Is there a better way to do it? If so, I would appreciate some suggestions.
Meanwhile, as a fresh learner of template for C++, I would like to know if I am understanding anything wrong with this self-study.
P.S.
I edited the original post as an attempt to simplify it.
Everything remains the same though.
Please feel free to ask me anything that is not clear above.
This is my first time posting here, so...
Thank you for reading, you all!