我在下面的例子中遇到了diffuculty,最后一行产生了“abort has been called”错误。我不明白为什么会这样。std ::向量中止问题
为了清晰起见,我使用(* abc).def而不是abc-> def。
#include <iostream>
#include <string>
#include <vector>
class branch
{
public:
unsigned short n;
std::vector<branch> branches;
branch left()
{
return branches.at(0);
}
};
void main()
{
branch trunk = branch();
trunk.n = 0;
branch b1, b2;
b1.n = 0;
b2.n = 5;
b1.branches.push_back(b2);
trunk.branches.push_back(b1);
branch* focus1 = &(trunk.branches.at(0));
branch* focus3 = &(trunk.left());
std::cout<<trunk.left().branches.at(0).n<<std::endl; // ok
std::cout<<(*focus1).branches.at(0).n<<std::endl; // ok
std::cout<<(*focus1).left().n<<std::endl; // ok
std::cout<<(*focus3).branches.at(0).n<<std::endl; // problem
}
什么操作系统?什么编译器?另外 - >比(* p)更清晰.def – 2011-01-05 23:49:09
Windows Vista和VSC++ 2010 – alan2here 2011-01-05 23:57:46