模板语法帮助
分配的两个问题,第一个是CharComparator.h文件中我的模板的第一组错误,我不明白为什么它会抱怨。第二个问题是为什么我在main.cpp文件中添加了“CharComparator.h”,我得到了更多的编译器错误。这是我的文件:
CharComparator.h
#ifndef CHARCOMPARATOR_H
#define CHARCOMPARATOR_H
template <>
int my_comp<const char*>(const char *a, const char *b) {
return std::strcmp(a, b) < 0;
}
#endif
main.cpp中
// Reduce Template Assignment
#include <iostream>
#include <algorithm>
using namespace std;
#include "CharComparator.h"
// definition of global varible
const int MAX = 10;
// declaration of template functions
template <class T>
int reduce(T array[], int size);
template <class T>
void show(const T array[], int size);
// Main program for testing
int main() {
// test using long instantiation
long nonUniqueArray[MAX] = {12, 12 ,5, 6, 11, 5, 6, 77, 11, 12};
// show non-unique array
cout << "old array with non-unique elements: " << endl;
show(nonUniqueArray, MAX);
int newsize = reduce(nonUniqueArray, MAX);
// now non-unique array becomes unique
cout << "new array has only unique elements: " << endl;
show(nonUniqueArray, newsize);
cout << "size reduced to " << newsize << endl;
cout << endl;
// test using string instantiation
const char* strArray[MAX] = {"aa", "bb", "bc", "ca", "bc", "aa", "cc", "cd", "ca", "bb"};
//aa bb bc ca cc cd
// show non-unique array
cout << "string array with non-unique elements: " << endl;
show(strArray, MAX);
newsize = reduce(strArray, MAX);
// now non-unique array becomes unique
cout << "string array has only unique elements: " << endl;
show(strArray, newsize);
cout << "size reduced to " << newsize << endl;
return (0);
}
// reduce the non-unique array to unique array, return new size
template <class T>
int reduce(T array[], int size) {
// CODE UP A REDUCE TEMPLATE HERE
T *begin = array;
T *end = array + size;
sort(begin, end);
T *end_new = unique(begin, end);
return end_new - array;
}
// show the array element
template <class T>
void show(const T array[], int size) {
for (int i = 0; i < size; i++) {
cout << array[i] << ' ';
}
cout << endl;
}
当然,编译器错误:
08projectsc4c4_1b_jwongc4_1b_jwongcharcomparator.h(5) : error C2143: syntax error : missing ';' before '<'
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongcharcomparator.h(5) : error C2988: unrecognizable template declaration/definition
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongcharcomparator.h(5) : error C2059: syntax error : '<'
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(22) : error C2065: 'MAX' : undeclared identifier
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(22) : error C2078: too many initializers
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(26) : error C2065: 'MAX' : undeclared identifier
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(28) : error C2065: 'MAX' : undeclared identifier
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(37) : error C2065: 'MAX' : undeclared identifier
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(37) : error C2078: too many initializers
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(41) : error C2065: 'MAX' : undeclared identifier
1>c:usersjondocumentsvisual studio 2008projectsc4c4_1b_jwongc4_1b_jwongmain.cpp(43) : error C2065: 'MAX' : undeclared identifier
1>Build log was saved at "file://c:UsersjonDocumentsVisual Studio 2008ProjectsC4C4_1b_JWongC4_1b_JWongDebugBuildLog.htm"
1>C4_1b_JWong - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
有什么想法吗?谢谢,对不起这样的noob问题。
没有找到相关结果
已邀请:
1 个回复
念炯
专业化用于提供通用模板的“特殊情况”的实现,参见例如C ++ FAQ lite 35.7。正如詹姆斯指出的那样,模板专业化有一些复杂性需要注意本文中描述的Sutter。