簡體   English   中英

指針類型的類模板專門化中沒有用於調用的匹配函數

[英]No matching function for call in class template specialization for a pointer type

我不知道為什么不能將指針作為參考傳遞給函數。 也許我錯過了錯誤的要點。

class Point{
public:
    Point(){}
};

template<typename KEY,typename VALUE>
class TemplTest{
public:
    TemplTest(){}
    bool Set(const KEY& key,const VALUE& value){
        return false;
    }
};

template<typename KEY,typename VALUE>
class TemplTest<KEY*,VALUE>{
public:
    TemplTest(){}
    bool Set(KEY*& key,const VALUE& value){
        return true;
    }
};

int main(){
    Point p1;
    TemplTest<Point*,double> ht;
    double n=3.14;
    ht.Set(&p1,n);

    return 0;
}

錯誤:

no matching function for call to 'TemplTest<Point*, double>::Set(Point*, double&)'
no known conversion for argument 1 from 'Point*' to 'Point*&'

請幫忙,謝謝!

因為引用不能綁定到右值,所以&p1是一個沒有名稱的右值,可以解決此問題

Point *p1_ptr = &p1;
Point *&p1_ptr_ref = p1_ptr;
ht.Set( p1_ptr_ref, n);

或者您可以將const添加到密鑰

    bool Set( KEY* const& key,const VALUE& value){
//                 ^^^^^
        return false;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM