C++:当类被模板化时,如何在使用默认构造函数实例化后将输入放入对象中
C++: How to cin input into an object after instantiating with default constructor when the class is templatized
简介:
抱歉,如果标题有点混乱或含糊不清。对我的问题进行互联网搜索非常困难,因为我的问题似乎没有分解为可搜索的术语。另外,这是我在 Stackoverflow 上的第一篇文章,如果我超越发布问题的常规主义,请耐心等待,我会尽我所能明智地格式化。
话虽如此,让我进入我正在尝试做的事情:
我是一所大学的学生,正在完成老师布置的作业。我们正在为向量创建一个类(即数学意义上的向量,而不是数据类型向量)。此类将是一个类模板,两种不同的模板化数据类型,一种用于向量的 x 分量,另一种用于向量的 y 分量。这是一个简单的类,该类返回矢量的大小和方向(以弧度为单位)。还有重载的输入和输出运算符作为友元函数,以及几个构造函数。我不使用动态内存,所以我们可以把那一整罐蠕虫放在一边。
这是我遇到的问题:
Vector2D<int, int> vec1(); //Default Constructor
cin >> vec1;
cout << "nVector 1 = " << vec1 << "ntDirection: " << vec1.direction()
<< "tMagnitude: " << vec1.magnitude() << "nn";
我的问题是,我无法做一个cin,我无法输出direction()和magnitude()。编译器给了我一个很长的错误,但基本上说
错误 C2678:二进制">>":找不到采用类型为"std::istream"的左操作数的运算符(或者没有可接受的转换)
但是,如果我这样做:
Vector2D<int, int> vec1(0,0); //No longer the default constructor
cin >> vec1;
cout << "nVector 1 = " << vec1 << "ntDirection: " << vec1.direction()
<< "tMagnitude: " << vec1.magnitude() << "nn";
一切都对世界感到满意。因此,我的问题很简单,我该如何解决这个问题?我想在使用默认构造函数实例化后使用 cin,我想输出 direction() 和 magnitude()。考虑一下我已经完成了所有的标头声明和其他所有正确的内容,并且我编写类的方式是错误的 - 这里是:
我的班级文件:
#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;
像蛋糕一样简单。再次感谢,死。
- 为什么std::vector和std::valarray初始化构造函数不同
- 我应该在C++中将这些结构用作参数化构造函数吗?
- 将显式实例化的函数模板与转换匹配
- 为什么即使我调用参数化构造函数也会调用默认构造函数?
- 使用向量初始化参数化构造函数的对象数组
- 初始化构造函数C++中结构的向量
- 我们可以用参数化构造函数初始化结构的数组吗?
- 以下代码如何工作以每次为唯一调用堆栈唯一实例化模板函数?
- 如何同时别名和实例化模板函数?
- 如何在其他类中使用参数化构造函数制作类的对象?
- C++ 中常量属性的初始化构造函数错误
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 如何执行参数化构造函数的调用?
- 为什么我不能在类中使用参数化构造函数?
- 如何在 QML 中实例化C++组件 - WITH 参数化构造函数
- C++如何用参数化构造函数实例化对象
- 基于运行时类型实例化构造函数
- 模板类的模板化构造函数的显式实例化
- 类模板的显式实例化没有实例化构造函数
- 初始化实例的构造函数内的静态成员