简体   繁体   English

没有已知的从std :: ostream *到std :: ostream&的转换

[英]No known conversion from std::ostream* to std::ostream&

I have the following code: 我有以下代码:

class A {
public:
    ...
    C *func() { ... }
    void func2() { ... }
    ...
};

class B {
public:
    ...
    B(std::ostream &s, A *curr);
    ...
};

class C {
public:
    ...
    ostream *stream;
    ...
}

void A::func2() {
    ...
    std::ostream *astream = func()->stream;
    B *env = new B(astream, this);
    ...
}

However I am getting the following error on the B *env = new B(astream, this); 但是我在B *env = new B(astream, this);上收到以下错误B *env = new B(astream, this); line: 线:

myfile.cc:680:86: error: no matching function for call to ‘B::B(std::ostream*&, A* const)’
myfile.cc:680:86: note: candidates are:
myfile.h:194:2: note: B::B(std::ostream&, A*)
myfile.h:194:2: note:   no known conversion for argument 1 from ‘std::ostream* {aka std::basic_ostream<char>*}’ to ‘std::ostream& {aka std::basic_ostream<char>&}’

I'm not sure how to solve this issue and would appreciate any input. 我不知道如何解决这个问题,并希望得到任何意见。

Pointers and references are not the same thing. 指针和引用不是一回事。 I might question exactly what you're doing here, but to solve your problem as it stands, do this: 我可能会质疑你在这里做了什么,但要解决你的问题,请执行以下操作:

B *env = new B(*astream, this);

When using a reference ( eg std::ostream & ), the syntax of normal variables applies. 使用引用( 例如 std::ostream & )时,将应用正常变量的语法。

In future, you can work out your mistake by reading the error message. 将来,您可以通过阅读错误消息来解决您的错误。 The error "no known conversion" means you are trying to assign one type to another type that is incompatible. 错误“无已知转换”表示您尝试将一种类型分配给另一种不兼容的类型。 It tells you the two types (one is a pointer and the other is a reference). 它告诉你两种类型(一种是指针,另一种是引用)。 Now you know a little more about pointers and references, you will hopefully pick these errors up yourself in future. 现在您对指针和引用有了更多了解,您希望将来能够自己选择这些错误。 =) =)

"astream" is a pointer. “astream”是一个指针。 B() constructor expects a reference. B()构造函数需要引用。 So, the choice is: 所以,选择是:

  • Convert everything to pointers or references 将所有内容转换为指针或引用
  • Apply dereferenced pointer whenever you need a reference: *astream 每当需要引用时应用解除引用的指针:* astream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;运算符&lt;&lt;(std :: ostream&,int&)&#39;不明确 - 'operator<<(std::ostream&, int&)’ is ambiguous 无法将std :: cout传递给ostream&构造函数 - Cannot pass std::cout to ostream& constructor std :: ostream&运算符&lt;&lt;类型推导 - std::ostream& operator<< type deduction -&gt; std :: ostream&是什么意思? - What does ->std::ostream& mean? “friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list)”是什么意思? - What does “friend std::ostream& operator<<(std::ostream& out, LinkedList& list)” mean? std :: ostream&运算符&lt;&lt;(std :: ostream&sstr,const T&val)的模棱两可的重载 - Ambiguous overload of std::ostream& operator<<(std::ostream& sstr, const T& val) std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const T&amp;) 未被覆盖 - std::ostream& operator<<(std::ostream&, const T&) not being overridden 我可以将 std::ostream&amp; 传递给期望 std::ofstream 的 function - can i pass std::ostream& to a function expecting std::ofstream 有关初始化类型为“ std :: ostream”的临时类的非常量引用“ std :: ostream&”的C ++编译器错误 - C++ compiler error regarding initialization of a non-const reference of type 'std::ostream&' from a temporary of type 'std::ostream" 为什么编译器现在接受从 std::stringstream::operator&lt;&lt;() 返回的 std::ostream&amp; 上调用 str() 成员? - Why do compilers now accept to call str() member on a returned std::ostream& from std::stringstream::operator<<()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM