Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2022303
  • 博文数量: 414
  • 博客积分: 10312
  • 博客等级: 上将
  • 技术积分: 4921
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-31 01:49
文章分类

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: C/C++

2008-08-27 11:44:39

 

 

/*
 * tempmemb.cpp
 *
 * Created on: 2008-8-26
 * Author: efegliu
 */


#ifndef TEMPMEMB_CPP_
#define TEMPMEMB_CPP_

#include <iostream>

using std::cout;
using std::endl;

template <typename T>
class beta{
private:
    template <typename V>
    class hold{
    private:
        V val;
    public:
        hold(V v=0):val(v){}
        void show()const{ cout << val << endl;}
        V Value()const{return val;}
    };
    hold<T> q;
    hold<int> n;
public:
    beta(T t, int i) : q(t), n(i) {}
    template<typename U>
    U blab(U u, T t){ return (n.Value() + q.Value()) * u / t;}
    void Show() const {q.show(); n.show();}
};

int main(){
    beta<double> guy(3.5, 3);
    guy.Show();
    cout << guy.blab(10, 2.3) << endl;
    cout << "Done\n";
    system("PAUSE");
    return 0;
}

#endif /* TEMPMEMB_CPP_ */

 

编译的时候出错:

 

[support@ailf ~]$ g++ tempmemb.cpp -o tempmemb tempmemb.cpp: In member function `U beta::blab(U, T) [with U = int, T = double]': tempmemb.cpp:33: instantiated from here tempmemb.cpp:25: warning: converting to `int' from `double'

 

查找错误原因:

一.

 

template removed,

problem solved:

Summary: "instantiate from here" = "please remove template"

I don't want to sound sarcastic, but today (2008) a compiler that is responsible for so many applications should know how to handle templates .. and should be able to report errors with enough data.

 

 

阅读(2284) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~