我有以下代码:
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>&}’我不知道如何解决这个问题,并希望得到任何意见。
发布于 2013-09-27 21:43:37
指针和引用不是一回事。我可能会问你在这里到底在做什么,但要解决你目前的问题,请这样做:
B *env = new B(*astream, this);当使用引用(如std::ostream &)时,将应用普通变量的语法。
将来,你可以通过阅读错误信息来解决你的错误。错误“无已知转换”意味着要将一种类型分配给另一种不兼容的类型。它告诉您这两种类型(一种是指针,另一种是引用)。现在,您对指针和引用有了更多的了解,希望您将来能够自己发现这些错误。=)
发布于 2013-09-27 21:45:55
"astream“是一个指针。B()构造函数需要一个引用。因此,我们的选择是:
https://stackoverflow.com/questions/19060514
复制相似问题