我对C++相当陌生,而且我被这个问题困扰了一下。我想从另一个类的方法调用一个变量,但它总是segfaults。我的代码编译没有警告,我已经检查过所有变量在gdb中都是正确的,但函数调用本身似乎会导致段错误。我使用的代码大致如下:在调用方法时出现Segfault错误C++
class History{
public:
bool test_history();
};
bool History::test_history(){
std::cout<<"test"; //this line never gets executed
//more code goes in here
return true;
}
class Game{
private:
bool some_function();
public:
History game_actions_history;
};
bool Game::some_function(){
return game_actions_history.test_history();
}
任何提示或建议非常感谢!
编辑:我编辑的代码,所以没有更多的local_variable和值直接返回。但它仍然存在段错误。至于发布实际的代码,它相当大,我应该发布哪些部分?
我们需要看到真正的代码。 – 2010-04-07 15:20:01
'local_variable'不是很局部,是吗? :)这是一个成员变量。另外,测试一个bool来返回这个bool有点多余:'return local_variable;'。当然,你并不需要首先存储它:'return game_actions_history.test_history();'无论如何,这段代码是“很好”的。你可以做一个'Game g;'但是你不能调用'g.some_function();',它是私有的。如果不是,它会正常工作。 – GManNickG 2010-04-07 15:20:15
你怎么称呼它? – EFraim 2010-04-07 15:20:51