Sign in to follow this  

operator overloading member in namespace question

This topic is 1384 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

Ok so I have scoured the deepest recesses of the internet to find an example of how to define the implementation of a member function that is overloading an operator for a class that is with in a namespace.

 

interface looks like this

namespace A
{

  class dog
  {
   public:
   dog();

    bool operator == ( const dog & other ) const;

   };

private:
float hieght;
float weight;
}
  


If I define the implementation with in the header it would look like this which works. but i want it in the Cpp file

namespace A
{

  class dog
  {
   public:
   dog();

    bool operator == ( const dog & other ) const
    {
     return weight == other.weight && hieght == other.hieght;
    }

   };

private:
float hieght;
float weight;
}
  


If I wanted to separate the implementation from the header to the cpp file how would I write the implementation of this overloaded function? 

 

just to show how i am writing the other member function's emplimetation in the cpp file here is an example.

bool A::dog::bark() {}

Thanks

 

Share this post


Link to post
Share on other sites
You mean this?
#include "MyFavIncludeFile.h"

bool A::dog::operator==(const dog &other) const {
    return weight == other.weight && height == other.height;
}

bool A::dog::bark() {}
Edited by dejaime

Share this post


Link to post
Share on other sites

That is in fact how I tired it using the examples i found but i get errors when using that form.

28  bool A::dog::operator==(const dog &other) const
29  {
30    return weight == other.weight && height == other.height;
31  }

"a type qualifier is not allowed on a nonmember function"           line 28
 

 "identifier "weight" is undefined"                                                  line 30

 

 "identifier "dog" is undefined"                                                      line 28

 

 "identifier "height" is undefined"                                                   line 30

 

"name fallowed by '::' must be a class or namespace name"        line 28

 

any idea why that is?

Share this post


Link to post
Share on other sites

Have you got that code inside a namespace?

namespace A
{
     bool A::dog::operator==(const dog &other) const {
          return weight == other.weight && height == other.height;
     }
}

Would then be looking for A::A::dog  and all the error messages would make sense

Share this post


Link to post
Share on other sites

It looks like it is just a typo problem.
height and hieght
 

In addition, hieght and weight are both globals in your code, they are not part of the dog class.

 

This works (I just compiled and tested):

Dog.h

#ifndef DOG_H
#define DOG_H

namespace A {

class Dog
{
    float height, weight;
    public:
        Dog(){}
        virtual ~Dog(){}

        bool operator==(const Dog& d) const;
};

}
#endif // DOG_H

Dog.cpp

#include "Dog.h"

bool A::Dog::operator==(const A::Dog& d) const {
    return ( (height==d.height) && (weight==d.weight) );
}

Main.cpp

#include <iostream>
#include "Dog.h"

int main()
{
    A::Dog d;
    std::cout << (d == d) << std::endl;
    return 0;
}

//Output:
// 1

Edit: forgot to define the dtor, working now.

Edited by dejaime

Share this post


Link to post
Share on other sites

Using "using namespace A" inside your implementation file would be a lot cleaner in my opinion...

Edited by ProtectedMode

Share this post


Link to post
Share on other sites

I wrote it just like Dejaimie's example with correct spelling and I still get those errors. And Dejaimie your right about the global scope of the two float variables but I just rewrote it wrong in my example here. they should be private members in the class. they are written correctly in my program.

 

Could there be a setting with my VS 2010 that is the problem. I mean when i write implementation out in the header file it works fine.

Edited by greenzone

Share this post


Link to post
Share on other sites

Ok thank you guys for your help. It worked finally just as I wrote it. Must have been an issue with the intellisense with in vs because i just deleted the name dog and rewrote it and it worked all of a sudden. no more errors. I am positive i did not spell dog right lol. weirdwacko.png  but thank you guys for your time 

Edited by greenzone

Share this post


Link to post
Share on other sites

This topic is 1384 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this