C++ C++;:当类被模板化时,如何在使用默认构造函数实例化后将数据输入到对象中

C++ C++;:当类被模板化时,如何在使用默认构造函数实例化后将数据输入到对象中,c++,templates,input,default-constructor,class-template,C++,Templates,Input,Default Constructor,Class Template,简介: Vector2D<int, int> vec1(); //Default Constructor cin >> vec1; cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction() << "\tMagnitude: " << vec1.magnitude() << "\n\n";

简介:

Vector2D<int, int> vec1(); //Default Constructor
cin  >> vec1;
cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction()
     << "\tMagnitude: " << vec1.magnitude() << "\n\n";
#pragma once
#include <iostream>
#include <iomanip>
#include <cmath>    //for sqrt function to get the magnitude and atan for radians.

using namespace std;

template <class T, class S>
class Vector2D
{
private:
    T m_xComp;
    S m_yComp;
    static int signif_digit; //Becomes the argument for setPrecision(x) on output.
public:
    static int signif_digits;
    Vector2D(): m_xComp((T)0), (S)m_yComp((S)0) {};
    Vector2D(T xComp, S yComp);
    void setX(T xComp);
    void setY(S yComp);
    T getX();
    S getY();
    double magnitude();
    double direction(); //returns direction of vector in radians.
    static void setPrecision(int prec);
    static int precision();
    friend ostream& operator<<(ostream& os, const Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        os  << '<' << vec.m_xComp << ',' << vec.m_yComp << '>';
        return os;
    }

    friend istream& operator>>(istream& is, Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        char remove_Char;
        T xComp = 0;
        S yComp = 0;
        is >> remove_Char >> xComp >> remove_Char >> yComp;
        vec.m_xComp = xComp;
        vec.m_yComp = yComp;
        return is;
    }
};


template <class T, class S>
Vector2D<T, S>::Vector2D(T xComp, S yComp)
{
    m_xComp = xComp;
    m_yComp = yComp;
}


template <class T, class S>
void Vector2D<T, S>::setPrecision(int prec)
{
    signif_digit = prec;
}


template <class T, class S>
int Vector2D<T, S>::precision()
{   return signif_digit;    }


template <class T, class S>
void Vector2D<T, S>::setX(T xComp)
{   m_xComp = xComp;    }


template <class T, class S>
void Vector2D<T, S>::setY(S yComp)
{   m_yComp = yComp;    }


template <class T, class S>
T Vector2D<T, S>::getX()
{   return m_xComp;     }


template <class T, class S>
S Vector2D<T, S>::getY()
{   return m_yComp;     }


template <class T, class S>
double Vector2D<T, S>::magnitude()
{
    return sqrt( (double)(m_xComp*m_xComp + m_yComp*m_yComp) );
}


//------------------------Consider using atan2 next time-------------------------------------
template <class T, class S>
double Vector2D<T, S>::direction()
{
    if (m_xComp == 0)
    {
        if(m_yComp == 0)
        {
            cout << "\nNote: Both x and y components equal zero.\n";
            return 0;
        }
        else if (m_yComp > 0)
            return atan(1.0)*2; //If y > 0 and x = 0, return PI/2
        else if (m_yComp < 0)
            return atan(1.0)*6; //If y < 0 and x = 0, return 3*PI/2
    }
    else if (m_xComp > 0)
    {
        if (m_yComp >= 0)
            return atan((double)(m_yComp/m_xComp)); //First Quadrant
        else
            return (atan(1.0)*8 + atan((double)(m_yComp/m_xComp)) ); //Fourth Quadrant
    }
    else
        return (atan(1.0)*4 + atan((double)(m_yComp/m_xComp)) ); //Second & Third Quadrant
}
//-------------------------------------------------------------------------------------------


template <class T, class S>
int Vector2D<T, S>::signif_digit = 3;   //private

template <class T, class S>
int Vector2D<T, S>::signif_digits = 3;  //public
抱歉,如果标题有点混乱或模糊。在网上搜索我的问题是非常困难的,因为我的问题似乎没有分解成可搜索的术语。另外,这是我在Stackoverflow上的第一篇帖子,如果我超出了发布问题的常规,请容忍我,我会尽我所能

话虽如此,让我来谈谈我想做的事情:

Vector2D<int, int> vec1(); //Default Constructor
cin  >> vec1;
cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction()
     << "\tMagnitude: " << vec1.magnitude() << "\n\n";
#pragma once
#include <iostream>
#include <iomanip>
#include <cmath>    //for sqrt function to get the magnitude and atan for radians.

using namespace std;

template <class T, class S>
class Vector2D
{
private:
    T m_xComp;
    S m_yComp;
    static int signif_digit; //Becomes the argument for setPrecision(x) on output.
public:
    static int signif_digits;
    Vector2D(): m_xComp((T)0), (S)m_yComp((S)0) {};
    Vector2D(T xComp, S yComp);
    void setX(T xComp);
    void setY(S yComp);
    T getX();
    S getY();
    double magnitude();
    double direction(); //returns direction of vector in radians.
    static void setPrecision(int prec);
    static int precision();
    friend ostream& operator<<(ostream& os, const Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        os  << '<' << vec.m_xComp << ',' << vec.m_yComp << '>';
        return os;
    }

    friend istream& operator>>(istream& is, Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        char remove_Char;
        T xComp = 0;
        S yComp = 0;
        is >> remove_Char >> xComp >> remove_Char >> yComp;
        vec.m_xComp = xComp;
        vec.m_yComp = yComp;
        return is;
    }
};


template <class T, class S>
Vector2D<T, S>::Vector2D(T xComp, S yComp)
{
    m_xComp = xComp;
    m_yComp = yComp;
}


template <class T, class S>
void Vector2D<T, S>::setPrecision(int prec)
{
    signif_digit = prec;
}


template <class T, class S>
int Vector2D<T, S>::precision()
{   return signif_digit;    }


template <class T, class S>
void Vector2D<T, S>::setX(T xComp)
{   m_xComp = xComp;    }


template <class T, class S>
void Vector2D<T, S>::setY(S yComp)
{   m_yComp = yComp;    }


template <class T, class S>
T Vector2D<T, S>::getX()
{   return m_xComp;     }


template <class T, class S>
S Vector2D<T, S>::getY()
{   return m_yComp;     }


template <class T, class S>
double Vector2D<T, S>::magnitude()
{
    return sqrt( (double)(m_xComp*m_xComp + m_yComp*m_yComp) );
}


//------------------------Consider using atan2 next time-------------------------------------
template <class T, class S>
double Vector2D<T, S>::direction()
{
    if (m_xComp == 0)
    {
        if(m_yComp == 0)
        {
            cout << "\nNote: Both x and y components equal zero.\n";
            return 0;
        }
        else if (m_yComp > 0)
            return atan(1.0)*2; //If y > 0 and x = 0, return PI/2
        else if (m_yComp < 0)
            return atan(1.0)*6; //If y < 0 and x = 0, return 3*PI/2
    }
    else if (m_xComp > 0)
    {
        if (m_yComp >= 0)
            return atan((double)(m_yComp/m_xComp)); //First Quadrant
        else
            return (atan(1.0)*8 + atan((double)(m_yComp/m_xComp)) ); //Fourth Quadrant
    }
    else
        return (atan(1.0)*4 + atan((double)(m_yComp/m_xComp)) ); //Second & Third Quadrant
}
//-------------------------------------------------------------------------------------------


template <class T, class S>
int Vector2D<T, S>::signif_digit = 3;   //private

template <class T, class S>
int Vector2D<T, S>::signif_digits = 3;  //public
我是一名学生,在一所大学里做老师布置的作业。我们正在为向量创建一个类(即,数学方面的向量,而不是数据类型向量)。这个类是一个类模板,有两个不同的模板化数据类型,一个用于向量的x分量,另一个用于向量的y分量。这是一个简单的类,该类返回向量的大小和方向(弧度)。还有作为友元函数的重载输入和输出运算符,以及一些构造函数。我不是在用动态记忆,所以我们可以把这整罐虫子放在一边

我的问题是:

Vector2D<int, int> vec1(); //Default Constructor
cin  >> vec1;
cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction()
     << "\tMagnitude: " << vec1.magnitude() << "\n\n";
#pragma once
#include <iostream>
#include <iomanip>
#include <cmath>    //for sqrt function to get the magnitude and atan for radians.

using namespace std;

template <class T, class S>
class Vector2D
{
private:
    T m_xComp;
    S m_yComp;
    static int signif_digit; //Becomes the argument for setPrecision(x) on output.
public:
    static int signif_digits;
    Vector2D(): m_xComp((T)0), (S)m_yComp((S)0) {};
    Vector2D(T xComp, S yComp);
    void setX(T xComp);
    void setY(S yComp);
    T getX();
    S getY();
    double magnitude();
    double direction(); //returns direction of vector in radians.
    static void setPrecision(int prec);
    static int precision();
    friend ostream& operator<<(ostream& os, const Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        os  << '<' << vec.m_xComp << ',' << vec.m_yComp << '>';
        return os;
    }

    friend istream& operator>>(istream& is, Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        char remove_Char;
        T xComp = 0;
        S yComp = 0;
        is >> remove_Char >> xComp >> remove_Char >> yComp;
        vec.m_xComp = xComp;
        vec.m_yComp = yComp;
        return is;
    }
};


template <class T, class S>
Vector2D<T, S>::Vector2D(T xComp, S yComp)
{
    m_xComp = xComp;
    m_yComp = yComp;
}


template <class T, class S>
void Vector2D<T, S>::setPrecision(int prec)
{
    signif_digit = prec;
}


template <class T, class S>
int Vector2D<T, S>::precision()
{   return signif_digit;    }


template <class T, class S>
void Vector2D<T, S>::setX(T xComp)
{   m_xComp = xComp;    }


template <class T, class S>
void Vector2D<T, S>::setY(S yComp)
{   m_yComp = yComp;    }


template <class T, class S>
T Vector2D<T, S>::getX()
{   return m_xComp;     }


template <class T, class S>
S Vector2D<T, S>::getY()
{   return m_yComp;     }


template <class T, class S>
double Vector2D<T, S>::magnitude()
{
    return sqrt( (double)(m_xComp*m_xComp + m_yComp*m_yComp) );
}


//------------------------Consider using atan2 next time-------------------------------------
template <class T, class S>
double Vector2D<T, S>::direction()
{
    if (m_xComp == 0)
    {
        if(m_yComp == 0)
        {
            cout << "\nNote: Both x and y components equal zero.\n";
            return 0;
        }
        else if (m_yComp > 0)
            return atan(1.0)*2; //If y > 0 and x = 0, return PI/2
        else if (m_yComp < 0)
            return atan(1.0)*6; //If y < 0 and x = 0, return 3*PI/2
    }
    else if (m_xComp > 0)
    {
        if (m_yComp >= 0)
            return atan((double)(m_yComp/m_xComp)); //First Quadrant
        else
            return (atan(1.0)*8 + atan((double)(m_yComp/m_xComp)) ); //Fourth Quadrant
    }
    else
        return (atan(1.0)*4 + atan((double)(m_yComp/m_xComp)) ); //Second & Third Quadrant
}
//-------------------------------------------------------------------------------------------


template <class T, class S>
int Vector2D<T, S>::signif_digit = 3;   //private

template <class T, class S>
int Vector2D<T, S>::signif_digits = 3;  //public
Vector2D vec1()//缺省构造
cin>>vec1;
cout>删除字符>>yComp;
vec.m_xComp=xComp;
vec.m_yComp=yComp;
回报是;
}
};
模板
Vector2D::Vector2D(T xComp,S yComp)
{
m_xComp=xComp;
m_yComp=yComp;
}
模板
void Vector2D::setPrecision(int prec)
{
符号数字=prec;
}
模板
int Vector2D::precision()
{返回符号数字;}
模板
void Vector2D::setX(T xComp)
{m_xComp=xComp;}
模板
void Vector2D::setY(S yComp)
{m_yComp=yComp;}
模板
T Vector2D::getX()
{返回m_xComp;}
模板
S Vector2D::getY()
{返回m_yComp;}
模板
双矢量2D::幅值()
{
返回sqrt((双精度)(m_xComp*m_xComp+m_yComp*m_yComp));
}
//------------------------下次考虑使用atan2-------------------------------------
模板
双矢量2D::方向()
{
如果(m_xComp==0)
{
如果(m_yComp==0)
{
cout(0)
返回atan(1.0)*2;//如果y>0且x=0,则返回PI/2
否则如果(m_yComp<0)
返回atan(1.0)*6;//如果y<0且x=0,则返回3*PI/2
}
如果(m_xComp>0),则为else
{
如果(m_yComp>=0)
返回atan((双)(m_yComp/m_xComp));//第一象限
其他的
返回(atan(1.0)*8+atan((双);//第四象限
}
其他的
返回(atan(1.0)*4+atan((双)(m_yComp/m_xComp));//第二象限和第三象限
}
//-------------------------------------------------------------------------------------------
模板
int Vector2D::signif_digit=3;//私有
模板
int Vector2D::signif_digits=3;//公共
就这些。如果我需要包括任何其他信息,请告诉我


谢谢。

在评论中,dyp回答了我的问题。它非常简单。只是,我试图实例化MyVector类,就像我在声明一个函数一样

此代码:

Vector2D<int, int> vec1();
Vector2D vec1();
应该是:

Vector2D<int, int> vec1;
vector2dvec1;

非常简单。再次感谢你,dyp。

Vector2D vec1();
这声明了一个函数。搜索“最麻烦的解析”,省去
()
来修复它;或者将它们改为
{}
。您应该始终添加语言标记。这还将提供自动语法突出显示。
使用命名空间std;
请不要在头文件中这样做。这是一种不好的做法,最终可能会导致问题(名称冲突).
//要弄清楚一件好事:为什么我必须在行中声明友元函数?
我想你的意思是定义,也就是说,为什么我必须将友元函数的函数体放在类模板定义/体中?因为这是最简单和最安全的事情。你也可以在类之外定义它,但是你应该使用转发声明(因为友元声明是声明,可以引入新名称,这可能会导致一些微妙的问题)。其中一些成员函数可以是
const
(这意味着您应该将它们声明为
const
)。这些问题可以在中解决。