亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5600 | 回復: 9
打印 上一主題 下一主題

[C++] 類成員函數(shù)模板特化 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2013-08-19 16:01 |只看該作者 |倒序瀏覽
本帖最后由 Redshadows 于 2013-08-19 16:33 編輯

test.h

  1. #pragma once
  2. namespace TT
  3. {

  4. struct No
  5. {
  6.         int i;       
  7. };

  8. struct Test;

  9. void nothing(Test &test);

  10. struct Test
  11. {
  12.         friend void nothing(Test &test);
  13.         template <typename T> void show(const T& tt);
  14. //        template <> void show(const int& tt);
  15.         void show1();
  16. };
  17. }
復制代碼
test.cpp
  1. namespace TT;

  2. template<typename T>
  3. void Test::show(const T& tt)
  4. {
  5.         cout << "test" << endl;
  6. }

  7. template<>
  8. void TT::Test::show(const TT::No& tt)
  9. {
  10.         cout << "haha" << endl;
  11. }

  12. void Test::show1()
  13. {
  14.         show(1);
  15. }
復制代碼
main.cpp
  1. #include "test.h"

  2. int main()
  3. {
  4.         TT::Test test;
  5.         int a = 0;
  6.         test.show1();
  7.         nothing(test);
  8.         return 0;
  9. }
復制代碼
編譯后提示
test.cpp:14: error: specialization of ‘template<class T> void TT::Test::show(const T&)’ in different namespace
test.h:17: error:   from definition of ‘template<class T> void TT::Test::show(const T&)’
test.cpp:16: confused by earlier errors, bailing out
Preprocessed source stored into /tmp/ccDOpYyR.out file, please attach this to your bugreport.
Traceback (most recent call last):
  File "/usr/share/apport/gcc_ice_hook", line 34, in <module>
    pr.write(open(apport.fileutils.make_report_path(pr), 'w'))
IOError: [Errno 13] Permission denied: '/var/crash/_usr_lib_gcc_i486-linux-gnu_4.3.3_cc1plus.1000.crash'

求指點。

論壇徽章:
4
雙子座
日期:2014-08-28 10:08:002015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:112015年亞洲杯之阿聯(lián)酋
日期:2015-03-13 03:25:15
2 [報告]
發(fā)表于 2013-08-19 16:46 |只看該作者
template<>
void TT::Test::show(const TT::No& tt)
{
        cout << "haha" << endl;
}

這個函數(shù)是什么意思?  刪掉試試

論壇徽章:
0
3 [報告]
發(fā)表于 2013-08-19 16:58 |只看該作者
weishuo1999 發(fā)表于 2013-08-19 16:46
template
void TT::Test::show(const TT::No& tt)
{

似乎是C++不允許類成員函數(shù)模板特化。

論壇徽章:
17
處女座
日期:2013-08-27 09:59:352015亞冠之柏太陽神
日期:2015-07-30 10:16:402015亞冠之薩濟拖拉機
日期:2015-07-29 18:58:182015年亞洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥豬
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56雙子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
4 [報告]
發(fā)表于 2013-08-19 17:25 |只看該作者
回復 1# Redshadows


    這是VC的代碼在gcc(g++)下是編不過的。你的程序有幾個問題:
1:g++不支持成員函數(shù)的模板特化。
2:namespace TT;是不是少寫了個using
3:#pragma once在gcc中是被列淘汰的特性,不應該使用。

論壇徽章:
17
處女座
日期:2013-08-27 09:59:352015亞冠之柏太陽神
日期:2015-07-30 10:16:402015亞冠之薩濟拖拉機
日期:2015-07-29 18:58:182015年亞洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥豬
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56雙子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
5 [報告]
發(fā)表于 2013-08-19 17:29 |只看該作者
回復 3# Redshadows

C++標準是支持類模板成員函數(shù)特化的。但是gcc(g++)不支持。


C++03, §14.7.3/2:
An explicit specialization shall be declared in the namespace of which the template is a member, or, for member templates, in the namespace of which the enclosing class or enclosing class template is a member.An explicit specialization of a member function, member class or static data member of a class template shall be declared in the namespace of which the class template is a member.
   

論壇徽章:
0
6 [報告]
發(fā)表于 2013-08-20 09:38 |只看該作者
myworkstation 發(fā)表于 2013-08-19 17:25
回復 1# Redshadows

少了using應該是從linux下復制出來少了。

論壇徽章:
0
7 [報告]
發(fā)表于 2013-08-20 09:41 |只看該作者
myworkstation 發(fā)表于 2013-08-19 17:29
回復 3# Redshadows

C++標準是支持類模板成員函數(shù)特化的。但是gcc(g++)不支持。

OK thanks

論壇徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16賽季CBA聯(lián)賽之青島
日期:2016-07-05 12:36:0515-16賽季CBA聯(lián)賽之廣東
日期:2016-06-29 11:45:542015亞冠之全北現(xiàn)代
日期:2015-07-22 08:09:472015年辭舊歲徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39獅子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技術圖書徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
8 [報告]
發(fā)表于 2013-08-20 09:51 |只看該作者
我將你的代碼中根本沒用到的代碼刪除,然后修補一下,在MinGW4.8.1和VC++9.0上都編譯測試通過
  1. #include <iostream>
  2. using namespace std;

  3. namespace TT
  4. {
  5.     struct No
  6.     {
  7.         int i;
  8.     };

  9.     struct Test
  10.     {
  11.         template <typename T> void show(const T& tt);
  12.         void show1();
  13.     };

  14. }

  15. namespace TT
  16. {
  17.     template<typename T>
  18.     void Test::show(const T& tt)
  19.     {
  20.         cout << "test" << endl;
  21.     }

  22.     template<>
  23.     void TT::Test::show(const TT::No& tt)
  24.     {
  25.         cout << "haha" << endl;
  26.     }

  27.     void Test::show1()
  28.     {
  29.         show(1);
  30.         show( No() );
  31.     }

  32. }

  33. int main()
  34. {
  35.     TT::Test test;
  36.     test.show1();

  37.     return 0;
  38. }
復制代碼

論壇徽章:
0
9 [報告]
發(fā)表于 2013-08-21 10:41 |只看該作者
x 模板能有cpp文件實現(xiàn)?

論壇徽章:
17
處女座
日期:2013-08-27 09:59:352015亞冠之柏太陽神
日期:2015-07-30 10:16:402015亞冠之薩濟拖拉機
日期:2015-07-29 18:58:182015年亞洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥豬
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56雙子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
10 [報告]
發(fā)表于 2013-08-21 11:07 |只看該作者
回復 9# superfeeler

在這個例子中是可以的。因為在實現(xiàn)模板的cpp文件中調(diào)用了模板函數(shù),show(1);這個調(diào)把模板實例化了。所以不會出問題,如果把show1在頭文件中實現(xiàn)(在頭文件中定義函數(shù)show1并調(diào)用show(1))那么就會出現(xiàn)錯誤。由于模板解析和實例化的過程復雜所以一般推薦模板都實現(xiàn)在頭文件中,不然會引出一堆的問題。
   
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP