gpt4 book ai didi

c++ - 模板语法帮助

转载 作者:行者123 更新时间:2023-11-30 04:38:14 27 4
gpt4 key购买 nike

作业的两个问题,第一个是 CharComparator.h 文件中我的模板的第一组错误,我不明白它为什么会提示。第二个问题是为什么我在我的 main.cpp 文件中添加了包含“CharComparator.h”,我得到了更多的编译器错误。这是我的文件:

字符比较器.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

主要.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 ==========

有什么想法吗?谢谢,很抱歉提出这样的菜鸟问题。

最佳答案

如果你想特化一个模板函数,你至少需要先声明原始模板,即:

template<class T> int my_comp<T>(T a, T b);

// ... now you can specialize my_comp()

特化是为通用模板的“特殊情况”提供实现,参见例如C++ FAQ lite 35.7 .正如 James 指出的那样,模板特化有一些复杂性,需要注意 Sutter 在 this article 中描述的内容。 .

关于c++ - 模板语法帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3278063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com