ч: 25: 59: объявление друга отменяет не шаблонную функцию.
Вы не можете объявить функцию как шаблон, который принимает Pairwise<K, V>
:
header.h:
#ifndef HEADER_H_INCLUDED /* or pragma once */
#define HEADER_H_INCLUDED /* if you like it */
#include <iostream> // or <ostream>
template<typename K, typename V>
class Pairwise { // made it a class so that the
K first; // friend actually makes sense.
V second;
public:
Pairwise() = default;
Pairwise(K first, V second)
: first{ first }, second{ second }
{}
template<typename K, typename V>
friend std::ostream& operator<<(std::ostream &out, Pairwise<K, V> const &p)
{
return out << p.first << ": " << p.second;
}
};
#endif /* HEADER_H_INCLUDED */
исходный файл:
#include <iostream> // the user can't know a random header includes it
#include <string>
#include "header.h"
int main()
{
Pairwise<std::string, std::string> p{ "foo", "bar" };
std::cout << p << '\n';
}
Sidenote: Вы также можете использовать
{
using Stringpair = Pairwise<std::string, std::string>;
// ...
Stringpair sp{ "foo", "bar" };
}
, если вам это нужно чаще.
Другие ошибки, которые вы получили в результате смешения std::ostringstream
с std::ostream
в operator<<()
.