Two questions for an assignment, first is the first set of errors for my template in a the CharComparator.h file, I don't see why it is complaining. The second question is why that I added include "CharComparator.h" to my main.cpp file, I get a bunch more of compiler errors. Here are my files:
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; } And of course, compiler errors:
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 ========== Any thoughts? Thanks, and sorry for such noob quesitons.
my_comppreviously declared as a template? If not, then you should just declare it as an ordinary function not as a template specialization.