Jump to content
  • Advertisement
Sign in to follow this  
SL33pY_Kr3W

Overloading operators

This topic is 4958 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
//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;
}

Thank you :D

Share this post


Link to post
Share on other sites
Advertisement
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:


//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;
}

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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!