如何使用重载>> <<到构成 ojbect 部门(部门**)的指针数组
how to use overloading >> << to array of pointers which consitst ojbect Departments (Departments**)
Department class '#include #include #pragma 一次
using namespace std;
class Department
{
// overload input output streams
friend ostream &operator<<(ostream &, const Department &);
friend istream &operator>>(istream &, Department &);
private:
string name;
long id;
public:
static int departmentsCounter; // count num of elements
Department();
// set get block
void setId(long);
void setName(string);
string getName() { return name; }
long getId() { return id; }
// overload block
Department &operator=(const Department &); // instead copy constructor
bool operator>(const Department &) const; // check if count of students greater than count of students in another department
void operator+=(Department &); // add new course to course list of the student
void operator=(Department &); // remove course from course list of the student
~Department();
};`
部门 CPP
#include "Department.h"
int Department::departmentsCounter = 0;
Department::Department()
{
name;
id = 0;
}
void Department::setId(long _id)
{
id = _id;
}
void Department::setName(string _name)
{
name = _name;
}
// overload out put (without cource list)
ostream &operator<<(ostream &output, const Department &s)
{
output << "Department: nName " << s.name << "nid " << s.id << "n-------------------------------------------------n";
return output;
}
// overload input without check of right input data
istream &operator>>(istream &input, Department &s) {
cout << "nEnter name: ";
input >> s.name;
cout << "nEnter id: ";
input >> s.id;
return input;
}
Department &Department::operator=(const Department &s) {
Department temp;
name = s.name;
id = s.id;
return temp;
}
Department::~Department()
{}
在主函数中使用此 cout << *(departments_list)[size];
但它不起作用 内存错误 所以我不知道如何解决它。也许我应该在构造函数中分配内存,但我不知道我像名字一样使用的字符串长度。实际上我的问题是如何使用运算符重载到**部门,因为我不明白这边是如何工作的
如果你有一个或指针向量,正确的语法是:
cout << *(departments_list[size]);
*注意(
和)
的位置变化。
注意:以上假设size
是数组或向量限制内的索引变量。
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何显式调用运算符<<
- C++部门对齐阅读
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 我的尝试将零部门有问题吗?
- Bigint部门总是返回零
- 以部门为单位的类型转换
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化