Array Perimeter for Template with Array Library Function

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:
Expected expression


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!
Last edited on
There's technically nothing wrong with your template function; can you show the us the code that you call this function with?
Hi, Little Captain, first of all, thank you very much for the reply. I thought my question is too dumb that I was not going to get any replies.

I replaced the code's variables for better readability and I added and combined my template function with the main function in one code snippet which I tried and apparently didn't work.

I reworded the post for easier understanding of my problem. I dearly hope to hear back any feedback soon. Thanks again for your time.
char OKchar[] = {}; is illegal. You must supply a non-zero size or a non-empty initializer list. If your compiler has an extension enabled that allows this, then indexing this array will result in bad things happening.

The way you use arrays in the context of a function call is like this:
fine(char2Check, OKchar, notOKchar), not fine(char2Check, OKchar[], notOKchar[])

string OKstr [] = {}; also illegal.

In your fine function, both OK and notOK are arrays of U. They are not standard containers with size and empty methods.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>
#include <iterator>

template < typename T, typename SEQ1, typename SEQ2 > bool fine( const T& v, const SEQ1& ok, const SEQ2& not_ok )
{
    // https://msdn.microsoft.com/en-us/library/dd293608.aspx
    const auto equal = [&v]( const auto& e ) { return e == v ; } ;

    // http://en.cppreference.com/w/cpp/iterator/begin
    // http://en.cppreference.com/w/cpp/algorithm/all_any_none_of
    return std::none_of( std::begin(ok), std::end(ok), equal ) &&
           std::any_of( std::begin(not_ok), std::end(not_ok), equal ) ;
}

http://coliru.stacked-crooked.com/a/952057b6260a46ee
cire and JLBorges, thank you. I am going to learn more about the template in depth and get back here soon.
Topic archived. No new replies allowed.