//Constructors
#include<iostream>
#include<string>
class Critter
{
std::string* m_pname; //Private data member
public:
Critter(std::string name = "")
{
std::cout<<"\nConstructor called\n";
m_pname = new std::string(name);
return;
}
std::ostream& operator<<(std::ostream& os, Critter& c) //overloaded operator
{
os << (*c).m_pname;
return os;
}
Critter& operator=(Critter& crit) //overloaded operator
{
if(this == &crit)
{
return *this;
}
else
{
(*this).m_pname = crit.m_pname;
return *this;
} // end operator overload
~Critter()
{
std::cout<<"Destructor called\n";
delete m_pname;
}
Critter(const Critter& acrit)
{
std::cout<<"\nCopy Constructor called\n";
m_pname = new std::string;
*m_pname = (*acrit).m_pname; //extra GetName() is copy constructo
return;
}
std::string GetName() const
{
std::cout<<"\nGetName() called\n";
return *m_pname;
}
};
void testDestructor()
{
Critter mr2("WHEEEE");
std::cout<<mr2.GetName()<<std::endl;
return;
}
void testCopyConstructor(Critter amoron)
{
std::cout<<amoron.GetName()<<std::endl;
return;
}
int main()
{
Critter Brian("Meep");
std::cout<<Brian.GetName()<<std::endl;
testDestructor();
testCopyConstructor(Brian);
std::cout<<Brian.GetName()<<std::endl;
std::cout<<"\n\n";
system("pause");
return 0;
}
Overloading operators
Im getting an error when I try to run my program. There is something wrong with my overloaded operators and I am stumped.
Here are t3h errors:
` std::ostream& Critter::operator<<(std::ostream&, Critter&)' must take
syntax error at end of input
And here is t3h code:
Thank you :D
You can't overload operator<< with a left hand side of std::ostream & inside your class. You need to move the definition of the overload outside your class, and seeing as you're using a private member variable inside, declare it as a friend.
You're also missing a closing } in one of your functions which makes decoding the error message more complex since the parser gets upset when it can't parse the rest of your source code. Plus you also have some misplaced parenthesis when de-referencing some of your pointers.
A rough cut at fixing these problems would look like:
You're also missing a closing } in one of your functions which makes decoding the error message more complex since the parser gets upset when it can't parse the rest of your source code. Plus you also have some misplaced parenthesis when de-referencing some of your pointers.
A rough cut at fixing these problems would look like:
//Constructors#include<iostream>#include<string>class Critter{ std::string* m_pname; //Private data memberpublic: Critter(std::string name = "") { std::cout<<"\nConstructor called\n"; m_pname = new std::string(name); return; } friend std::ostream& operator<<(std::ostream& os, Critter& c); Critter& operator=(Critter& crit) //overloaded operator { if(this == &crit) { return *this; } else { *m_pname = *crit.m_pname; return *this; } } // end operator overload ~Critter() { std::cout<<"Destructor called\n"; delete m_pname; } Critter(const Critter& acrit) { std::cout<<"\nCopy Constructor called\n"; m_pname = new std::string(*acrit.m_pname); return; } std::string GetName() const { std::cout<<"\nGetName() called\n"; return *m_pname; } };std::ostream& operator<<(std::ostream& os, Critter& c) //overloaded operator{ os << *(c.m_pname); return os;} void testDestructor(){ Critter mr2("WHEEEE"); std::cout<<mr2.GetName()<<std::endl; return;}void testCopyConstructor(Critter amoron){ std::cout<<amoron.GetName()<<std::endl; return;} int main(){ Critter Brian("Meep"); std::cout<<Brian.GetName()<<std::endl; testDestructor(); testCopyConstructor(Brian); std::cout<<Brian.GetName()<<std::endl; std::cout<<"\n\n"; system("pause"); return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement