Share via

C++ Vectors

Sid Kraft 46 Reputation points
2026-05-26T21:02:14.4333333+00:00
This is maddening, went to great lengths to make sure that the statements on either side of the = operator of the same type, still get a compiler error  

I = 0;
 do
 {
     float CH1 = PT1[I];
     float CH2 = PT2[I];
     float CH3 = PT3[I];
     float VC1 = VEC1[I];
     float VC2 = VEC2[I];
     float VC3 = VEC3[I];
     float CHTMP1 = CH1 + (CHORD1 * VC1) / 2.;
     float CHTMP2 = CH2 + (CHORD2 * VC2) / 2.;
     float CHTMP3 = CH3 + (CHORD3 * VC3) / 2.;
     CHMID1[0] = CHTMP1;                                                ERROR
     CHMID2[0] = CHTMP2;												ERROR
     CHMID3[0] = CHTMP3;												ERROR
     ++I;
  } while(I <= 2);
CH1=PT1[I];				WORKS
CHMID1[I]=CHTMP1;		DOESN'T WORK?????

CH1 float
CHTMP1 FLOAT
PT1[I] vector
VEC1[I] vector

Did assign elements of PT1[I] to float CH1
Cannot assign float CHTMP1 	to elements of CHMID1[I] etc.?????
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.


2 answers

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 4,605 Reputation points Microsoft External Staff Moderator
    2026-05-27T06:16:09.14+00:00

    Hello @Sid Kraft ,

    Thanks for your question.

    Since CHMID1, CHMID2, CHMID3 were declared with no size, no slots exist to store data. Furthermore, CHTMP1, CHTMP2, CHTMP3 were declared inside do{}, they die when loop ends.

    You can refer to following code example:

    #include <vector>
    
    int main() {
    
        std::vector<float> PT1 = {1.0f, 2.0f, 3.0f};
        std::vector<float> PT2 = {1.0f, 2.0f, 3.0f};
        std::vector<float> PT3 = {1.0f, 2.0f, 3.0f};
        std::vector<float> VEC1 = {1.0f, 2.0f, 3.0f};
        std::vector<float> VEC2 = {1.0f, 2.0f, 3.0f};
        std::vector<float> VEC3 = {1.0f, 2.0f, 3.0f};
    
        float CHORD1 = 1.0f, CHORD2 = 1.0f, CHORD3 = 1.0f;
    
        std::vector<float> CHMID1(3);
        std::vector<float> CHMID2(3);
        std::vector<float> CHMID3(3);
    
        float CHTMP1 = 0.0f;
        float CHTMP2 = 0.0f;
        float CHTMP3 = 0.0f;
    
        int I = 0;
        do
        {
            float CH1 = PT1[I];
            float CH2 = PT2[I];
            float CH3 = PT3[I];
            float VC1 = VEC1[I];
            float VC2 = VEC2[I];
            float VC3 = VEC3[I];
    
            CHTMP1 = CH1 + (CHORD1 * VC1) / 2.0f;
            CHTMP2 = CH2 + (CHORD2 * VC2) / 2.0f;
            CHTMP3 = CH3 + (CHORD3 * VC3) / 2.0f;
    
            CHMID1[I] = CHTMP1;
            CHMID2[I] = CHTMP2;
            CHMID3[I] = CHTMP3;
    
            ++I;
        } while(I <= 2);
    
        return 0;
    }
    

    image (15)

    I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.

    Was this answer helpful?

    0 comments No comments

  2. AI answer

    2026-05-26T21:02:28.0833333+00:00

    PT1, PT2, PT3, VEC1, VEC2, VEC3, CHMID1, CHMID2, and CHMID3 are vectors (or other containers), so PT1[I], CHMID1[I], etc. are element accesses, not the vectors themselves.

    From the snippet and the comments:

    • CH1 is float
    • CHTMP1 is float
    • PT1 is a vector, so PT1[I] is a float element
    • CHMID1 is also a vector, so CHMID1[I] is a float element

    This means the following should be valid if the types really match:

    float CH1 = PT1[I];      // read element I from PT1 into float CH1
    float CHTMP1 = CH1 + (CHORD1 * VC1) / 2.0f;
    CHMID1[I] = CHTMP1;      // write float into element I of CHMID1
    

    However, in the code shown, the assignments that fail are:

    CHMID1[0] = CHTMP1;
    CHMID2[0] = CHTMP2;
    CHMID3[0] = CHTMP3;
    

    while later you say:

    CHMID1[I] = CHTMP1;  // DOESN'T WORK?????
    

    The key is that the compiler error is not about assigning a float to a float element; it is almost certainly about the type of CHMID1 itself or about I not being a valid index type. Common causes:

    1. CHMID1 is not a std::vector<float> (or compatible type)
      • If CHMID1 is declared as something like vector<int, allocator<int>>CHMID1; and CHTMP1 is float, then assigning a float to an int element is allowed but may warn about narrowing, not error.
      • A real compile error suggests CHMID1 might be a different type entirely (e.g., a const vector, a pointer, or a different template parameter).
    2. CHMID1 might be const
      • If declared as const std::vector<float> CHMID1;, then CHMID1[0] is not an l-value that can be assigned to, and the compiler can emit an error similar to C2105 (operator needs l-value) or a const-assignment error.
    3. I is not an integer index type
      • If I is not an integral type (e.g., a float or some other type), CHMID1[I] may not compile.
    4. Mismatch between declaration and usage
      • If CHMID1 is declared as a scalar float CHMID1; somewhere, then CHMID1[0] is invalid because a scalar is not indexable. That would produce a compile error on CHMID1[0] = CHTMP1; while CH1 = PT1[I]; works because PT1 is correctly declared as a vector.

    To fix the issue, ensure that:

    1. CHMID1, CHMID2, and CHMID3 are declared as mutable vectors of float (or the same element type as CHTMP1, CHTMP2, CHTMP3), for example:
         std::vector<float> CHMID1(3);
         std::vector<float> CHMID2(3);
         std::vector<float> CHMID3(3);
      
    2. I is an integer index type, e.g.:
         int I = 0;
      
    3. Use the same indexing style consistently:
         I = 0;
         do
         {
             float CH1 = PT1[I];
             float CH2 = PT2[I];
             float CH3 = PT3[I];
             float VC1 = VEC1[I];
             float VC2 = VEC2[I];
             float VC3 = VEC3[I];
         
             float CHTMP1 = CH1 + (CHORD1 * VC1) / 2.0f;
             float CHTMP2 = CH2 + (CHORD2 * VC2) / 2.0f;
             float CHTMP3 = CH3 + (CHORD3 * VC3) / 2.0f;
         
             CHMID1[I] = CHTMP1;
             CHMID2[I] = CHTMP2;
             CHMID3[I] = CHTMP3;
         
             ++I;
         } while (I <= 2);
      

    If the compiler error mentions "needs l-value" (C2105), that means the left-hand side of = is not a modifiable l-value. In this context, that would happen if CHMID1[0] or CHMID1[I] is not a writable element (for example, because CHMID1 is const or not an indexable container).

    Double-check the declarations of CHMID1, CHMID2, CHMID3, and I. Once they are mutable vectors with a proper element type and I is an integer, assigning CHTMP1 to CHMID1[I] will compile the same way CH1 = PT1[I]; does.


    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.