我需要添加运算符<<以使我的cout正常工作。
代码template.cpp:
#include "maptemplate.h"
int main(void)
{
typedef unsigned int ID; //Identification number of Employee
map_template<ID,Employee> Database; //Database of employees
Database.Add(761028073,Employee("Jan Kowalski","salesman",28)); //Add first employee: name: Jan Kowalski, position: salseman, age: 28,
Database.Add(510212881,Employee("Adam Nowak","storekeeper",54)); //Add second employee: name: Adam Nowak, position: storekeeper, age: 54
Database.Add(730505129,Employee("Anna Zaradna","secretary",32)); //Add third employee: name: Anna Zaradna, position: secretary, age: 32
//cout << Database << endl; //Print databese
//map_template<ID,Employee> NewDatabase = Database; //Make a copy of database
Employee* pE;
pE = Database.Find(510212881); //Find employee using its ID
pE->Position = "salesman"; //Modify the position of employee
pE = Database.Find(761028073); //Find employee using its ID
pE->Age = 29; //Modify the age of employee
//Database = NewDatabase; //Update original database
///cout << Database << endl; //Print original database
cout<<"Wszystko dziala"<<endl;
}
代码:template.h
#include <iostream>
#include <vector>
using namespace std;
// Początek klasy Employee bez template
class Employee
{
private:
public:
Employee(string Name, string Position, int Age);
string Name;
int Age;
string Position;
}; // koniec klasy employee
// Dodanie pól Name, Age, Position
Employee::Employee(string Name, string Position, int Age)
{
this->Name = Name;
this->Age = Age;
this->Position = Position;
}
template <class Key, class T> // template <klucze, dane pracownikow>
class map_template
{
private:
vector<Key> keys; // vector do przechowywania unikalnych kluczy pracowników
vector<T> content; // vector do przechowywania danych pracowników
public:
map_template()
{
}
void Add(Key key, T t);
T* Find(Key key);
}; // koniec klasy map_template
// Dodanie do bazy (Add)
template <class Key, class T>
void map_template<Key, T>::Add(Key key, T t)
{
keys.push_back(key);
content.push_back(t);
}
// Szukanie w bazie (Find)
template <class Key, class T>
T* map_template<Key, T>::Find(Key key)
{
for (unsigned int i = 0; i < keys.size(); i++)
if (keys[i] == key)
{
return &content.at(i);
}
return nullptr;
}
我在想应该如何看待它。这是我第一次使用模板,所以我不知道operator <<应该是什么样子。
正在考虑类似的东西:
friend ostream & operator<< (ostream & s, teamplate<something>);
但是真的不知道如何添加它。 我需要
cout << Database << endl;
正常工作。 抱歉,有些波兰语评论。
编辑:
我尝试将此朋友声明放入Employee类,但我不断收到错误消息:( 代码:
class Employee
{
private:
public:
Employee(string Name, string Position, int Age);
string Name;
int Age;
string Position;
friend ostream &operator << (ostream &out, const map_template<Key, T> &map);
}
错误:
maptemplate.h:49:63: error: ‘Key’ was not declared in this scope
friend ostream &operator << (ostream &out, const map_template<Key, T> &map);
^~~
maptemplate.h:49:68: error: ‘T’ was not declared in this scope
friend ostream &operator << (ostream &out, const map_template<Key, T> &map);
^
maptemplate.h:49:69: error: template argument 1 is invalid
friend ostream &operator << (ostream &out, const map_template<Key, T> &map);
^
maptemplate.h:49:69: error: template argument 2 is invalid
maptemplate.h: In instantiation of ‘std::ostream& operator<<(std::ostream&, const
map_template<Key, T>&):
我应该更改声明还是什么?我知道Key和T是私密的。也许是这样吗? 有人可以帮忙吗? 我是初学者,所以:/
答案 0 :(得分:2)
您要
template <class Key, class T>
std::ostream& operator << (std::ostream& out, const map_template<Key, T>& map) {
// your logic on how to output the map here...
return out;
}
因此您的operator<<
将与任何专业化的map_template
一起使用。
很显然,您的friend
方法也适用,而且实际上您会删除不必要的template
行。如果您将operator <<
声明放在您的班级中,请执行以下操作:
friend std::ostream& operator << (std::ostream& out, const map_template<Key, T>& map) {
// your logic on how to output the map here..
return out;
}
就可以了。请注意,缺少重复的template <class Key, class T>
。在您的班级内部-这些模板类型可见,因此您可以使用它们。