warning: left-hand operand of comma has no effect

This is a discussion on warning: left-hand operand of comma has no effect within the C++ Programming forums, part of the General Programming Boards category; Hi. I've got the above warning message roughly 130 times for a program I'm using. Analyzing this more carefully I've ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    warning: left-hand operand of comma has no effect

    Hi. I've got the above warning message roughly 130 times for a program I'm using. Analyzing this more carefully I've found that I'm only getting it 10 times, but multiple occurrences of the same locations. Presumably from multiple method calls.
    I have no idea what causes these warnings, except that it's to do with my overloaded brackets operator.
    Can anyone help me?

    Code:
    // Overloaded brackets operator
    mType & Matrix::operator()(const int i, const int j) {
            return values[j * xsize + i];
     }
    
    // Lines causing warnings
    res(i, j) += (q,j) * m(i, q);
    res(i, j) = (i, j) * f;
    res(i,j) = (i, j) + m(i,j);
    (i,j) = x;
    res(i,j) = (j,i);
    return Vec3D<mType>((0,0),(0,1),(0,2));	            // 3 x Warnings
    return Vec2D<mType>((0,0),(0,1));                     // 2 x Warnings
    It's very obvious that the warning only appears when the () is used with the implied "this" operator.
    NB. The lines generating warnings that I have shown are not in this context. They are within seperate methods, but since the overall file is 160 lines long I chose to display them this way for now. More code is available if needed.

    Thanks,
    Simon

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,628
    You cannot call it implicitly. Something like (*this)(q, j) and (*this)(0, 0) should work.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    You cannot imply it. You want to do something like:

    (*this)(q,j);

    I don't know if that would work because I'm not much familiar with overloading brackets but that seems more right to me.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    Thanks. I haven't tried that yet. Why is this a problem? I mean, (curse me for saying this) but it's only a warning? It's not a full blown error, and my program works exactly as I want it to do.
    What is wrong with doing it this way that deserves a warning?
    I'm reasonably new with C++ but I'm finding my way, and this will eventually be part of my dissertation so a reason or some background would be extremely helpful to me.
    Thanks for the rapid response.
    Simon

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Basically, the parentheses in C++ are really put to work. They stand for grouping, function calls, casting, functional macro replacement - have I forgotten something?

    Anyway, what they mean exactly depends on where in the syntax they are. Only when they immediately follow a callable entity do they actually mean function call, and only function call can be overloaded with operator().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,628
    Well, the problem is that something like:
    Code:
    res(i,j) = (j,i);
    is equivalent to:
    Code:
    res(i,j) = j,i;
    which is equivalent to:
    Code:
    res(i,j) = i;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    1
    Well, the problem is that something like:
    Code:
    Code:
    res(i,j) = (j,i);
    is equivalent to:
    Code:
    Code:
    res(i,j) = j,i;
    Those lines are not exactly equivalent. Consider this example:

    Code:
    int a, b, i = 1, j = 2;
    a = (i,j);
    b = i,j;
    cout << a << " " << b << endl << endl;
    Prints out "2 1".

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Whoa, way to resurrect a months-old thread.

    Yeah, since ,'s priority is even lower than ='s, the two lines are indeed not equivalent, but I really don't think it matters anymore.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Release Configuration Problems
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2002, 04:54 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21