Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法访问模板类中的友元函数_C++_Friend_Ostream - Fatal编程技术网

C++ 无法访问模板类中的友元函数

C++ 无法访问模板类中的友元函数,c++,friend,ostream,C++,Friend,Ostream,Pairwise类表示一个带有key:value的对。我已经创建了一对模板,在尝试使用类的键和值输入运行并将其打印出来时出错 鉴于我的主要观点: #include "file_name.h" int main (){ Pairwise<string, string> example = {{"key", "value"}}; cout << example << endl; } 但是,我得到了一个错误: h:28:11: error: '

Pairwise类表示一个带有key:value的对。我已经创建了一对模板,在尝试使用类的键和值输入运行并将其打印出来时出错

鉴于我的主要观点:

#include "file_name.h"

int main (){
    Pairwise<string, string> example = {{"key", "value"}};
    cout << example << endl;
 }
但是,我得到了一个错误:

h:28:11: error: 'std::basic_ostream<_CharT, _Traits> is protected within..."
h:28:11:错误:'std::basic_ostream在…中受保护。'

编写时,您将运算符定义为一个成员函数,这很可能是无意的。将其像

template<typename K, typename V>
struct Pairwise{
    K first;
    V second;
    Pairwise() = default;
    Pairwise(K, V);
    //print out as a string in main
    friend ostream& operator<<(ostream &out, const Pairwise &n);
};

template<typename K, typename V>
ostream& operator<<(ostream &out, const Pairwise<K,V> &n) {
    ...
    return out;
}
模板
结构成对{
K首先;
V秒;
成对()=默认值;
成对(K,V);
//在main中以字符串形式打印
friend ostream&运营商
h:25:59:friend declaration delares是一个非模板函数

您缺少将函数声明为成对执行
的模板:

标题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 */
#如果包含标题/*或pragma一次*/
#如果喜欢,请定义标题_H_INCLUDED/**/
#包括//或
样板
类成对{//使其成为一个类,以便
K first;//朋友实际上是有道理的。
V秒;
公众:
成对()=默认值;
成对(K第一,V第二)
:第一{first},第二{second}
{}
样板

朋友STD::OcStand运算符

C++,较少的往往更多……/P>

#pragma once

#include<iostream>
#include<string>

// never do this in a header file:
// using std::ostream; 

template<typename K, typename V>
struct Pairwise{
    K first;
    V second;
    Pairwise() = default;
    Pairwise(K, V);
    //print out as a string in main
    friend std::ostream& operator<<(std::ostream &out, const Pairwise &n) {
        return out << n.first << ':' << n.second;
    }
};

int main (){
    using std::cout; using std::endl; using std::string;

    Pairwise<string, string> example = {"key", "value"};
    cout << example << endl;
 }
#pragma一次
#包括
#包括
//切勿在头文件中执行此操作:
//使用std::ostream;
样板
结构成对{
K首先;
V秒;
成对()=默认值;
成对(K,V);
//在main中以字符串形式打印

friend std::ostream&operator在运算符之前有一个模板在friend声明前面不是缺少了一个
模板吗?例如
模板friend ostream&operator非常感谢,这澄清了我对friend运算符的理解!顺便问一下,你能不能快速浏览一下我的main?如果值是以corr形式传递的通常在编写时,您将运算符定义为成员函数–带有
friend
的定义从不定义成员。@箭鱼指的是警告“h:25:59:friend declaration delares是非模板函数…”。。。“。你介意在另一篇回复文章中写下你将如何实现这一更改吗?这似乎导致我无法准确地打印出我的输出“key:value”那个流插入器有点复杂。
out
ostream-oss;
是一个打字错误,对吧?应该是
ostringstream-oss;
。错误消息被截断了。不要总结;发布你能找到的显示问题的最小代码,并引用整个错误消息。这里没有任何保护,所以这个流插入器的位ror消息需要上下文才能理解。此外,它不必要地复杂…
oss@PeteBecker真棒,缩短的代码对我总是很有帮助!@swardfish感谢你指出“+”运算符。我一直在寻找改进代码的方法。你认为我的主函数看起来还不错吗?将值传递给临时函数晚班
#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';
}
{
    using Stringpair = Pairwise<std::string, std::string>;
    // ...
    Stringpair sp{ "foo", "bar" };
}
#pragma once

#include<iostream>
#include<string>

// never do this in a header file:
// using std::ostream; 

template<typename K, typename V>
struct Pairwise{
    K first;
    V second;
    Pairwise() = default;
    Pairwise(K, V);
    //print out as a string in main
    friend std::ostream& operator<<(std::ostream &out, const Pairwise &n) {
        return out << n.first << ':' << n.second;
    }
};

int main (){
    using std::cout; using std::endl; using std::string;

    Pairwise<string, string> example = {"key", "value"};
    cout << example << endl;
 }