operator overloading member in namespace question

Started by
6 comments, last by pressgreen 10 years, 1 month ago

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

J-GREEN

Greenpanoply
Advertisement
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() {}

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?

J-GREEN

Greenpanoply

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

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.

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

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.

J-GREEN

Greenpanoply

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

J-GREEN

Greenpanoply

This topic is closed to new replies.

Advertisement