PDA

View Full Version : رفع error های برنامه



fshb_ 1370
سه شنبه 05 مهر 1390, 09:48 صبح
سلام
error های این برنامه رو چطور برطرف کنم؟

string2.h
#include<iostream>
using namespace std;
#ifndef STRING2_H_
#define STRING2_H_
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM=80;
public:
String();
String(const char *s);
String(const String &);
~String();
int length()const{return len;}
String & operator=(const String &s);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;
String& stringlow(String &s);
String& stringup(String &s);
int count(char c);
friend bool operator>(const String &s1,const String & s2);
friend bool operator<(const String &s1,const String & s2);
friend bool operator==(const String &s1,const String &s2);
friend String& operator+(const string &s1,const String &s2);
friend ostream & operator<<(ostream &os,const String &s);
friend istream & operator>>(istream &is,String &s);
static int HowMany();
};
#endif


string2.cpp
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
#include "string2.h"
int String::num_strings=0;
int String::HowMany()
{
return num_strings;
}
String::String()
{
len=4;
str=new char[1];
str[0]='\ 0';
num_strings++;
}
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String(const String &s)
{
num_strings++;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
}
String::~String()
{
--num_strings;
delete [] str;
}
String& String::operator =(const char *s)
{
delete [] str;
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}
String& String::operator =(const String &s)
{
if (this==&s)
return *this;
delete [] str;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
return *this;
}
char& String::operator [](int i)
{
return str[i];
}
const char& String::operator [](int i) const
{
return str[i];
}
String& String::stringlow(String &s)
{
for (int i=0;i<s.len;i++)
tolower(s.str[i]);
return s;
}
String& String::stringup(String &s)
{
for (int i=0;i<s.len;i++)
toupper(s.str[i]);
return s;
}
//
int count(char c)
{
int counter=0;
for (int i=0;i<len;i++)
if(str[i]==c)
counter++;
return counter;
}
bool operator<(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)<0);
}
bool operator>(const String &s1,const String &s2)
{
return s2.str<s1.str;
}
bool operator==(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)==0);
}
//
String& operator+(const String &s1,const String &s2)
{
String s;
s.len=s1.len+s2.len;
s.str=new char[s.len+1];
strcpy(s.str,s1.str);
int count=0;
for(int i=s1.len;i<s.len;i++)
{
s.str[i+1]=s2.str[count];
count++;
}
s.str[s.len]='\0';
return s;
}
ostream & operator<<(ostream &os,const String &s)
{
os<<s.str;
return os;
}
istream & operator>>(istream &is,String &s)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
s = temp;
while (is && is.get() != '\ n')
continue;
return is;
}

BeginnerProgrammer
سه شنبه 05 مهر 1390, 13:36 عصر
یکی از اشکالای برنامه ت اینه که len و str جزو privateهای کلاسته و توابع دیگه (count) بهشون دسترسی ندارن.

توی تابع String& operator+(const String &s1,const String &s2) به جای استفاده مستقیم از len از تابع length استفاده کن.التبه در خط زیر :


s.len=s1.len+s2.len;

باید یه تابعم برای set کردن طول رشته بنویسی :


s.SetLen(s1.lenght()+s2.lenght());

fshb_ 1370
سه شنبه 05 مهر 1390, 18:02 عصر
یکی از اشکالای برنامه ت اینه که len و str جزو privateهای کلاسته و توابع دیگه (count) بهشون دسترسی ندارن.

توی تابع String& operator+(const String &s1,const String &s2) به جای استفاده مستقیم از len از تابع length استفاده کن.التبه در خط زیر :


s.len=s1.len+s2.len;

باید یه تابعم برای set کردن طول رشته بنویسی :


s.SetLen(s1.lenght()+s2.lenght());

پس چطور در operator= من از len و str استفاده میکنم و error ی نمیگیره؟ منظور من از len و str در تابع count همان len و str در object ی است که به وسیله آن constructor را صدا می زنیم.

BeginnerProgrammer
سه شنبه 05 مهر 1390, 19:46 عصر
چون operator جزو توابع کلاسه

fshb_ 1370
سه شنبه 05 مهر 1390, 23:36 عصر
چون operator جزو توابع کلاسه

من متوجه نمیشم، count() هم جزو توابع کلاس هستش، میشه ی خورده بیشتر توضیح بدید؟
ممنون

BeginnerProgrammer
سه شنبه 05 مهر 1390, 23:56 عصر
باید بنویسید ???
String::count

BeginnerProgrammer
چهارشنبه 06 مهر 1390, 00:40 صبح
اینم کدهای اصلاح شده :
string2.h


#include<iostream>
using namespace std;
#ifndef STRING2_H_
#define STRING2_H_
class String
{

friend ostream & operator<<(ostream &os,const String &s);
friend istream & operator>>(istream &is,String &s);
private:
char *str;
int len;
static int num_strings;
static const int CINLIM=80;
public:
String();
String(const char *s);
String(const String &);
~String();
int length(){return len;}
void setLen(int l){len=l;};
String & operator=(const String &s);
String & operator=(const char *);
char & operator[](int i);
String& operator+(const String &s1);
const char & operator[](int i)const;
bool operator>(const String &s1);
bool operator<(const String &s1);
bool operator==(const String &s1);
String& stringlow(String &s);
String& stringup(String &s);
int count(char c);
static int HowMany();
};
#endif

string2.cpp


#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
#include "string2.h"
int String::num_strings=0;
int String::HowMany()
{
return num_strings;
}
String::String()
{
len=4;
str=new char[1];
str[0]='\ 0';
num_strings++;
}
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String(const String &s)
{
num_strings++;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
}
String::~String()
{
--num_strings;
delete [] str;
}
String& String::operator =(const char *s)
{
delete [] str;
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}
String& String::operator =(const String &s)
{
if (this==&s)
return *this;
delete [] str;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
return *this;
}
char& String::operator [](int i)
{
return str[i];
}
const char& String::operator [](int i) const
{
return str[i];
}
String& String::stringlow(String &s)
{
for (int i=0;i<s.len;i++)
tolower(s.str[i]);
return s;
}
String& String::stringup(String &s)
{
for (int i=0;i<s.len;i++)
toupper(s.str[i]);
return s;
}
//
int String::count(char c)
{
int counter=0;
for (int i=0;i<len;i++)
if(str[i]==c)
counter++;
return counter;
}
bool String::operator<(const String &s1)
{
return (strcmp(str,s1.str)<0);
}
bool String::operator>(const String &s1)
{
return s1.str<str;
}
bool String::operator==(const String &s1)
{
return (strcmp(str,s1.str)==0);
}
//
String& String::operator+(const String &s1)
{
String s;
s.len=len +s1.len;
s.str=new char[s.length()+1];
strcpy(s.str,s1.str);
int count=0;
for(int i=s1.len;i<s.len;i++)
{
s.str[i+1]=s1.str[count];
count++;
}
s.str[s.len]='\0';
return s;
}
ostream & operator<<(ostream &os,const String &s)
{
os<<s.str;
return os;
}
istream & operator>>(istream &is,String &s)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
s = temp;
while (is && is.get() != '\ n')
continue;
return is;
}
int main()
{
return 0;
}


معمولا وقتیoperatorی که میخوای آورلود کنی ورودیهای یکسانی از نوع همون کلاس میگیره دیگه friend تعریفش نمیکنن و به عنوان تابع همون کلاس تعریف میشه!