S
Sean
Can someone help me see why the following "operator=" overloading
doesn't work under g++? and the error message is copied here. I see no
reason the compiler complain this. Thanks,
[bash]$ g++ copyconstructor1.cpp
#copyconstructor1.cpp: In function `int main()':
#copyconstructor1.cpp:86: no match for `sample& = sample' operator
#copyconstructor1.cpp:53: candidates are: sample sample:perator=(sample&)
=========================================
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class sample {
char *s;
public:
sample(); // normal constructor
sample(const sample &ob); // copy constructor
~sample( ) { if(s) delete [] s; cout << "Freeing s\n"; }
void show() { cout << s << "\n"; }
void set(char *str);
sample operator=(sample &ob); // overload assignment
};
// Normal constructor.
sample::sample()
{
s = new char('\0'); // s points to a null string.
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
}
// Copy constructor.
sample::sample(const sample &ob)
{
s = new char[strlen(ob.s)+1];
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
strcpy(s, ob.s);
}
// Load a string.
void sample::set(char *str)
{
s = new char[strlen(str)+1];
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
strcpy(s, str);
}
sample sample:perator=(sample &ob)
{
/* If the target string is not large enough
then allocate a new string. */
if(strlen(ob.s) > strlen(s)) {
delete [] s;
s = new char[strlen(ob.s)+1];
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
}
strcpy(s, ob.s);
return *this;
}
// Return an object of type sample.
sample input()
{
char instr[80];
sample str;
cout << "Enter a string: ";
cin >> instr;
str.set(instr);
return str;
}
int main()
{
sample ob;
ob = input(); // This doesn't compile under g++
ob.show();
return 0;
}
doesn't work under g++? and the error message is copied here. I see no
reason the compiler complain this. Thanks,
[bash]$ g++ copyconstructor1.cpp
#copyconstructor1.cpp: In function `int main()':
#copyconstructor1.cpp:86: no match for `sample& = sample' operator
#copyconstructor1.cpp:53: candidates are: sample sample:perator=(sample&)
=========================================
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class sample {
char *s;
public:
sample(); // normal constructor
sample(const sample &ob); // copy constructor
~sample( ) { if(s) delete [] s; cout << "Freeing s\n"; }
void show() { cout << s << "\n"; }
void set(char *str);
sample operator=(sample &ob); // overload assignment
};
// Normal constructor.
sample::sample()
{
s = new char('\0'); // s points to a null string.
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
}
// Copy constructor.
sample::sample(const sample &ob)
{
s = new char[strlen(ob.s)+1];
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
strcpy(s, ob.s);
}
// Load a string.
void sample::set(char *str)
{
s = new char[strlen(str)+1];
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
strcpy(s, str);
}
sample sample:perator=(sample &ob)
{
/* If the target string is not large enough
then allocate a new string. */
if(strlen(ob.s) > strlen(s)) {
delete [] s;
s = new char[strlen(ob.s)+1];
if(!s) {
cout << "Allocation error\n";
exit(1); // exit program if out of memory
}
}
strcpy(s, ob.s);
return *this;
}
// Return an object of type sample.
sample input()
{
char instr[80];
sample str;
cout << "Enter a string: ";
cin >> instr;
str.set(instr);
return str;
}
int main()
{
sample ob;
ob = input(); // This doesn't compile under g++
ob.show();
return 0;
}