newbie question...error converting between types?

Status
Not open for further replies.

Crazyguy

Seniorius Lurkius
41
I'm writing a class shape which uses the member function
setName( char *objectname) to allocate free space for a new object name using "new"....i'm getting this error however and can't seem to fix it
code:<HR>
Code:
 :\shapei.cpp(25) : error C2440: '=' : cannot convert from 'char *' to 'char [99]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays

here's the corresponding code...
code:<HR>
Code:
 #ifndef shapei_h
#define shapei_h<P>#include <iostream.h><P>class Shape
{
	friend ostream & operator<<(ostream &output, const Shape &s);<P>public:
	Shape( char *object); 
	~Shape();
	void printShape() const;
	void print() const;
	void printArea() const;
	void printPerimeter() const;
	void setName( char *name);
	const char *getName() const {return name;}<P>protected:<P>static int objectcount;
double area, perimeter;<P>private:
	char name[99]; <P>};<P>#endif
and...
code:<HR>
Code:
 #include <iostream.h>
#include "shapei.h"
#include <string.h>
#include <assert.h>
int Shape::objectcount = 0;<P>Shape::Shape( char *object)
{
	area = 0;
	perimeter = 0;
	strcpy( name, object );
	objectcount++;
	cout << "The Number of objects is: " << objectcount << endl;
}<P>Shape::~Shape()
{
	objectcount--;
	cout << "The number of objects is: " << objectcount << endl;<P>}<P>void Shape::setName(char *objectname)
{
	name = new char [ strlen(objectname) + 1];
	assert( name != 0);
	strcpy( name, objectname);
}
void Shape::printShape() const
{
	cout << "The Shape is ?" << endl;   <P>
}<P>void Shape::print() const
{
	cout << "The object: " << getName() << " with";
}
void Shape::printArea() const
{
	
	cout << "\tarea = " << area << endl;
	
}<P>void Shape::printPerimeter() const
{
	
	cout << "\tperimeter = " << perimeter << endl;<P>}<P>ostream &operator<<(ostream &output, const Shape &s)
{
		s.printShape();
		s.printArea();
		s.printPerimeter();
		return output;
}
 

earl

Well-known member
3,066
You're getting the error because you already allocated space when you said char name[99]. You've already created storage, so you can use the space you allocated without newing. If you want to dynamically allocate the storage, change the class to have this behaviour:

code:<HR>
Code:
//change your class definition
char* name;<P>//change your constructor to set name = NULL
Shape::Shape( char *object):name(NULL)<P>//add a default constructor so we can rely on name being null
//note that if you don't want this to run just assert inside it
Shape::Shape():name(NULL){}<P>//and add this to the destructor
Shape::~Shape()
{
  if (name!=NULL) delete [] name;
  //your other code here...
}

-earl-
 

earl

Well-known member
3,066
OK, there are two types of storage. Static and dynamic. Static is storage that has the size set at compile time. Such as this:
code:<HR>
Code:
char bleh[99];

That allocates 98 characters (with a null terminator) of storage. While the program is running, you can't change how much is there. Need more? TDB.

Or, you can use dynamic storage created and referenced by a pointer. At compile time, you create room for a single variable that holds a memory address saying where the storage starts. At run time, you ask the OS for memory, it returns an address if memory is available, and your program uses the address to work with the memory you asked for (allocated.) Example:
code:<HR>
Code:
  char* str = NULL;     //create the pointer
  str = new char[99];   //allocate the memory
  sprintf(str, "dynamic memory size = !\n", 99);  //work with the string here
  delete [] str;      //free the memory to avoid a memory leak

The caveat, of course, with allocating memory is that unless your system is garbage collected (eg Java, C#, addons for C++) you have to "free" it yourself -- tell the OS that you are done and it can reuse the memory you requested. Before C99, this is the only way to get dynamically sized arrays in C / C++.

Now, your problem is that you are mixing the two types of memory. You say char[99], but then try and use new on it. That doesn't work. You have to pick one or the other. The code modifications I suggested above are what is needed to safely use dynamic memory. If you don't want to use dynamic memory, use static -- and remove the new statement because you don't need it. Just remember that you can't get more memory than you requested at compile time and that you can only get so much space on the stack.

For future reference,
code:<HR>
Code:
//stack memory = declared ahead of time: (ie static)
char bleh[99];
int x;
double z, t;<P>//heap memory = requested at runtime (ie dynamic)
char* b = new char[1000]; delete [] b;
int* x = (int*)malloc(sizeof(int)*1000);  free(x);<P>

-earl-
 

Crazyguy

Seniorius Lurkius
41
shape is the base class for an inheritance assignment and on the class line which inherits from point which inherits from shape i'm getting the following errors...
code:<HR>
Code:
 \Line1.cpp(8) : error C2511: 'Line::Line' : overloaded member function 'void (double,double,double,double,char *)' not found in 'Line'
        c:\line1.h(5) : see declaration of 'Line'
C:\Line1.cpp(47) : fatal error C1004: unexpected end of file found

here's the coresponding code...
code:<HR>
Code:
#ifndef LINE1_H
#define LINE1_H
#include "point1.h"<P>class Line : public Point
{
	friend ostream & operator << (ostream &out, const Line &s);
public:
	Line(double x = 0, double y = 0, double length = 1, double angle = 0, char name = '?');
	void setLength(double length);
	void setAngle(double angle);
	void setPerimeter(double length);
	double getLength() const;
	double getAngle() const;
	double getPerimeter() const;
	void printShape() const;
	void print() const;
protected:
	double length, angle;
	double area, perimeter;
};<P>#endif<P>
and...
code:<HR>
Code:
 // line_i.cpp<P>#include <iostream.h>
#include <math.h>
#include "line1.h"<P>Line::Line(double x, double y, double length, double angle, char *name)
	  : Point(x, y, *name)
{
	setLength( length);
	setAngle( angle);
}<P>void Line::print() const
{
   Point::print();
   cout << "\t Length = " << length
   	   << "\t Angle(degrees) = " << angle << endl;
}<P>void Line::setLength(double len) { length = len; }<P>void Line::setAngle(double ang)  { angle = ang; }<P>void Line::setPerimeter(double len) { perimeter = len; }<P>double Line::getLength() const   { return length; }<P>double Line::getAngle() const    { return angle; }<P>double Line::getPerimeter() const { return perimeter; }<P>void Line::printShape() const
{
   cout << "\n The shape is a Line. ";
}<P>ostream & operator << (ostream &output, const Line &s)
{
	s.printShape();
	s.print();
	s.printArea();
	s.printPerimeter();
	return output;
}<P>

Late Edit: thanks for explaining :)
 

Crazyguy

Seniorius Lurkius
41
back to the topic of "new"
i think i attempted to do what you said but am now getting these errors
code:<HR>
Code:
 c:\shapei.h(18) : error C2065: 'name' : undeclared identifier
c:\shapei.cpp(19) : error C2541: delete : cannot delete objects that are not pointers
c:\shapei.cpp(27) : error C2440: '=' : cannot convert from 'char *' to 'int'

here's what i did...
code:<HR>
Code:
 #ifndef shapei_h
#define shapei_h<P>#include <iostream.h><P>class Shape
{
	friend ostream & operator<<(ostream &output, const Shape &s);<P>public:
	Shape( char *object); 
	~Shape();
	void printShape() const;
	void print() const;
	void printArea() const;
	void printPerimeter() const;
	void setName( char *name);
	const char *getName() const {return name;}<P>protected:<P>static int objectcount;
double area, perimeter;<P>private:
 <P>};<P>#endif

code:<HR>
Code:
 #include <iostream.h>
#include "shapei.h"
#include <string.h>
#include <assert.h>
int Shape::objectcount = 0;<P>Shape::Shape( char *object)
{
	area = 0;
	perimeter = 0;
	char *name = NULL;
	setName( object );
	objectcount++;
	cout << "The Number of objects is: " << objectcount << endl;
}<P>Shape::~Shape()
{
	delete [] name;
	objectcount--;
	cout << "The number of objects is: " << objectcount << endl;<P>}<P>void Shape::setName(char *objectname)
{
	name = new char [ strlen(objectname) + 1];
	assert( name != 0);
	strcpy( name, objectname);
}
void Shape::printShape() const
{
	cout << "The Shape is ?" << endl;   <P>
}<P>void Shape::print() const
{
	cout << "The object: " << getName() << " with";
}
void Shape::printArea() const
{
	
	cout << "\tarea = " << area << endl;
	
}<P>void Shape::printPerimeter() const
{
	
	cout << "\tperimeter = " << perimeter << endl;<P>}<P>ostream &operator<<(ostream &output, const Shape &s)
{
		s.printShape();
		s.printArea();
		s.printPerimeter();
		return output;
}
 
Status
Not open for further replies.