请问一个很弱的问题:如何在构造函数中初始化成员
比如说:
class Graph
{
private string[] NodeList;
private int[,] Edge;
public Graph(int sz)
{
string[] NodeList = new string[sz];
for (int j = 0; j < sz; j++)
{
NodeList[j] = "";
}
int[,] Edge = new int[sz, sz];
for (int i = 0; i < sz; i++)
{
for (int j = 0; j < sz; j++)
{
Edge[i, j] = 0;
}
}
}
}
}
编译之后也能通过,但是有如下两个警告:
Warning 1 Field 'Traffic.Graph.NodeList' is never assigned to, and will always have its default value null E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs 10 26 Traffic
Warning 2 Field 'Traffic.Graph.Edge' is never assigned to, and will always have its default value null E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs 11 24 Traffic
然后我运行时程序就抛出异常了System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Traffic"
StackTrace:
at Traffic.Graph.InsertNode(String name) in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs:line 98
at Traffic.Form1.button2_Click(Object sender, EventArgs e) in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Form1.cs:line 65
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Traffic.Program.Main() in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Program.cs:line 19
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
我是green hand ,大家多帮忙啊,谢谢了
class Graph
{
private string[] NodeList;
private int[,] Edge;
public Graph(int sz)
{
string[] NodeList = new string[sz];
for (int j = 0; j < sz; j++)
{
NodeList[j] = "";
}
int[,] Edge = new int[sz, sz];
for (int i = 0; i < sz; i++)
{
for (int j = 0; j < sz; j++)
{
Edge[i, j] = 0;
}
}
}
}
}
编译之后也能通过,但是有如下两个警告:
Warning 1 Field 'Traffic.Graph.NodeList' is never assigned to, and will always have its default value null E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs 10 26 Traffic
Warning 2 Field 'Traffic.Graph.Edge' is never assigned to, and will always have its default value null E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs 11 24 Traffic
然后我运行时程序就抛出异常了System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Traffic"
StackTrace:
at Traffic.Graph.InsertNode(String name) in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Graph.cs:line 98
at Traffic.Form1.button2_Click(Object sender, EventArgs e) in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Form1.cs:line 65
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Traffic.Program.Main() in E:\Visual Studio 2005\Projects\Traffic(DS)\Traffic\Program.cs:line 19
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
我是green hand ,大家多帮忙啊,谢谢了
...全文
相关推荐
建立一个名为Student的类,该类有以下几个私有成员变量:学生姓名、学号、性别、年龄。还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。编写一个主函数。声明一个学学生对象。然后调用成员函数在屏幕输出学生信息。
C++考试题目:题目:建立一个名为Student的类,该类有以下几个私有成员变量:学生姓名、学号、性别、年龄。还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生
关于C++类的成员初始化列表的相关问题
初始化成员类(member classes)的构造函数:如果成员变量是另一个类的实例,那么在初始化列表中调用成员类的构造函数可以确保它被正确初始化,尤其是当成员类的构造函数有参数时。
C++类成员和数据成员初始化总结
这包括两步:1) 分配内存,然后隐式或显示地初始化每个数据成员;2) 进入构造函数体,执行一般的计算或操作。这意味着,数据成员的初始化主要在构造函数中完成。然而,有一些特殊的规则需要遵循:1.
成员初始化列表与构造函数体中的区别详细解析
成员初始化列表与构造函数体中的区别在于它们处理数据成员初始化的方式和效率。在C++中,构造函数的主要目的是初始化类的对象。初始化列表和构造函数体都可以用来设置成员变量的初始值,但它们在执行时间和效果上
c++ 子类构造函数初始化及父类构造初始化的使用
首先,我们需要了解构造函数的基本概念。构造函数是类的特殊成员函数,用于初始化类对象。如果在类中没有显式地声明构造函数,那么编译器会自动创建一个默认的构造函数。


