我有以下代码:
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);
...
}
但是,我在 B *env = new B(astream, this);
行中收到以下错误:
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>&}’
我不确定如何解决这个问题,如果有任何意见,我将不胜感激。
最佳答案
指针和引用不是一回事。我可能会质疑你在这里做什么,但要解决你目前的问题,请执行以下操作:
B *env = new B(*astream, this);
当使用引用时(例如 std::ostream &
),普通变量的语法适用。
以后,您可以通过阅读错误信息来解决您的错误。错误“无已知转换”意味着您正试图将一种类型分配给另一种不兼容的类型。它告诉您两种类型(一种是指针,另一种是引用)。现在您对指针和引用有了更多的了解,希望您将来能够自己解决这些错误。 =)
关于c++ - 没有已知的从 std::ostream* 到 std::ostream& 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19060514/