找不到已定义的运算符

时间:2011-10-30 15:47:29

标签: c++

有Int类(下面的代码)和operator +(下面的)我可以使用gcc 4.6.1成功编译它,如果我有:

template<class Int_T>
    class Int
    {
    };

template<class A,class B>
A operator+(const Int<A>& left, const Int<B>& right)

{
    return -1;///this is dummy
}



int main()
{
    Int<int> a(5);
    Int<int> b(6);//BUT IF I CHANGE TO OTHER TYPE THAN int I'M GETTING ERROR
    auto c = a + b;
    return 0;
}

我得到的错误:
... \ main.cpp | 10 |错误:'a + b'中的'operator +'不匹配 谁能告诉我,我做错了什么? 编辑(实例)

#ifndef IF__H_INCLUDED
#define IF__H_INCLUDED

template<bool Cond, class First, class Second>
struct if_
{
    typedef typename First::type type;
};

template<class First, class Second>
struct if_<false,First,Second>
{
    typedef typename Second::type type;
};

#endif // IF__H_INCLUDED
#ifndef CHOOSE_LARGER_H_INCLUDED
#define CHOOSE_LARGER_H_INCLUDED
#include <type_traits>
template<class A,class B>
struct Choose_Larger
{
    typedef typename std::conditional<(sizeof(A) > sizeof(B)),A,B>::type type;
};
#ifndef IS_CHAR_H_INCLUDED
#define IS_CHAR_H_INCLUDED
#include <type_traits>

template<class Int_T>
struct Is_Char_
{
    enum {value = false};
};

template<>
struct Is_Char_<char>
{
    enum {value = true};
};

template<>
struct Is_Char_<unsigned char>
{
    enum {value = true};
};

template<>
struct Is_Char_<signed char>
{
    enum {value = true};
};

template<class Int_T>
struct Is_Char : Is_Char_<typename std::remove_cv<Int_T>::type>
{

};

#endif // IS_CHAR_H_INCLUDED

#endif // CHOOSE_LARGER_H_INCLUDED
#ifndef PROMOTE_H_INCLUDED
#define PROMOTE_H_INCLUDED
#include <type_traits>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/next.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/end.hpp>
#include "Is_Char.h"

/**
Created by Art 10/2011
*/
/*Promotes Integer type to one up in size range*/
template<class Integer>
struct Promote
{
    static_assert(std::is_integral<Integer>::value,"Non Integer type is not allowed.");
    /*Check correct type - depending on Integer being signed or unsigned*/
    typedef typename std::conditional<std::is_signed<Integer>::value,
                                boost::mpl::vector<signed char,short,int,long,long long>,
    boost::mpl::vector<unsigned char,unsigned short,unsigned int,long,long long>
                                     >::type types;
    /*
    Find this type from the list above - substituting Integer for signed or unsigned char iff Integer is of type char
    */
    typedef typename boost::mpl::find<types,
    typename std::conditional<Is_Char<Integer>::value,
    typename std::conditional<std::is_signed<Integer>::value,signed char,unsigned char>::type, Integer>::type>::type this_type;

    /*If Integer is int and if size of it is == to long promote int to long long (iterate to next element twice)*/
    typedef typename boost::mpl::eval_if<boost::mpl::bool_<((std::is_same<Integer,int>::value || std::is_same<Integer,unsigned int>::value)
                                                                && (sizeof(int) == sizeof(long)))>,
                                         boost::mpl::next<typename boost::mpl::next<this_type>::type>,
                                         boost::mpl::next<this_type>
                                        >::type next_type;
    /*Check if iterator points within range or if one pass end which means that Integer was u/long long*/
    typedef typename std::conditional<std::is_same<typename boost::mpl::end<types>::type,next_type>::value,Integer,typename boost::mpl::deref<next_type>::type>::type type;
};

#endif // PROMOTE_H_INCLUDED

/*Add two Int types*/
template<class A,class B,class R = Int<typename if_<!std::is_same<Int<A>,Int<B>>::value,
                                                    Choose_Larger<Int<A>,Int<B>>,
                                                    Promote<A>
                                                    >::type
                                        >
        >
R operator+(const Int<A>& left, const Int<B>& right)

{
    return add<R>(left,right);
}

1 个答案:

答案 0 :(得分:1)

自从我开始回答这个问题以来,这个问题已经改变了。

所以它真的是一个不再存在的问题的答案。

无论如何,对于下面的替代方法,遗憾的是Visual C ++ 10.0还不支持新的C ++ 11函数声明语法,但至少这与MSVC和g ++一起编译:

#include <iostream>

template< class IntType >
struct Int
{
    IntType value_;
    explicit Int( IntType v = 0 ): value_( v ) {}
};

template< class IntA, class IntB >
Int< decltype( IntA() + IntB() ) > operator+( Int< IntA > a, Int< IntB > b )
{
    typedef decltype( IntA() + IntB() ) R; 
    return Int<R>( a.value_ + b.value_ );
}

int main()
{
    Int<int> a(5);
    Int<long> b(6);
    auto c = a + b;

    std::cout << c.value_ << std::endl;
}

干杯&amp;第h。,

相关问题