国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

c++ - 函數(shù)模板具體化 已調(diào)試1小時(shí),什么手段都試了,標(biāo)準(zhǔn)答案都對(duì)過(guò)了,就是無(wú)法編譯
高洛峰
高洛峰 2017-05-16 13:27:45
0
3
801

做的是c++ primer plus上的第八章 第6題
要求做2個(gè)函數(shù)模板,分別對(duì)int double數(shù)組,還有一個(gè)模板具體化用char*[]
int 和double的是沒(méi)問(wèn)題的, 具體化的那個(gè),我弄了好久.不知道為什么不能編譯.
然后我用普通函數(shù)寫了一遍,發(fā)現(xiàn)能正常運(yùn)行.然后又搞了半天還是不行就上網(wǎng)找答案,發(fā)現(xiàn)我跟答案
寫的格式基本一樣. 就是不能編譯

報(bào)錯(cuò)信息:main.cpp:43:23: 錯(cuò)誤:‘const char maxn(const char, int)’的模板標(biāo)識(shí)符‘maxn<const char []>’不匹配任何模板聲明 template<>const char maxn<const char[]>(const char* x[], int n)

代碼:

#include <cstring>
#include <iostream>

using namespace std;

template<typename T>T maxn(T x, int n);
template<>const char* maxn<const char*[]>(const char* x[], int n);

/*
 * 模板函數(shù)針對(duì)不同類型數(shù)組 輸出數(shù)組中最大的那個(gè)值
 */
int main(int argc, char** argv) {
    int ix[6] = {34, 12, 343, 1, 43, 31};
    double dx[4] = {1.34, 1231.2, 34.3, 44.23};
    const char* sx[5] = {"abcde", "fedcba", "abcdefg", "12345", "qwert"};
    cout << "ix[6] max: " << *maxn(ix, sizeof ix / sizeof ix[0]) << endl;
    cout << "dx[4] max: " << *maxn(dx, sizeof dx / sizeof dx[0]) << endl;
    cout << "sx[5] max_address: " << (int*) maxn(sx, 5) << endl;
    cout << "Press Enter To Exit..." << endl;
    cin.get();
    return 0;
}
//這個(gè)是我自己寫的普通函數(shù),能運(yùn)行
//const char* maxn(const char* x[5],int n) {
//    const char* max_len = x[0];
//    for (int i = 0; i < n-1; i++) {
//        max_len = strlen(max_len) >= strlen(x[i + 1]) ? max_len : x[i + 1];
//    }
//    return max_len;
//}

template<>const char* maxn<const char*[]>(const char* x[], int n)
{
    const char* max_len = x[0];
    for (int i = 0; i < n - 1; i++) {
        max_len = strlen(max_len) >= strlen(x[i + 1]) ? max_len : x[i + 1];
    }
    return max_len;
}

template<typename T>
T maxn(T x, int n) {
    for (int i = 0; i < n - 1; i++) {
        x[i + 1] = x[i] > x[i + 1] ? x[i] : x[i + 1];
    }
    return x + n - 1;
}
高洛峰
高洛峰

擁有18年軟件開發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級(jí)軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...

全部回復(fù)(3)
洪濤

聲明:template<> const char **maxn(const char **x, int n);

調(diào)用:cout << "sx[5] max_address: " << (int*)*maxn(sx, 5) << endl;

定義:

template<>
const char **maxn(const char **x, int n)
{
    const char **max_len = x;
    for (int i = 0; i < n - 1; i++)
        max_len = strlen(*max_len) >= strlen(x[i + 1]) ? max_len : &x[i + 1];
    return max_len;
}

你犯了三個(gè)錯(cuò)誤:

  • 特化語(yǔ)法不對(duì)

  • 特化時(shí)兩處T的類型不一致

  • 多處指針沒(méi)有解引用


思路:

模板聲明:template<typename T> T maxn(T x, int n);
調(diào)用:maxn(sx, 5)。其中變量sx的類型是const char *[5],即數(shù)組類型。

由于sx是數(shù)組類型,模板形式參數(shù)的形式是T,這里sx會(huì)被隱式轉(zhuǎn)換成指針類型(array to pointer conversion)const char **,即T是const char **。

所以特化應(yīng)當(dāng)是
template<> const char **maxn(const char **x, int n);

PS:直覺(jué)上應(yīng)該還可以有個(gè)引用版本的特化。但是因?yàn)門推導(dǎo)不出引用類型,所以這里不會(huì)調(diào)用引用版本的特化。當(dāng)然可以提供模板參數(shù),來(lái)調(diào)用這個(gè)特化。

洪濤

按我理解,maxn這個(gè)函數(shù)應(yīng)該是想返回長(zhǎng)度為n的數(shù)組中“最大”的那個(gè)元素,所以我覺(jué)得函數(shù)聲明應(yīng)該寫成這樣:

template<typename T>T maxn(T x[], int n);
template<>const char* maxn<const char*>(const char* x[], int n);

按這個(gè)聲明,把你的實(shí)現(xiàn)改一下,就可以通過(guò)編譯了。
以下代碼是在你原來(lái)代碼基礎(chǔ)上做了簡(jiǎn)單修改,在vs2017中編譯通過(guò),供參考:

template<typename T>T maxn(const T x[], int n);
template<>const char* maxn<const char*>(const char* const x[], int n);

template<>const char* maxn<const char*>(const char* const x[], int n)
{
    const char* max_len = x[0];
    for (int i = 0; i < n - 1; i++) {
        max_len = strlen(max_len) >= strlen(x[i + 1]) ? max_len : x[i + 1];
    }
    return max_len;
}

template<typename T> T maxn(const T x[], int n) {
    for (int i = 0; i < n - 1; i++) {
        x[i + 1] = x[i] > x[i + 1] ? x[i] : x[i + 1];
    }
    return x[n - 1];
}
漂亮男人

特化版本你寫錯(cuò)了。

template<>const char* maxn<const char*[]>(const char* x[], int n);

正確姿勢(shì):

const char* maxn(const char* x[], int n);
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板