Thread: defining objects in a non-member operator?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    Lightbulb defining objects in a non-member operator?

    ok, i have two matrices a and b. they are objects from a class. so i write a non-member operator + to add them. the new matrix is called c. if i declare the matrix c in the operator then the body of the program doesn't recognize c, and if i declare it in the body, the operator doesn't recognize it. any ideas?
    I Love Jesus

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    It's not very clear from you post what's happening. Are you returning c from the operator+ function? Is the operator+ function a friend of your matrix class? If it's just adding two objects why isn't it a member function?

    If this doesn't help post some code.
    zen

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    i made the operator non-member because my prof said to.

    here is the operator:

    MySquareMatrix operator +(const MySquareMatrix &A, const MySquareMatrix &B)
    {
    double sum;
    int order = A.GetOrder();
    MySquareMatrix C(order); //if i declare C here, the program body (below) doesn't recognize it when i try to print it out or something

    for (int i = 0; i < order; i++)
    {
    for (int j = 0; j < order; j++)
    {
    sum = A.ReturnVal(i, j) + B.ReturnVal(i, j);
    C.PlaceValue(i, j, sum); // puts the result of a and b in c
    }
    }
    return C;



    ...

    program body...

    .
    .
    .
    A+B; // this compiles ok

    for (int i = 0; i < order; i++)
    {
    for (int j = 0; j < order; j++)
    {
    cout << C.ReturnVal(i, j); // this doesn't compile right "Undefined symbol C"
    }
    }


    i don't know where to declare C (operator or body?)

    hope this helps.

    thanks a lot
    I Love Jesus

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    A+B; // this compiles ok
    This is a function that returns a value. If you want to use the value returned you'll have to assign it to something.

    If you had two integers int a and int b, just doing

    a+b

    will not allow you to use the sum of these anywhere.
    zen

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    ok...i guess that makes sense, but i feel like i don't know what i should do next. what would i set a + b equal to?
    I Love Jesus

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    A MySquareMatrix instance -

    MySquareMatrix C = A+B;
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 01:23 PM
  3. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  4. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 10:17 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM