|
Please use the < tag when you need to display such characters, the most important part of your code (in this case) is unreadable.
But I guess (because some parts are missing), that you are writing outside the bounds of the array:
cin >> temp[element];
element is outside the bounds (remember that C and C++ use zero-indexed arrays, so element-1 is the last element of your array).
|
|
|
|
|
Ack, simple error....of course. Alrighty, now that *that* is taken care of, I've got yet another error. Gonna get it focused before I post it.
|
|
|
|
|
Revised code is as follows then
Header File :
#include <iostream>
using namespace std;
int i;
class Array
{
private:
int element;
int *array;
bool extend();
bool contract();
public:
int *temp;
int *temp2;
Array( );
Array(int array);
Array(int array, int elem);
~Array() {}
bool append();
bool chop();
bool print();
bool success;
};
/* Default Constructor
-Pre none
-Post *array set to 0
*/
Array :: Array ( )
{
int *array = 0;
}
/* Secondary Constructor
Initializes Dynamic Array
-Pre int elem set to a value
-Post *array set to dynamic array
*/
Array::Array (int elem)
{
element = elem;
array = new int[element];
for (i=0; i<element; i++)
{
cout << "Please enter element "<< i << " : " << endl;
cin >> array[i];
}
}
bool Array :: append()
{
success = false;
success = extend();
if (success = true)
return (true);
else
return (false);
}
bool Array:: extend()
{
element += 1;
temp = new int[element];
for (i=0; i<element; i++){
temp[i] = array[i];
}
cout << "Enter new element value : " << endl;
cin >> temp[element-1];
delete [] array;
array = new int[element];
for (i=0; i<element; i++){
array[i] = temp[i];
}
delete [] temp;
*temp = NULL;
return (true);
}
bool Array :: chop()
{
success = false;
success = contract();
if (success = true)
return (true);
else
return (false);
}
bool Array :: contract()
{
element -= 1;
temp2 = new int[element];
for (i=0; i<element; i++){
temp2[i] = array[i];
}
delete [] array;
array = new int[element];
for (i=0; i<element; i++){
array[i] = temp2[i];
}
delete [] temp2;
*temp2 = NULL;
return (true);
}
bool Array :: print()
{
for (i = 0; i < element; i++)
{
cout << "The value of element " << i << " is " << array[i] << endl;
}
return (true);
}
Main
#include <iostream>
#include "class.h"
using namespace std;
void main()
{
int n;
int elem;
bool completion = false;
cout << "Enter desired array size : " << endl;
cin >> elem;
Array ar1(elem);
completion = ar1.append();
if (completion == true)
{
cout << "Append successful!" << endl;
}
else
{
cout << "Append Unsuccessful!!" << endl;
}
completion = false;
completion = ar1.chop();
if (completion == true)
{
cout << "Chop successful!" << endl;
}
else
{
cout << "Chop Unsuccessful!!" << endl;
}
completion = false;
completion = ar1.print();
if (completion == true)
{
cout << "Print successful!" << endl;
}
else
{
cout << "Print Unsuccessful!!" << endl;
}
completion = false;
system ("pause");
}
I unfortunately get an access violation error, but if I force the debugger to continue, the program works. Which, is nice to know, but I still need to fix it
|
|
|
|
|
Use your debugger (F5 key) to locate the problem. You can put breakpoints in your code with F9 key. This is very usefull to watch your variables.
|
|
|
|
|
bool Array :: contract()
{
element -= 1;
temp2 = new int[element];
for (i=0; i<element; i++){
temp2[i] = array[i];
}
delete [] array;
array = new int[element];
for (i=0; i<element; i++){
array[i] = temp2[i];
}
delete [] temp2;
*temp2 = NULL;
return (true);
}
There's an access violation somewhere in the midst of this fun. Element is being properly decremented before its use with temp in creating the new array; in the assignment for loop, it will not go beyond the scope because element is the size of the smallest array. Everything seems pretty clear.
|
|
|
|
|
Well, I'm going to call it a night. If anyone has any further insight, I'll be back on in about six and a half hours to take a look. Peace.
|
|
|
|
|
JarethAshaer wrote: *temp2 = NULL;
This is wrong: you deleted temp2 so the memory has been freed. Then you try to write NULL in the 'variable' pointed by temp2. Remove the '*': you will write NULL in the pointer, not in the variable pointed by temp2.
|
|
|
|
|
Good thing I checked one last time before I called it a night! That fixed it! Thanks much
|
|
|
|
|
JarethAshaer wrote: Array :: Array ( )
{
int *array = 0;
}
I assume you mean this:
array = 0;
What you had does nothing. It sets a local pointer to zero which then goes out of scope.
Steve
-- modified at 3:26 Friday 5th May, 2006
|
|
|
|
|
Aye, took care of that along with Cedric's suggestion. Thanks though!
|
|
|
|
|
Yes sorry, that wasn't very clear: I said the constructors (so which means both of them ). But I agree, I should have been more explicit
|
|
|
|
|
What is the API to get the Current folder path where the application resides?
Thanks in advance.
Rinu
|
|
|
|
|
GetModuleFileName()
An advice: Read the faqs[^] first as this will answer most of your questions.
Nibu thomas
Software Developer
|
|
|
|
|
U can try
char *_getcwd( char *buffer, int maxlen );
|
|
|
|
|
You can try
char *_getcwd(
char *buffer,
int maxlen
);
This shoul solve your problem.
|
|
|
|
|
|
how to upload an image to the server and again store that in database using sockets or any other way
|
|
|
|
|
what type of serevr. Is it FTP or any other.
If you have to o upload to ftp then You need to use CFtpConnection and CInternetSession.
Cheers
"Peace of mind through Technology"
|
|
|
|
|
can anybody give the info related to USB Communication in vc++?
or any active x controls which continuosly polling for a usb device from the application, when the application is active
|
|
|
|
|
|
USB communications don't work like serial communications (RS-232). What you will need, is a device driver which is supplied with your hardware. Then, you need to communicate with your device through this device driver (you 'open the driver with CreateFile, then you get a handle and you can send commands).
So in brief, the documentation you are looking for is generally supplied with the driver.
|
|
|
|
|
i want to know the full details about BHO concept in VC++ MFC programming...
Ravi Chandran.C
|
|
|
|
|
|
unsigned int* link[200];
float* delms[200];
float* sds[200];
float* delmih[200];
float* sdih[200];
for (int i=0;i<200;i++)
{
link[i]=new unsigned int[4];
delms[i]=new float[4];
sds[i]=new float[4];
delmih[i]=new float[4];
sdih[i]=new float[4];
}
......................................codes initialized the above arrays with values
dlay_tme(delms,sds,delmih,sdih,nlinks,ndih,link,time,time_var);
//
//
delete [] *link;
delete [] *delms;
delete [] *sds;
delete [] *delmih;
delete [] *sdih;
I got the following error message, How to pass 2D dynamic array to the function ?? Please correct.
Thanks
blst_vi_procDlg.cpp
c:\blast_vib_proc_model_from_d\blst_vi_procdlg.cpp(522) : error C2664: 'void __thiscall CBlast_vib_procDlg::dlay_tme(float [][4],float [][4],float [][4],float [][4],unsigned int [],unsigned int [],unsigned int [][4],float [],float [])' : cannot
convert parameter 1 from 'float *[200]' to 'float [][4]'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
feature_display.cpp
Error executing cl.exe.
blast_vib_proc.exe - 1 error(s), 0 warning(s)
|
|
|
|
|
As an example you can allocate an array of array of ints, pass the array to a function which deletes it like this:
void initit()
{
int* stuff[100];
for (int i = 0; i < 100; i++)
{
stuff[i] = new int[5];
}
deleteit(stuff);
}
void deleteit(int* stuff[])
{
for (int i = 0; i < 100; i++)
delete [] stuff[i];
}
Haven't tested this thouroughly, but it should work. Please correct me if I'm wrong.
- S
50 cups of coffee and you know it's on!
|
|
|
|