对于赋值的两个问题,首先是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;
}
当然,编译错误:
08\projects\c4\c4_1b_jwong\c4_1b_jwong\charcomparator.h(5) : error C2143: syntax error : missing ';' before '<'
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\charcomparator.h(5) : error C2988: unrecognizable template declaration/definition
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\charcomparator.h(5) : error C2059: syntax error : '<'
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(22) : error C2065: 'MAX' : undeclared identifier
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(22) : error C2078: too many initializers
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(26) : error C2065: 'MAX' : undeclared identifier
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(28) : error C2065: 'MAX' : undeclared identifier
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(37) : error C2065: 'MAX' : undeclared identifier
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(37) : error C2078: too many initializers
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(41) : error C2065: 'MAX' : undeclared identifier
1>c:\users\jon\documents\visual studio 2008\projects\c4\c4_1b_jwong\c4_1b_jwong\main.cpp(43) : error C2065: 'MAX' : undeclared identifier
1>Build log was saved at "file://c:\Users\jon\Documents\Visual Studio 2008\Projects\C4\C4_1b_JWong\C4_1b_JWong\Debug\BuildLog.htm"
1>C4_1b_JWong - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
有什么想法?谢谢,对不起这样的noob问题。
答案 0 :(得分:2)
如果你想专门化一个模板函数,你需要至少先声明原始模板,即:
template<class T> int my_comp<T>(T a, T b);
// ... now you can specialize my_comp()
专业化是为通用模板的“特殊情况”提供实现,请参阅例如C++ FAQ lite 35.7。正如詹姆斯指出的那样,模板专业化有一些复杂性需要注意this article中描述的Sutter。