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

标签 c++ templates input default-constructor class-template

简介:

如果标题有点困惑或含糊不清,我们深表歉意。很难对我的问题进行互联网搜索,因为我的问题似乎没有分解为可搜索的术语。此外,这是我在 Stackoverflow 上发表的第一篇文章,如果我超出了发布问题的惯例,请多多包涵,我会尽我所能合理安排格式。

话虽如此,让我谈谈我正在尝试做的事情:

我是一所大学的学生,正在完成教师布置的作业。我们正在为 vector 创建一个类(即数学上的 vector ,而不是数据类型 vector )。这个类是一个类模板,两种不同的模板化数据类型,一个用于 vector 的 x 分量,另一个用于 vector 的 y 分量。这是一个简单的类,该类返回 vector 的大小和方向(以弧度为单位)。还有作为友元函数的重载输入和输出运算符,以及几个构造函数。我不使用动态内存,所以我们可以把那整堆蠕虫放在一边。

这是我遇到的问题:

Vector2D<int, int> vec1(); //Default Constructor
cin  >> vec1;
cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction()
     << "\tMagnitude: " << vec1.magnitude() << "\n\n";

我的问题是,我无法执行 cin,也无法输出 direction() 和 magnitude()。编译器给了我一个很长的错误,但基本上说

错误 C2678:二进制“>>”:未找到采用“std::istream”类型左侧操作数的运算符(或没有可接受的转换)

但是,如果我这样做:

Vector2D<int, int> vec1(0,0); //No longer the default constructor
cin  >> vec1;
cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction()
     << "\tMagnitude: " << vec1.magnitude() << "\n\n";

天下皆乐。因此,我的问题很简单,我该如何解决这个问题?我想在用默认构造函数实例化后使用 cin,我想输出方向()和幅度()。假设我已经完成了所有标题声明和其他所有正确的事情,但我编写类(class)的方式是错误的——这里是:

我的类(class)文件:

#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

就是这样。如果我需要包含任何其他信息,请告诉我。

谢谢。

最佳答案

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

这段代码:

Vector2D<int, int> vec1();

应该是:

Vector2D<int, int> vec1;

简单易行。再次感谢,dyp。

关于C++:如何在类被模板化时使用默认构造函数实例化后将输入输入到对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22872461/

相关文章:

c++ - 如何创建正确的嵌套循环?

c++ - 循环内声明的生命周期

c++ - 为继承设计类

java - Java中如何获取用户输入?

C++ 获取传递给函数的 vector 的名称

c++ - 使用 "delete this"删除当前对象是否可以?

c++ - 什么是模板演绎指南,我们应该在什么时候使用它们?

c++ - 简化模板参数

jQuery:如果内部有一些填充,则动画为零宽度

javascript - 如何让应用程序在其内部返回搜索结果以实现离线快速页面导航?