yy123shabi
young-y
2016-05-22 15:37
采纳率: 100%
浏览 1.3k
已采纳

C++模板中类模板的问题

Myvector vf(5, Fraction(1,2));当执行到此代码时,应该调用Myvector的模板来生成Fraction类型的模板但他却调用了Fraction的构造函数,然后就无穷递归了,这是怎么回事,求解决方案
template class Myvector {
T arry[1000];
int realsize;
public:
Myvector(int ,T );
Myvector();
T & operator=( T & );
};
template Myvector::Myvector(int a, T b)
{
realsize = a;
for (int i = 0; i < a; i++)arry[i] = b;

}

class Fraction {
private:
int fenzi;
int fenmu;
public:
Fraction(int, int);
Fraction();
Fraction &operator=(const Fraction&);
};

Fraction::Fraction() { fenzi = 0; fenmu = 1; }

Fraction::Fraction(int a, int b)
{
fenzi = a;
fenmu = b;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • caozhy
    已采纳

    你说的

     Myvector <Fraction> vf(5, Fraction(1,2));
    

    这代码在哪里,你完整的代码是什么。

    点赞 评论
  • yy123shabi
    young-y 2016-05-23 04:26

    #pragma once
    #ifndef MYVECTOR_H
    #define MYVECTOR_H
    #include
    #include
    using namespace std;
    templateclass Myvector {
    T arry[1000];
    int realsize;
    public:
    Myvector(int ,T );
    Myvector();
    T & operator=( T & );
    int size();
    void resize(int );
    bool empty();
    T&operator;
    T &at(int );
    T&front();
    T&back();
    void push_back(T);
    void pop_back();
    void insert(const T&,int );
    void erase(int );
    void clear();
    void sort();
    void show();
    int binarysearch( const T&);
    };
    class point {
    private:
    double m_x;
    double m_y;
    public:
    point() { m_x = 0; m_y = 0; }
    point(double, double);
    point &operator=(const point&);
    void show();
    templatefriend ostream & operator<<(ostream &out, Myvector&arr);
    };
    template ostream & operator<<(ostream &out, Myvector&arr)
    {
    arr.show();
    return out;
    }
    point &point::operator=(const point & a)
    {
    m_x = a.m_x;
    m_y = a.m_y;
    return *this;
    }
    point::point(double x, double y)
    {
    m_x = x;
    m_y = y;
    }
    void point::show()
    {
    cout << "(" << m_x << "," << m_y << ")" << " ";
    }
    class Circle {
    private :
    point zb;
    double m_r;
    public:
    Circle() { m_r = 0; }
    Circle(point, double);
    bool operator<( Circle &)const;
    bool operator<( Circle );
    Circle &operator=( const Circle &);
    friend ostream&operator<<(ostream&, Circle&);
    void show();

    };
    ostream&operator<<(ostream & out, Circle & arr)
    {
    arr.show();
    out << endl;
    return out;
    }
    Circle &Circle::operator=( const Circle &a)
    {
    this->zb = a.zb;
    this->m_r = a.m_r;
    return *this;
    }
    bool Circle::operator<( Circle & a)const
    {
    if (this->m_r < a.m_r)return true;
    else return false;
    }
    bool Circle::operator<( Circle a)
    {
    if (this->m_r < a.m_r)return true;
    else return false;
    }
    Circle::Circle(point a, double b)
    {
    zb = a;
    m_r = b;
    }
    void Circle::show()
    {
    zb.show();
    cout << "半径:" << m_r << " ";
    }

    class Fraction {
    private:
    int fenzi;
    int fenmu;
    public:
    Fraction(int, int);
    Fraction();
    Fraction &operator=(const Fraction&);
    bool operator<(const Fraction&)const;
    friend ostream&operator<<(ostream&, Fraction&);
    void show();
    };
    ostream&operator<<(ostream& out, Fraction& arr)
    {
    arr.show();
    out << endl;
    return out;
    }
    Fraction::Fraction() { fenzi = 0; fenmu = 1; }
    Fraction::Fraction(int a, int b)
    {
    fenzi = a;
    fenmu = b;
    }
    Fraction &Fraction::operator=(const Fraction & a)
    {
    this->fenzi = a.fenzi;
    this->fenmu = a.fenmu;
    return*this;
    }
    bool Fraction::operator<(const Fraction &a)const
    {
    if ((fenzi*a.fenmu - a.fenzi*fenmu) / (fenmu*a.fenmu) < 0)return true;
    else return false;
    }
    void Fraction::show()
    {
    cout << fenzi << "/" << fenmu << " ";
    }
    template Myvector::Myvector()
    {
    realsize =0;
    for (unsigned i = 0; i < Size; i++)arry[i] = 0;
    }
    template Myvector::Myvector(int a, T b)
    {
    realsize = a;
    for (int i = 0; i < a; i++)arry[i] = b;

    }
    templateT & Myvector:: operator=( T & a)
    {
    for (unsigned i = 0; i < a.realsize; i++)arry[i] = a.arry[i];
    realsize = a.realsize;
    rerurn *this;

    }
    templateint Myvector::size()
    {
    return realsize;
    }
    templatevoid Myvector::resize(int a)
    {
    realsize = a;
    }
    templatebool Myvector::empty()
    {
    if (realsize == 0)return false;
    else return true;
    }
    templateT& Myvector::operator
    {
    if (i > (realsize - 1) || i < 0) {
    cout << "下标出界!" << endl;
    exit(1);
    }
    return arry[i];
    }
    templateT& Myvector::at(int a)
    {
    return arry[a];
    }
    templateT& Myvector::front()
    {
    return arry[0];
    }
    templateT& Myvector::back()
    {
    return arry[realsize-1];
    }
    templatevoid Myvector::push_back(T a)
    {
    if (realsize >= (999)) { cout << "数组已满不能再添加!" << endl; }
    else { arry[realsize] = a; ++realsize; }
    }
    templatevoid Myvector::pop_back()
    {
    --realsize;
    }
    templatevoid Myvector::clear()
    {
    realsize = 0;
    }
    templatevoid Myvector::insert(const T&p,int a )
    {
    int j;
    if (arealsize) { cout << "插入位置不在数组内!" << endl; }
    else {
    ++realsize;
    for (j = realsize - 1; j > a; j--)arry[j] = arry[j - 1];
    arry[a] = p;
    }
    }
    templatevoid Myvector::erase(int i)
    {
    if (irealsize - 1) { cout << "对应的下标不在数组内" << endl; }
    else {
    for (int j = i; j < realsize - 1; j++)arry[j] = arry[j + 1];
    --realsize;
    }
    }
    templatevoid Myvector::sort()
    {
    for (int i = 0; i < realsize; i++)
    {
    for (int j = realsize; j > i; j--)
    {
    if (arry[j] < arry[j - 1])
    {
    T temp = arry[j - 1];
    arry[j - 1] = arry[j];
    arry[j] = temp;
    }
    }
    }
    }
    templateint Myvector ::binarysearch( const T& x)
    {
    int high = realsize, low = 0, mid=-1;
    if ( realsize== 0)return -1;
    while (low <= high)
    {
    mid = (low + high) / 2;
    if (x<arry[mid])high = mid - 1;
    else if (arry[mid]<x)low = mid + 1;
    else return mid;
    }

    return mid;
    

    }
    templatevoid Myvector ::show()
    {
    for (int i = 0; i < this->size(); i++)cout << this->arry[i];

    }
    #endif

    #include"Myvector.h"
    #include
    using namespace std;
    template
    void test(Vector& v, const DataType& val) {
    v.show();

    v.push_back(val); v.show();
    v.sort(); v.show();
    v.binarysearch(val);
    v.erase(0); v.show();
    v.insert(val, 0); v.show();
    

    }
    int main()
    {
    Fraction(2, 3);
    Myvectorvi(5, 1);
    vi[0] = -1; vi[1] = 3; vi[2] = 9; vi[3] = -5;
    Myvectorvs(5, "C");
    vs[0] = "Java"; vs[1] = "Lisp"; vs[2] = "C#"; vs[3] = "Object-C";
    /*Myvectorvf(5, Fraction(1,2));
    vf[0] = Fraction(2, 3); vf[1] = Fraction(2, 3); vf[2] = Fraction(2, 5); vf[3] = Fraction(4, 7);*/
    Myvector vc(5, Circle(point(), 1.0));
    vc[0] = Circle(point(), 2.0); vc[1] = Circle(point(), 0.5); vc[2] = Circle(point(), 5.0); vc[3] = Circle(point(), 0.1);
    test, int>(vi, 10);
    test, string>(vs, "C++");
    /*test, Fraction>(vf, Fraction(7, 10));*/
    test,Circle>(vc,Circle(point(),3.0));
    return 0;
    }

    点赞 评论
  • yy123shabi
    young-y 2016-05-23 04:28

    #pragma once
    #ifndef MYVECTOR_H
    #define MYVECTOR_H
    #include
    #include
    using namespace std;
    templateclass Myvector {
    T arry[1000];
    int realsize;
    public:
    Myvector(int ,T );
    Myvector();
    T & operator=( T & );
    int size();
    void resize(int );
    bool empty();
    T&operator;
    T &at(int );
    T&front();
    T&back();
    void push_back(T);
    void pop_back();
    void insert(const T&,int );
    void erase(int );
    void clear();
    void sort();
    void show();
    int binarysearch( const T&);
    };
    class point {
    private:
    double m_x;
    double m_y;
    public:
    point() { m_x = 0; m_y = 0; }
    point(double, double);
    point &operator=(const point&);
    void show();
    templatefriend ostream & operator<<(ostream &out, Myvector&arr);
    };
    template ostream & operator<<(ostream &out, Myvector&arr)
    {
    arr.show();
    return out;
    }
    point &point::operator=(const point & a)
    {
    m_x = a.m_x;
    m_y = a.m_y;
    return *this;
    }
    point::point(double x, double y)
    {
    m_x = x;
    m_y = y;
    }
    void point::show()
    {
    cout << "(" << m_x << "," << m_y << ")" << " ";
    }
    class Circle {
    private :
    point zb;
    double m_r;
    public:
    Circle() { m_r = 0; }
    Circle(point, double);
    bool operator<( Circle &)const;
    bool operator<( Circle );
    Circle &operator=( const Circle &);
    friend ostream&operator<<(ostream&, Circle&);
    void show();

    };
    ostream&operator<<(ostream & out, Circle & arr)
    {
    arr.show();
    out << endl;
    return out;
    }
    Circle &Circle::operator=( const Circle &a)
    {
    this->zb = a.zb;
    this->m_r = a.m_r;
    return *this;
    }
    bool Circle::operator<( Circle & a)const
    {
    if (this->m_r < a.m_r)return true;
    else return false;
    }
    bool Circle::operator<( Circle a)
    {
    if (this->m_r < a.m_r)return true;
    else return false;
    }
    Circle::Circle(point a, double b)
    {
    zb = a;
    m_r = b;
    }
    void Circle::show()
    {
    zb.show();
    cout << "半径:" << m_r << " ";
    }

    class Fraction {
    private:
    int fenzi;
    int fenmu;
    public:
    Fraction(int, int);
    Fraction();
    Fraction &operator=(const Fraction&);
    bool operator<(const Fraction&)const;
    friend ostream&operator<<(ostream&, Fraction&);
    void show();
    };
    ostream&operator<<(ostream& out, Fraction& arr)
    {
    arr.show();
    out << endl;
    return out;
    }
    Fraction::Fraction() { fenzi = 0; fenmu = 1; }
    Fraction::Fraction(int a, int b)
    {
    fenzi = a;
    fenmu = b;
    }
    Fraction &Fraction::operator=(const Fraction & a)
    {
    this->fenzi = a.fenzi;
    this->fenmu = a.fenmu;
    return*this;
    }
    bool Fraction::operator<(const Fraction &a)const
    {
    if ((fenzi*a.fenmu - a.fenzi*fenmu) / (fenmu*a.fenmu) < 0)return true;
    else return false;
    }
    void Fraction::show()
    {
    cout << fenzi << "/" << fenmu << " ";
    }
    template Myvector::Myvector()
    {
    realsize =0;
    for (unsigned i = 0; i < Size; i++)arry[i] = 0;
    }
    template Myvector::Myvector(int a, T b)
    {
    realsize = a;
    for (int i = 0; i < a; i++)arry[i] = b;

    }
    templateT & Myvector:: operator=( T & a)
    {
    for (unsigned i = 0; i < a.realsize; i++)arry[i] = a.arry[i];
    realsize = a.realsize;
    rerurn *this;

    }
    templateint Myvector::size()
    {
    return realsize;
    }
    templatevoid Myvector::resize(int a)
    {
    realsize = a;
    }
    templatebool Myvector::empty()
    {
    if (realsize == 0)return false;
    else return true;
    }
    templateT& Myvector::operator
    {
    if (i > (realsize - 1) || i < 0) {
    cout << "下标出界!" << endl;
    exit(1);
    }
    return arry[i];
    }
    templateT& Myvector::at(int a)
    {
    return arry[a];
    }
    templateT& Myvector::front()
    {
    return arry[0];
    }
    templateT& Myvector::back()
    {
    return arry[realsize-1];
    }
    templatevoid Myvector::push_back(T a)
    {
    if (realsize >= (999)) { cout << "数组已满不能再添加!" << endl; }
    else { arry[realsize] = a; ++realsize; }
    }
    templatevoid Myvector::pop_back()
    {
    --realsize;
    }
    templatevoid Myvector::clear()
    {
    realsize = 0;
    }
    templatevoid Myvector::insert(const T&p,int a )
    {
    int j;
    if (arealsize) { cout << "插入位置不在数组内!" << endl; }
    else {
    ++realsize;
    for (j = realsize - 1; j > a; j--)arry[j] = arry[j - 1];
    arry[a] = p;
    }
    }
    templatevoid Myvector::erase(int i)
    {
    if (irealsize - 1) { cout << "对应的下标不在数组内" << endl; }
    else {
    for (int j = i; j < realsize - 1; j++)arry[j] = arry[j + 1];
    --realsize;
    }
    }
    templatevoid Myvector::sort()
    {
    for (int i = 0; i < realsize; i++)
    {
    for (int j = realsize; j > i; j--)
    {
    if (arry[j] < arry[j - 1])
    {
    T temp = arry[j - 1];
    arry[j - 1] = arry[j];
    arry[j] = temp;
    }
    }
    }
    }
    templateint Myvector ::binarysearch( const T& x)
    {
    int high = realsize, low = 0, mid=-1;
    if ( realsize== 0)return -1;
    while (low <= high)
    {
    mid = (low + high) / 2;
    if (x<arry[mid])high = mid - 1;
    else if (arry[mid]<x)low = mid + 1;
    else return mid;
    }

    return mid;
    

    }
    templatevoid Myvector ::show()
    {
    for (int i = 0; i < this->size(); i++)cout << this->arry[i];

    }
    #endif

    #include"Myvector.h"
    #include
    using namespace std;
    template
    void test(Vector& v, const DataType& val) {
    v.show();

    v.push_back(val); v.show();
    v.sort(); v.show();
    v.binarysearch(val);
    v.erase(0); v.show();
    v.insert(val, 0); v.show();
    

    }
    int main()
    {
    Fraction(2, 3);
    Myvectorvi(5, 1);
    vi[0] = -1; vi[1] = 3; vi[2] = 9; vi[3] = -5;
    Myvectorvs(5, "C");
    vs[0] = "Java"; vs[1] = "Lisp"; vs[2] = "C#"; vs[3] = "Object-C";
    /*Myvectorvf(5, Fraction(1,2));
    vf[0] = Fraction(2, 3); vf[1] = Fraction(2, 3); vf[2] = Fraction(2, 5); vf[3] = Fraction(4, 7);*/
    Myvector vc(5, Circle(point(), 1.0));
    vc[0] = Circle(point(), 2.0); vc[1] = Circle(point(), 0.5); vc[2] = Circle(point(), 5.0); vc[3] = Circle(point(), 0.1);
    test, int>(vi, 10);
    test, string>(vs, "C++");
    /*test, Fraction>(vf, Fraction(7, 10));*/
    test,Circle>(vc,Circle(point(),3.0));
    return 0;
    }

    点赞 评论

相关推荐