我试图在C++,Visual Studio 2010中从外部库定义一个变量.它只有在我把它放在main函数之外时才有效.
此代码崩溃:
#include "StdAfx.h"
#include <ogdf\basic\Graph.h>
#include <ogdf\basic\graph_generators.h>
int main()
{
ogdf::Graph g;
ogdf::randomSimpleGraph(g, 10, 20);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给了我一个未经处理的例外:访问冲突.但是,如果它在main函数之外,它的工作没有任何问题:
#include "StdAfx.h"
#include <ogdf\basic\Graph.h>
#include <ogdf\basic\graph_generators.h>
ogdf::Graph g;
int main()
{
ogdf::randomSimpleGraph(g, 10, 20);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你有什么解决办法吗?我认为,这是由某种链接问题引起的.
编辑:看起来问题不是变量的初始化.当应用程序退出时,它会抛出异常.
int main()
{
ogdf::Graph g; // No problem
ogdf::randomSimpleGraph(g, 10, 20); // No problem
int i; // No problem
std::cin>>i; // No problem
return 0; // Throws an exception after read i;
Run Code Online (Sandbox Code Playgroud)
}
调用堆栈: 
输出为:graphs.exe中的0x0126788f处的第一次机会异常:0xC0000005:访问冲突写入位置0x00000000.
graphs.exe中0x0126788f处的未处理异常:0xC0000005:访问冲突写入位置0x00000000.