为什么编译器不显示这些返回类型的错误?
使用最新版本的 NetBeans 运行 Ubuntu 11.10 的默认安装。我有类似以下内容:
class MyClass {
public:
Type1 RunAlgo();
private:
Type2 Run();
}
Type1 MyClass::RunAlgo() {
//additional code
return Run();
}
Type2 Run() {
//additional code
Type2 obj;
return obj;
}
Type1 和 Type2 完全不相关。我在编写 Run() 方法时在返回类型中犯了一个拼写错误,发现了这一点,并对它的编译结果感到惊讶。我只是想知道为什么这不会返回错误并且编译正常?我缺少什么?
编辑:新样本。作为一个独立的项目,这确实会产生错误。似乎无法发现为什么真正的项目确实可以编译。
class Node { };
//only difference here is that in my code I have a custom comparer
typedef map<Node*, map<Node*, double> > Network;
class HMM {
Network _network;
};
class Algorithm {
public:
HMM RunAlgo();
private:
Network _network;
Network Run();
};
HMM Algorithm::RunAlgo() {
return Run();
}
Network Algorithm::Run() {
return _network;
}
EDIT2:
我为我的问题和例子表述不当表示歉意。以后我会更加注意示例。我已经工作了 10 多个小时,却失去了注意力。以下示例重现了我的情况:
#include <map>
using std::map;
class Node {
};
typedef map<Node*, map<Node*, double> > Network;
class HMM {
public:
HMM(const Network& network) {};
Network _network;
};
class TestClass {
public:
HMM RunAlgo(int x, int y);
private:
Network _network;
Network Run();
};
HMM TestClass::RunAlgo(int x, int y) {
return Run();
}
Network TestClass::Run() {
return _network;
}
将特定构造函数添加到 HMM 类后,它可以毫无问题地进行编译。我不知道可以这样做,因为这是我第一次遇到这种情况。如果我浪费了您的时间,我再次表示歉意,并且感谢您尝试帮助我。
Running a default installation of Ubuntu 11.10 with the latest version of NetBeans. I have something similar to the following:
class MyClass {
public:
Type1 RunAlgo();
private:
Type2 Run();
}
Type1 MyClass::RunAlgo() {
//additional code
return Run();
}
Type2 Run() {
//additional code
Type2 obj;
return obj;
}
Type1 and Type2 are completely unrelated. I came upon this by making a typo in the return type when I was writing the Run() method and was amazed that it compiled. I am just wondering why this does not return an error and just compiles fine? What am I missing?
EDIT: New sample. This does generate an error as a stand alone project. Can't seem to spot why the real project would indeed compile.
class Node { };
//only difference here is that in my code I have a custom comparer
typedef map<Node*, map<Node*, double> > Network;
class HMM {
Network _network;
};
class Algorithm {
public:
HMM RunAlgo();
private:
Network _network;
Network Run();
};
HMM Algorithm::RunAlgo() {
return Run();
}
Network Algorithm::Run() {
return _network;
}
EDIT2:
I apologize for my badly formulated question and example. I will be more careful in the future about examples. I've been working for a bit over 10 hours and lost focus. The following example reproduces my case:
#include <map>
using std::map;
class Node {
};
typedef map<Node*, map<Node*, double> > Network;
class HMM {
public:
HMM(const Network& network) {};
Network _network;
};
class TestClass {
public:
HMM RunAlgo(int x, int y);
private:
Network _network;
Network Run();
};
HMM TestClass::RunAlgo(int x, int y) {
return Run();
}
Network TestClass::Run() {
return _network;
}
After adding that specific constructor to the HMM class it compiles without problems. I didn't know this could be done as this is the first time I encounter this case. Again I apologize if I wasted your time and I appreciate you trying to help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复非测试用例中的错误后,我的编译器确实出错了。
您关于
Type1
和Type2
不相关的说法一定是错误的。下次注意真实的测试用例。
After fixing the mistakes in your non-testcase, my compiler does error out.
Your statement that
Type1
andType2
are unrelated must be false.Take care on a real testcase next time.
您没有显示您的实际代码;您给我们的示例无法编译(Debian/Sid/AMD64 上的 GCC 4.6),
但是当您涉及转换或转换时,您所描述的情况可能会发生。您应该显示您的实际代码(或显示症状的简化代码)以获得真正的帮助。
You didn't show your actual code; the example you gave us don't compile (GCC 4.6 on Debian/Sid/AMD64)
But what you describes may happen when you have conversions or casting involved. You should show your actual code (or a simplified code which exhibits the symptoms) to get real help.