Hi all,
I'm getting a runtime error for the below program, though it compiles
fine. The culprit seems to be the managed array--it doesn't exist.
What am I doing wrong?
RL
// OverL2_simple.cpp : main project file.
#include "stdafx.h"
#include "AClass.h"
using namespace System;
int main(array<System::String ^^args)
{
Console::WriteLine(L"Hello World");
AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
declaration/definition
for (int j=0; j<(A10->a1->Length); j++){
Console::WriteLine("a1 is: {0}", A10->a1[j]);
}
// Unhandled Exception: System.NullReferenceException: Object
reference not set to an instance of an object.
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
// Aclass.h
#pragma once
using namespace System;
ref class AClass
{
public:
AClass(void);
int pubint;
array<int>^ a1; //this array is causing problems
private:
int privint;
};
//////////////////////////////////////////////
// Aclass.cpp
#include "StdAfx.h"
#include "AClass.h"
//
AClass::AClass(void): pubint(1),privint(1)
{
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
}
/*
//compiles but give runtime error:
Unhandled Exception: System.NullReferenceException: Object reference
not set to an instance of an object.
*/ 3 1409
I'm getting a runtime error for the below program, though it compiles
fine. The culprit seems to be the managed array--it doesn't exist.
What am I doing wrong?
AClass ^A10 = gcnew AClass(); //see AClass.h, .cpp below for
>AClass::AClass(void): pubint(1),privint(1)
{
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
}
Subtle. in your constructor, you declare a local variable that has the same
name as your class member. I assume thatthis is copy and paste error.
you initialize it properly, but it hides your a1 member variable, which
remains uninitialized.
Then -in your test code- you dereference a1. since that is an unitialized
pointer, you will get an exception.
Change
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
to
a1 = gcnew array<int>(5);
for(int j=0; j<a1->Length; j++)
a1[j] = j;
to solve the problem
--
Kind regards,
Bruno van Dooren br**********************@hotmail.com
Remove only "_nos_pam"
Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
used managed arrays before and this being the first time, I did not
know the right format.
RL
Bruno van Dooren [MVP VC++] wrote:
Subtle. in your constructor, you declare a local variable that has the same
name as your class member. I assume thatthis is copy and paste error.
you initialize it properly, but it hides your a1 member variable, which
remains uninitialized.
Then -in your test code- you dereference a1. since that is an unitialized
pointer, you will get an exception.
Change
array<int>^ a1 = {1,2,3,4,5}; //compiles but give runtime error
to
a1 = gcnew array<int>(5);
for(int j=0; j<a1->Length; j++)
a1[j] = j;
to solve the problem
--
Kind regards,
Bruno van Dooren br**********************@hotmail.com
Remove only "_nos_pam"
raylopez99 wrote:
Muchos gracias Bruno van Dooren! Indeed this is the case--I have never
used managed arrays before and this being the first time, I did not
know the right format.
In C++/CLI, you can initialize the array at dynamic construction, which
is a really nice feature:
a1 = gcnew array<int>(5) {1,2,3,4,5};
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: simondex |
last post by:
Hi, Everyone!
Does anyone know how to initialize an int array with a non-zero number?
Thank You Very Much.
Truly Yours, Simon Dexter
|
by: Ekim |
last post by:
hello,
I'm allocating a byte-Array in C# with byte byteArray = new byte;
Now I want to pass this byte-Array to a managed C++-function by reference,
so that I'm able to change the content of the...
|
by: Omid Hodjati |
last post by:
Hi All,
I implemented an encryption algorithm using C#, native C++
and managed C++. Then I measured the CPU time used for
executing this algorithm in all implementation. The
managed version of...
|
by: Mirek Rewak |
last post by:
Hello,
I have to move the project to the .NET (exactly ASP.NET) environment but
I encountered some problems. The old code was written using C++ and MFC
and was organized to separate "business...
|
by: Johann Blake |
last post by:
I need to call a C DLL function. The first parameter expects a pointer
to a long. It returns a value at the address of the pointer. The second
parameter expects a pointer to a pointer. It creates...
| |
by: Sharon |
last post by:
I Have a PC with dual XEON CPU’s and 4 Giga Byte RAM win Windows XP Pro.
I have 2 problems with it:
(1) Windows is showing only 3.25 Giga byte on the System Properties ?
General tab.
While the...
|
by: WesIdell |
last post by:
Hi All:
I need some advice regarding a problem that I'm experiencing. I'm using
a group of TextBox controls in the .aspx page and am using a function
in the .cs code to perform some actions with...
|
by: WaterWalk |
last post by:
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other...
|
by: Jai Prabhu |
last post by:
Hi All,
Consider the following piece of code:
void func (void)
{
static unsigned char arr = "\x00\xAA\xBB";
fprintf (stderr, "0x%x\n", arr);
fprintf (stderr, "0x%x\n", arr);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |