Hi all, I'm having a similar problem with a code of mine. <<note: split from here>>

In my code I have a class in which I am trying to implement a dynamic array of structs. My compiler does not seem to like my deconstructor, and complains that my dynamic array was not declared in that scope. The code is pretty sparse as I'm just trying to ensure I understand proper declarations.

I've tried writing the same code with dynamic array of ints instead (for practice), and it compiled without complaint.

Any insight you all can offer would be greatly appreciated, as I'm fairly new to OO.

#include<iostream>

using namespace std;

struct COORD_STRUCT 				// Structure to hold X,Y, and Z coordinates for spheres
{

	double X;
	double Y;
	double Z;

};


class HARD_SPHERE_COORDS 			// Class to contain all system's spheres and operate on them
{
	
	
	int N;					// System size
	COORD_STRUCT *COORDINDATES;		// Pointer to array to hold system's coordinates
	
	public:
	
	HARD_SPHERE_COORDS(); 			// Constructor
	~HARD_SPHERE_COORDS();			// Deconstructor

};



int main()
{
	// empty for now
return 0;	
}



HARD_SPHERE_COORDS::HARD_SPHERE_COORDS()	// Set array to hold N spheres
{
	int N;
	COORD_STRUCT * COORDINATES;
	COORDINATES = new COORD_STRUCT[N];
}



HARD_SPHERE_COORDS::~HARD_SPHERE_COORDS()	// Cleanup memory

	delete [] COORDINATES;
}

Edited 6 Years Ago by Nick Evan: Added backlink

Thanks for the catch. I fixed that and am still getting the same error:

error: ‘COORDINATES’ was not declared in this scope

hi lindsay,
i will suggest you to use vector instead of dynamic array for your structure. here is the code

#include<iostream>
#include <vector.h> 
using namespace std;
 
struct COORD_STRUCT 				// Structure to hold X,Y, and Z coordinates for spheres
{ 
	double X;
	double Y;
	double Z;
        COORD_STRUCT()
        {
        } 
};
 
 
class HARD_SPHERE_COORDS 			// Class to contain all system's spheres and operate on them
{ 
	int N;					// System size
	vector<COORD_STRUCT> COORDINDATES;	// vector declaration
 
	public:
 
	HARD_SPHERE_COORDS(); 			// Constructor
	~HARD_SPHERE_COORDS();			// Deconstructor 
};
  
int main()
{
	// empty for now
return 0;	
}
 
 
HARD_SPHERE_COORDS::HARD_SPHERE_COORDS()	// Set array to hold N spheres
{
	int N;
        for(int i=0; i<N; i++)  { //creating array of structure
           COORDINDATES.push_back(COORD_STRUCT());
        }
        // if you want to add two more you can
       COORDINDATES.push_back(COORD_STRUCT());
       COORDINDATES.push_back(COORD_STRUCT());

       //you want to access first index struct variable
       COORDINDATES[0].X = 20.03;
       //you want to see how big is your array of struct (vector)
       int structsize = COORDINDATES.size();
}
 
HARD_SPHERE_COORDS::~HARD_SPHERE_COORDS()	// Cleanup memory  
{        
       //you want to delete 10th index
       COORDINDATES.erase(COORDINDATES.begin()+10); 	
      //you want to delete all the index
 COORDINDATES.erase(COORDINDATES.begin(),COORDINDATES.end()); 
       //or
        COORDINDATES.clear();
}

this is much easier and very flexible then dynamic array allocation.

cheers,
Aburik.

Edited 6 Years Ago by play_c: n/a

class HARD_SPHERE_COORDS 			// Class to contain all system's spheres and operate on them
{
        ...
	COORD_STRUCT *COORDINDATES;		// Pointer to array to hold system's coordinates
        ...
}

HARD_SPHERE_COORDS::~HARD_SPHERE_COORDS()	// Cleanup memory

	delete [] COORDINATES;
}

First check spelling:
COORDINDATES
COORDINATES

Hi all, I'm having a similar problem with a code of mine. <<note: split from here>>

In my code I have a class in which I am trying to implement a dynamic array of structs. My compiler does not seem to like my deconstructor, and complains that my dynamic array was not declared in that scope. The code is pretty sparse as I'm just trying to ensure I understand proper declarations.

I've tried writing the same code with dynamic array of ints instead (for practice), and it compiled without complaint.

Any insight you all can offer would be greatly appreciated, as I'm fairly new to OO.

class HARD_SPHERE_COORDS 			
{
  	int N;
	COORD_STRUCT *COORDINDATES;
 	public:
 	HARD_SPHERE_COORDS();
	~HARD_SPHERE_COORDS();
};

The Member of COORD_STRUCT here is called COORDINDATES and not COORDINATES...

So you must replace this:

HARD_SPHERE_COORDS::~HARD_SPHERE_COORDS()
{
 	delete [] COORDINATES;
}

by this:

HARD_SPHERE_COORDS::~HARD_SPHERE_COORDS()
{
 	delete [] COORDINDATES;
}

or change the name to be different form the variable COORDINATES declared in the constructor...

And keep in mind again not to use closed names for variables so don't declare two variables like that:
int kimo;
int Kimo;

Regards,,,
Kimo :P

haha wow that's embarrassing, looks like my spelling fail was the culprit.

thanks for catching that everyone!

This article has been dead for over six months. Start a new discussion instead.