Share via

C++ Vectors

Sid Kraft 46 Reputation points
2026-05-26T20:32:32.23+00:00
In C++ why am I getting an error on this statement?       CHMID1[I] = PT1[I] + (CHORD1 * VEC1[I]) / 2.;

CHMID1[I] a float vector
PT1[I] a float vector
CHORD1 a float variable
VEC1[I] a float vector

system says = not a valid operator???
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


3 answers

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 18,640 Reputation points Microsoft External Staff Moderator
    2026-05-27T02:50:47.31+00:00

    Hi @Sid Kraft ,

    Thanks for reaching out.

    That statement is valid in C++ as long as the left-hand side resolves to a writable scalar value and the other expressions evaluate to numeric values such as float. Because of that, the = operator itself is usually not the real issue.

    An error like this means one of the operands is not the type the compiler expects, or the left-hand side is not something that can be assigned to.

    If CHMID1, PT1, and VEC1 are std::vector<float> objects, then this pattern should work as expected:

    std::vector<float> CHMID1(n);
    std::vector<float> PT1(n);
    std::vector<float> VEC1(n);
    float CHORD1 = 10.0f;
    
    for (std::size_t I = 0; I < n; ++I)
    {
        CHMID1[I] = PT1[I] + (CHORD1 * VEC1[I]) / 2.0f;
    }
    

    The next things I would check are whether CHMID1 is writable and not declared as const, whether I is an integer type used for indexing, and whether CHMID1[I], PT1[I], and VEC1[I] actually resolve to individual numeric values rather than pointers, structs, or nested containers.

    It is also worth checking that the vectors have been sized before using operator[], and that I is within range. With std::vector, indexing is only valid when that element already exists.

    If any of those expressions are not scalar values, the compiler can produce a misleading error that makes it look like the = operator is the problem when the real issue is the operand type.

    If those variables are actually custom vector or math types rather than containers of float, C++ will not perform vector arithmetic automatically unless the necessary operators have been defined for that type. In that case, the calculation may need to be done component by component.

    If you can share the declarations of CHMID1, PT1, VEC1, CHORD1, and I, it should be possible to identify the exact cause pretty quickly.

    Hope this helps! If my explanation and the information I provided were helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

    Was this answer helpful?


  2. Bruce (SqlWork.com) 84,071 Reputation points
    2026-05-27T02:38:56.1166667+00:00

    You must of declared the variables wrong:

    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector<float> CHMID1 = {1.0, 2.0, 3.0}; 
        std::vector<float> PT1 = {1.0, 2.0, 3.0}; 
        std::vector<float> VEC1 = {1.0, 2.0, 3.0}; 
        float CHORD1 = 1.0;
        int I = 0;
        
        CHMID1[I] = PT1[I] + CHORD1 * VEC1[I] / 2.0;   
        std::cout << CHMID1[I];
    
        return 0;
    }
    

    Note: due to precedence the (‘s are not required.

    Was this answer helpful?

    0 comments No comments

  3. AI answer

    2026-05-26T20:32:49.84+00:00

    The error is caused by the types of the operands in the expression, not by the = itself.

    In C++, = is a valid assignment operator, but the compiler or debugger reports an error when the types on either side of the operator (or in the sub‑expression) are incompatible with the operators being used.

    From the description:

    • CHMID1[I]float
    • PT1[I]float
    • CHORD1float
    • VEC1[I]float

    The expression

    CHMID1[I] = PT1[I] + (CHORD1 * VEC1[I]) / 2.;
    

    is valid only if all of those are actually scalar float values and I is an integral index.

    Typical reasons for an error like “operator not valid” or similar here:

    1. One of the operands is not a scalar float but a pointer or some other type.
      • For example, if CHMID1, PT1, or VEC1 are declared as pointers (e.g., float* CHMID1;) and I is not an integer, or if the indexing is wrong, the compiler may see an invalid left or right operand for = or for +, *, or /.
      • Errors such as:
        • C2114: pointer on left; needs integral value on right, when pointer arithmetic is used incorrectly.
        • C2296: bad left operand, when the left operand of an operator is not of a valid type.
        • C2297: bad right operand, when the right operand is invalid.
    2. In the debugger’s expression evaluator, if the types are incompatible with the operator, an error like CXX0034: types incompatible with operator can appear. For example, trying to multiply or divide a pointer instead of a numeric type.

    To fix the issue:

    1. Verify declarations:
      • CHMID1, PT1, VEC1 should be arrays or containers of float, e.g.
             float CHMID1[N];
             float PT1[N];
             float VEC1[N];
             float CHORD1;
             int I;
        
    2. Ensure I is an integer index, not a pointer or floating type.
    3. If any of these are pointers, ensure they are indexed with an integral type and that no pointer is being multiplied or divided directly.
    4. If this is being typed into the debugger’s Watch/Immediate window, ensure the expression uses only valid runtime types (no incomplete or mismatched types) and that no pointer arithmetic like pointer * value is being attempted.

    If any operand is a pointer, cast to an appropriate numeric type only when that is logically correct, and avoid multiplying or dividing pointers directly, as that leads to the “types incompatible with operator” diagnostics.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.