help with constructor

December 26, 2010 at 16:05:38
Specs: Windows XP
i write this code But constructor error appear
and constructor is exist please help

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include <string.h>

class Car{
private:
char Type[30];
char CarColor[20];
float Price;
public:
Car(char* t1, char* c1, float p1);
char* get_type(){return Type;};
char* get_CarColor(){return CarColor;};
float get_price(){return Price;};
} ;

Car::Car(char* t1, char* c1, float p1)
{
strcpy(Type,t1);
strcpy(CarColor,c1);
Price=p1;
}


void add_cars(Car* cr0){

char ttype[20];
char tcolor[20];
float tprice;
for (int i=0;i<=4;i++){
cout<<"Enter Type The Car ::";
cin>>ttype;
cout<<"Enter CarColor The Car ::";
cin>>tcolor;
cout<<"Enter Price The Car ::";
cin>>tprice;
cr0[i]=Car(ttype,tcolor,tprice);
}
}

float ComputeAveragePrice(Car* cr){
float sum=0;
for (int k=0;k<5;k++)
sum+=cr[k].get_price();
return sum/5;
}

void printdata(Car* cr1){
for (int l=0;l<5;l++){
cout<<"\nThe Type Is::"<<cr1[l].get_type();
cout<<"\nThe CarColor Is::"<<cr1[l].get_CarColor();
cout<<"\nThe Price Is::"<<cr1[l].get_price();
cout<<"\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
}

}

void main(){
clrscr();
Car tc[5];
//Car tc= new Car[10];
add_cars(tc);
cout<<"\n The Car Avg Price:\t"<<ComputeAveragePrice(tc)<<".\n";
printdata(tc);

getch();
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


please help me
the following error appear :
Cannot find default constructor to initialize array element of type 'Car'


See More: help with constructor

Report •


#1
December 26, 2010 at 17:37:41
1) In C++, main always returns an int, never void.
2) You have a constructor, but not a default one. At the bare minimum, go with:
Car::Car(char* t1 = 0, char* c1 = 0, float p1 = 0)

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question