Operator on power overload

Hi ,I am trying to overload ^ operator,is my code right and how do you call this operator from the main
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
objA& operator^(objA &o,int  power)
  {
    o.varI=pow(o.varI,power);
    o.varF=pow(o.varF,power);
    return o;
  }

int main()
{
   objA o1(3,4.44);
   o1^(o1,7);
}
Have you tried o1^7;
Thanks
I did ,:))
Doesn't work
What is it that doesn't work? What error message do you get?
^ is a bitwise xor operator.
CodeMonkey, you are right, but ^ can be overloaded.

Leo, how do you know there is something wrong with your code?
Did it not compile? Or it didn't give a correct output?

I noticed you didn't give any cout or printf statements in your code. You didn't even store the result in a variable. Is this your real code?

Also, your main() doesn't return any value.

If you are using cout to display your answer, use
cout<<(ol^7); ---1
instead of
cout<<ol^7; ---2
as << has more priority than ^ and your compiler will read the 2nd statement as
(cout<<ol)^7

There are many people who do this mistake.
Last edited on
Thanks for the replays
I am geting the error
.cpp|14|error: 'objA& objA::operator^(objA&, int)' must take exactly one argument|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <iostream>
using namespace std;
class objA
{
  public:
  objA()
  :varI(0),varF(0)
  {
  }
  objA(int i,float f)
  :varI(i),varF(f)
  {
  }
   objA& operator^(objA &o,int  power)
  {
    o.varI=pow(o.varI,power);
    o.varF=pow(o.varF,power);
    return o;
  }
  int getVarI(){return varI;}
  float getVarF(){return varF;}
  void setVarI(int i){varI=i;}
  void setVarF(float f){ varF=f;}
  private:
 int varI;
 float varF;
};
ostream& operator<<(ostream&out,objA &o)
{
    out<<o.getVarI()<<"---"<<o.getVarF()<<endl;
    return out;
}

int main()
{
   objA o1(3,4.44);
   cout<<(o1^7);
}



CodeMonkey, you are right, but ^ can be overloaded.

I know that the operator can be overloaded, I would say that the semantics of the operator should not be changed ie you should not be trying to make the xor operator into a power operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

struct Foo
{
    double bar;
};

Foo operator^(Foo& value, double power)
{
    value.bar = pow(value.bar,power);
    return value;
}

int main()
{
    int bar = 4;
    Foo foo1;
    foo1.bar = 4.0;

    Foo foo2 = foo1 ^ 2;
    int anInt = bar ^ 2;

    cout << foo2.bar << " " << anInt << endl;
}
Last edited on
Thanks ,but my code still doesnt work ,any idea whay(the same error)
I add cmath for pow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cmath>

using namespace std;

class objA
{
public:
    objA():varI(0),varF(0)
    {
    }
    objA(int i,float f) :varI(i),varF(f)
    {
    }

    objA operator^(int  power)
    {
        int   a = static_cast<int>  ( pow(static_cast<double>(varI),power));
        float b = static_cast<float>( pow(static_cast<double>(varF),power));
        return objA(a,b);
    }

    int getVarI(){return varI;}
    float getVarF(){return varF;}
    void setVarI(int i){varI=i;}
    void setVarF(float f){ varF=f;}
private:
    int varI;
    float varF;
};

ostream& operator<<(ostream&out,objA &o)
{
    out<<o.getVarI()<<"---"<<o.getVarF()<<endl;
    return out;
}

int main()
{
   objA o1(3,4.44);
   cout << (o1^7);
}

Thanks,but your code gives me this error
.cpp||In function 'int main()':|
.cpp|41|error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
c:\compiler\bin\..\lib\gcc\i686-pc-mingw32\4.6.2\..\..\..\..\include\c++\4.6.2\ostream|581|error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = objA]'|
||=== Build finished: 2 errors, 0 warnings ===|
closed account (zb0S216C)
It's because o1^7 returns a objA object which std::cout doesn't know how to handle.
I didn't see the overloaded operator << .

Wazzak
Last edited on
CodeMonkey forgot to put const on line 23 int getVarI() const {return varI;}, line 24 float getVarF() const {return varF;} and line 32 ostream& operator<<(ostream&out,const objA &o).
Last edited on
Thanks ,now everithing works
Could you explain why ,in this case all this functions has to me constant
If I use operator overload<< in diferent case to print object works fine without const
here's an example of powering any type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cmath>
 
using namespace std;

template<typename T>
class mytype
{
      public:
      T x;
      mytype()
      {
             x=T(0);
             }
      mytype(T a)
      {
                x=a;
                }
      T operator^(int b);
      };

template<typename T>
T mytype<T>::operator^(int b)
{
    return T(pow((double)x, b));
    }

It's quite cool to use it on chars!
Topic archived. No new replies allowed.