vs2010写代码时,出现了问题,求解答!!在线等

zwd1994 2015-01-13 03:33:27
我使用MFC写一个代码,里面有几个类,为:Cmannager ,mannager, Cregister。然后在Cregister中代码点击ok代码如下:
void Cregist::OnBnClickedOk()
{
// TODO: 注册 用户名user 密码password
UpdateData();
char user[20],password[15];
mannager mann1;

Cmannage manna;
if(USER.IsEmpty())
{
MessageBox(_T("用户名不能为空!"));
return;
}
if(!USER.IsEmpty())
{
if(PASSWORD.IsEmpty())
{
MessageBox(_T("密码不能为空"));
return;
}
}
sprintf(user,"%s",USER);
sprintf(password,"%s",PASSWORD);
mann1.add_s(user,password);



manna.DoModal();
CDialog::OnOK();
}


调用了另一个对话框,Cmannager,里面的ok代码如下:
void Cmannage::OnBnClickedOk()
{
// TODO: Jizhong 集中式,Fensan 分散式,Sfensan受限分散
UpdateData();
mannager test;
if(Choose==0)
{


test.choose_fensan(); -----------------------------------------此处是出问题的地方
}
else if(Choose==1)
{
MessageBox(_T("1"));
}
else if(Choose==2)
{
MessageBox(_T("2"));
}
else
{
MessageBox(_T("必须选取一项!"));
return;
}


CDialog::OnOK();
}
mannager里面主要的几个函数和结构:
public:
struct s_table /*主体表*/
{
char sname[20];
char password[15];
int num_o;
char info[100];
int mannage;/*1为集中式,2为分散式,3为受限分散式*/
struct s_table *next;
};
struct s_table *s;


void mannager::choose_jizhong()
{
(s->mannage)=1; -------------------------------------出问题的地方

}


在此之前已经已经经过该函数处理:
int mannager::add_s(char name[20],char password[15])
{
struct s_table *p;
if(s==NULL)
{
p=(struct s_table *)malloc(sizeof(struct s_table));
strcpy(p->sname,name);
strcpy(p->password,password);
p->num_o=0;
p->mannage=1;
p->next=NULL;
s=p;
return 1;
}
p=(struct s_table *)malloc(sizeof(struct s_table));
strcpy(p->sname,name);
strcpy(p->password,password);
p->num_o=0;
p->mannage=1;
p->next=s;
s=p;
return 1;
}
编译都没有问题,但是运行到要用到上面的时候,出现问题
mannageCXX0030: 错误: 无法计算表达式的值
我查了一下,那个是没有分配空间。但是之前我已经分配了,经过了那个函数的处理。
希望各位大神能帮我解答一下!!!谢谢!!!
...全文
335 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-01-13
  • 打赏
  • 举报
回复
在构造函数中s=NULL;再试试看。
赵4老师 2015-01-13
  • 打赏
  • 举报
回复
使用锁或临界区确保类创建的顺序。 或者将程序主循环所用的所有类的实例都放在一起且最好在初始化时统一按既定顺序实例化一遍。 使用TRACE宏辅助调试MFC程序。 TRACE TRACE( exp ) Parameters exp Specifies a variable number of arguments that are used in exactly the same way that a variable number of arguments are used in the run-time function printf. Remarks Provides similar functionality to the printf function by sending a formatted string to a dump device such as a file or debug monitor. Like printf for C programs under MS-DOS, the TRACE macro is a convenient way to track the value of variables as your program executes. In the Debug environment, the TRACE macro output goes to afxDump. In the Release environment, it does nothing. TRACE is limited to sending a total of 512 characters at a time. If you call TRACE with formatting commands, the total string length after the formatting commands have been expanded cannot be more than 512 characters, including the terminating NULL. Exceeding this limit causes an ASSERT. Note This macro is available only in the debug version of MFC. For more information, seeMFC Debugging Support in Visual C++ Programmer’s Guide. Example // example for TRACE int i = 1; char sz[] = "one"; TRACE( "Integer = %d, String = %s\n", i, sz ); // Output: 'Integer = 1, String = one' See Also TRACE0, TRACE1, TRACE2, TRACE3, AfxDump, afxTraceEnabled
zwd1994 2015-01-13
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
跨类的全局变量要在头文件中声明。 跨类的全局指针变量所指内存要在程序初始化时分配。 多线程的话,还要加锁。
那您的意思是,如果我要在不同的类里面使用另一个类里面的一个数据,那就要在那个类里面的.h文件定义那些数据的变量,而不是在.h的class里面定义?
赵4老师 2015-01-13
  • 打赏
  • 举报
回复
跨类的全局变量要在头文件中声明。 跨类的全局指针变量所指内存要在程序初始化时分配。 多线程的话,还要加锁。
zwd1994 2015-01-13
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
struct s_table *;; 改为 struct s_table *s=NULL;
在同一个类里面,引用mannager里的数据是共享的,然后里面的s也是分配了空间的,但是用另外一个类,想对刚刚经过处理的数据进行操作时,就会出现上面的问题。
zwd1994 2015-01-13
  • 打赏
  • 举报
回复
我改了已经,但是还会报同样的错误。
赵4老师 2015-01-13
  • 打赏
  • 举报
回复
struct s_table *;; 改为 struct s_table *s=NULL;

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧