Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 932177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:37:25+00:00 2026-05-15T20:37:25+00:00

Two questions for an assignment, first is the first set of errors for my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:37:25+00:00Added an answer on May 15, 2026 at 8:37 pm

    If you want to specialize a template function, you need to have at least declared the original template first, i.e.:

    template<class T> int my_comp<T>(T a, T b);
    
    // ... now you can specialize my_comp()
    

    Specializations are for providing implementations for “special cases” of a generic template, see e.g. C++ FAQ lite 35.7. As James points out there are some intricacies to template specialization to be aware of that Sutter described in this article.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.