我需要添加operator <
代码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是私密的。也许是这样吗?
有人可以帮忙吗?
我是一个初学者,所以:/

最佳答案

你要:

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>。在您的类内部-这些模板类型是可见的,因此您可以使用它们。

08-04 14:35