String proplem!!!!Please help
Write the following member function:
// strcmp is negative if s < s1
// is 0 if s == s1
// and positive if s > s1
// where s is the emplicite argument
int my_string::strcmp(const my_string& s1);
// strrev reverses the my_string
void my_string::strrev();
//print oeverloaded to print the frist n characters
void my_string:: print(int n);
here is what i got so far:
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class my_string
{
public:
my_string();
my_string(const my_string& str);
my_string(const char* p);
~my_string();
int strcmp(const my_string& s1);
void strrev();
void print(int n);
int const getLen();
const char* getS();
private:
char* s;
int len;
};
my_string::my_string(): len(0),s (0){}
my_string::my_string(const char* p)
{
len = strlen (p);
s = new char[len +1];
assert(s !=0);
strcpy(s, p);
}
my_string::my_string(const my_string& str):len(str.len)
{
s = new char[len+1];
assert(s !=0);
strcpy(s, str.s);
}
// Destructor
my_string::~my_string()
{
delete [] s;
}
int my_string::strcmp(const my_string& s1)
{
int i;
for(i=0; s1.s[i] && s[i] && s1.s[i] == s[i]; ++i)
{
return s1.s[i]-s[i];
}
}
void my_string::strrev()
{
my_string strrev;
char* temp = new char[len + 1];
int i= 0;
int j= 0;
while (s[i])
{
temp[j++] = s[i--];
}
temp[j] ='\0';
strcpy(s,temp);
delete[] temp;
}
void my_string:: print(int n)
{
return ;
}
int const my_string::getLen()
{
return len;
}
const char* my_string::getS()
{
return s;
}
int main ()
{
my_string myStr ("same");
if (myStr.strcmp ("same") < 0)
cout << myStr.getS() << " < " << "same" << endl;
else if (myStr.strcmp ("same") == 0)
cout << myStr.getS() << " = " << "same" << endl;
else
cout << myStr.getS() << " > " << "same" << endl;
cout << "Reverse string " << myStr.getS() << " to ";
myStr.strrev();
myStr.print();
system("pause");
}
when i compile it ,the compiler telling me the error: " No matching function for call to my_string:: print()" and "Candidates are : void my_string:: print()".Please tell me where did i go wrong,thanks alot
Note:
Code:
Use the following string to test the function strcmp:
1st string 2nd string
gladiator gladiolus
xyz abcd
same same
Re: String proplem!!!!Please help
Quote:
Originally posted by tinhnho
when i compile it ,the compiler telling me the error: " No matching function for call to my_string:: print()" and "Candidates are : void my_string:: print()".Please tell me where did i go wrong,thanks alot
First, when posting, note that you have a "Disable Smilies in this post" option below the edit window.
Second, the reason for the error is obvious: your print() member function takes an integer argument, but you called it without an argument.
Third, your class has no assignment operator. If I did this, your code would not be correct:
Code:
int main()
{
my_string s = "ABC";
my_string t;
t = s;
}
You will now have two objects pointing to the same dynamically allocated char *. On destruction, you will delete the same memory twice.
Regards,
Paul McKenzie