problem with specialized template class with non-type parameter

I am trying to do a specialized template class of char with non-type parameter. However, it doesn't accept the specialized one and gives error message:

error C2146: syntax error : missing ',' before identifier 'N'

on line 31

Any idea how I can do it? Thanks.

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
57
58
59
60
61
62
63
64
65
66
67
// program testing

#include <iostream>

using namespace std;


// template class Container to contain array of type T with array size N
template <typename T, int N>
class Container {
private:
	T v[N];

public:
	void setMember(int pos, T value);
	T getMember(int n);
};

template <typename T, int N>
void Container<T, N>::setMember(int pos, T value) {
	v[pos] = value;
}

template <typename T, int N>
T Container<T, N>::getMember(int pos) {
	return v[pos];
}


// specialized template class Container to contain array of type char with array size N
template <char, int N>
class Container<char, int N> {
private:
	char v[N];

public:
	void setMember(int pos, char c);
	char getMember(int pos);
	char getUpperMember(int pos);
};

template <char, int N>
void Container<char, int N>::setMember(int pos, char c) {
	v[pos] = value;
}

template <char, int N>
char Container<char, int N>::getMember(int pos) {
	return v[pos];
}

template <char, int N>
char Container<char, int N>::getUpperMember(int pos) {
	return (v[pos] >= 'a' && v[pos] <= 'z')? v[pos] - ('a' - 'A'): v[pos];
}


// mains
int main() {
	Container<int, 10> container1;

	container1.setMember(5, 50);

	cout << container1.getMember(5) << endl;

	return 0;
}
I think
1
2
template <char, int N>
void Container<char, int N>::setMember(int pos, char c) 

should just be
1
2
template <char, int N>
void Container<char, N>::setMember(int pos, char c) 

but I've never used specialized templates
Last edited on
Gumbercules, I have tried your suggestion but it gives the following complaint on line 40:

error C3855: 'Container': template parameter 'T' is incompatible with the declaration
Try with
1
2
template <int N>
void Container<char, N>::setMember(int pos, char c)

Last edited on
still the error message

syntax error : missing ',' before identifier 'N'

on line 32
it compiled with Bazzy's instruction for me. Check to make sure you didn't miss one.
tried it. Same original error message. The thing is I have generic template class <typename T, int N> and I am declaring a specialized template class <int N>.

Is this allowed? The program runs if I have only one of them.
Invert the order of template parameters. N followed by T

You have to default the rightmost one, not the leftmost one, just like default parameters on functions.
I have tried N followed by T now but it says

error C2975: 'Container' : invalid template argument for 'N', expected compile-time constant expression

on line 32.

I have also tried the type of the specialized one to be <N> and <char, int N> respectively but still doesn't run.

My code now becomes:

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
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace std;


// template class Container to contain array of type T with array size N
template <int N, typename T>
class Container {
private:
	T v[N];

public:
	void setMember(int pos, T value);
	T getMember(int n);
};

template <int N, typename T>
void Container<N, T>::setMember(int pos, T value) {
	v[pos] = value;
}

template <int N, typename T>
T Container<N, T>::getMember(int pos) {
	return v[pos];
}


// specialized template class Container to contain array of type char with array size N
template <char, int N>
class Container<char, N> {
private:
	char v[N];

public:
	void setMember(int pos, char c);
	char getMember(int pos);
	char getUpperMember(int pos);
};

template <int N>
void Container<char, int N>::setMember(int pos, char c) {
	v[pos] = value;
}

template <int N>
char Container<char, int N>::getMember(int pos) {
	return v[pos];
}

template <int N>
char Container<char, int N>::getUpperMember(int pos) {
	return (v[pos] >= 'a' && v[pos] <= 'z')? v[pos] - ('a' - 'A'): v[pos];
}


// mains
int main() {
	Container<int, 10> container1;

	container1.setMember(5, 50);

	cout << container1.getMember(5) << endl;

	return 0;
}


Any other suggestion?
My suggestion:

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
57
58
59
60
// template class Container to contain array of type T with array size N
template <int N, typename T>
class Container {
private:
	T v[N];

public:
	void setMember(int pos, T value);
	T getMember(int n);
};

template <int N, typename T>
void Container<N, T>::setMember(int pos, T value) {
	v[pos] = value;
}

template <int N, typename T>
T Container<N, T>::getMember(int pos) {
	return v[pos];
}


// specialized template class Container to contain array of type char with array size N
template <int N>
class Container<N, char> {
private:
	char v[N];

public:
	void setMember(int pos, char c);
	char getMember(int pos);
	char getUpperMember(int pos);
};

template <int N>
void Container<N, char>::setMember(int pos, char c) {
	v[pos] = value;
}

template <int N>
char Container<N, char>::getMember(int pos) {
	return v[pos];
}

template <int N>
char Container<N, char>::getUpperMember(int pos) {
	return (v[pos] >= 'a' && v[pos] <= 'z')? v[pos] - ('a' - 'A'): v[pos];
}


// mains
int main() {
	Container<10, int> container1;

	container1.setMember(5, 50);

	cout << container1.getMember(5) << endl;

	return 0;
}
Topic archived. No new replies allowed.