PDA

View Full Version : error در constructor با پارامتر از نوع pointer to char



fshb_ 1370
سه شنبه 05 مهر 1390, 09:33 صبح
سلام به همه دوستان
constructor این برنامه به عنوان پارامتر pointer از نوع char میگیره. من در main وقتی object ایجاد میکنم و یک آرایه از نوع char و یا یک string بهش میدم سبب error میشه.
من چطور باید ی پارامتر به تابع بدم؟
پیشاپیش از راهنماییتون ممنون


pr1_chap12.h
#ifndef PR1_CHAP12
#define PR1_CHAP12
class Cow
{
private:
char name[20];
char *hobby;
double weight;
public:
Cow();
Cow(const char *nm,const char *ho,double wt);
Cow(const Cow &c);
~Cow();
Cow & operator=(const Cow &c);
void ShowCow()const;
};
#endif;


pr1_chap12.cpp
#include<iostream>
#include<cstring>
#include "pr1_chap12.h"
using namespace std;
Cow::Cow()
{
name[0]='\0';
hobby=NULL;
weight=0;
}
Cow::Cow(const char *nm, const char *ho, double wt)
{
strcpy(name,nm);
hobby=new char[strlen(ho)+1];
strcpy(hobby,ho);
weight=wt;
}
Cow::Cow(const Cow &c)
{
hobby=new char[strlen(c.hobby)+1];
strcpy(name,c.name);
strcpy(hobby,c.hobby);
weight=c.weight;
}
Cow::~Cow()
{
delete [] hobby;
}
Cow & Cow::operator =(const Cow &c)
{
if (this==&c)
return *this;
delete [] hobby;
strcpy(name,c.name);
hobby=new char[strlen(c.hobby)+1];
strcpy(hobby,c.hobby);
weight=c.weight;
return *this;
}
void Cow::ShowCow() const
{
cout<<"name: "<<name<<endl;
cout<<"hobby: "<<hobby<<endl;
cout<<"weight: "<<weight<<endl;
}

main.cpp
#include<iostream>
#include "pr1_chap12.h"
using namespace std;
int main()
{
char n1[40]="test1";
char n2[40]="test2";
char n3[40]="test3";
Cow c1=new Cow(&n1,&n2,61);
Cow c2=new Cow("test2","test",62);
Cow c3=new Cow("test3","test",87);
Cow c22=c2;
Cow c33;
c33=c3;
}

fshb_ 1370
یک شنبه 10 مهر 1390, 11:27 صبح
یعنی هیچ کی هیچ نظری نداره؟

_hamid
یک شنبه 10 مهر 1390, 22:38 عصر
لطف کن خطایی که می گی رو هم بذار.