[英]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:
所以,选择是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.