MFC 结构指针 全局变量

来源:百度知道 编辑:UC知道 时间:2024/05/29 08:29:20
我要做一个与图有关的小程序!要设置一个指针的全局变量,尝试了很多方法,都失败了啊!有没有高人指点一下!
//#include "stdafx.h"
//include "graph_struct.h"(包含结构Mgraph的定义)

extern struct Mgraph * G;

//#include "stdafx.cpp"
struct Mgraph * G = NULL;

但是早其他类中用new给G开辟空间的话,就会提示错误!
//#include "creategraph.cpp"
G = new Mgraph;
错误提示:
error C2512: 'Mgraph' : no appropriate default constructor available

若用malloc开辟的话,也有错误
G = (struct Mgraph*)malloc(sizeof(Mgraph));
错误提示:
error C2027: use of undefined type 'Mgraph'
: see declaration of 'Mgraph'

在结构体里设计一个不带参数的构造函数。。C++的结构体可以有函数的。。,然后在你要用的地方包含这个头文件。。。不过我还发现你的一个问题:#include "creategraph.cpp"。。。这个怎么不是包含头文件呢。。一般都不是包含实现文件的。。

最简单的办法

把 struct Mgraph * G放到 #include "stdafx.h" 里

这根本不是什么指针的问题, 而是你不带参数构造 struct Mgraph, 而又没给他写无参数的构造函数