Thread: warning: left-hand operand of comma has no effect

  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
    28,413
    You cannot call it implicitly. Something like (*this)(q, j) and (*this)(0, 0) should work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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
    28,413
    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;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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, 04:49 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10: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