Jump to content

  • Log In with Google      Sign In   
  • Create Account

operator overloading member in namespace question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 02 April 2014 - 06:02 PM

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

Sponsor:

#2 dejaime   Crossbones+   -  Reputation: 4027

Like
0Likes
Like

Posted 02 April 2014 - 06:16 PM

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, 02 April 2014 - 06:29 PM.


#3 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 02 April 2014 - 06:55 PM

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

#4 Stainless   Members   -  Reputation: 948

Like
0Likes
Like

Posted 02 April 2014 - 07:03 PM

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



#5 dejaime   Crossbones+   -  Reputation: 4027

Like
0Likes
Like

Posted 02 April 2014 - 07:05 PM

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, 02 April 2014 - 07:15 PM.


#6 ProtectedMode   Members   -  Reputation: 1216

Like
0Likes
Like

Posted 03 April 2014 - 04:37 AM

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


Edited by ProtectedMode, 03 April 2014 - 04:37 AM.


#7 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 03 April 2014 - 11:16 AM

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, 03 April 2014 - 11:19 AM.

J-GREEN

Greenpanoply

#8 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 03 April 2014 - 11:26 AM

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, 03 April 2014 - 11:26 AM.

J-GREEN

Greenpanoply




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS