CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Feb 2016
    Posts
    8

    error C2106: '=' : left operand must be l-value

    hi to all,
    I use Visual c++ 6.00
    I have a code which in my opinion should work:
    .....
    CString one=" some text Q the rest of the text ";
    CString two;
    int x, y;
    x=y=0;
    while (one [x]!='Q')
    two [y]=one[x]; // it gives error at this line: error C2106: '=' : left operand must be l-value
    ....
    why don't it work?
    Could anyone help me please
    Thanks
    Val

  2. #2
    Join Date
    Feb 2016
    Posts
    8

    Re: error C2106: '=' : left operand must be l-value

    sorry to all
    I found a mistake in code, it should be:

    .....
    CString one=" some text Q the rest of the text ";
    CString two;
    int x, y;
    x=y=0;
    while (one [x]!='Q')
    {
    two [y]=one[x]; // it gives error at this line: error C2106: '=' : left operand must be l-value
    x+=1;
    y+=1;
    }
    ....

    but it still will give the same error....

  3. #3
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: error C2106: '=' : left operand must be l-value

    use either
    char two[50]
    or
    CString * two and use new .. delete.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: error C2106: '=' : left operand must be l-value

    CString[] only allows the value of a char to be obtained. It cannot be used to change. It can be used on right of = only and not on left side. See https://msdn.microsoft.com/en-us/lib...=vs.60%29.aspx

    If you want to change you need to use SetAt(). See https://msdn.microsoft.com/en-us/lib...=vs.60%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: error C2106: '=' : left operand must be l-value

    Since the variable, two, has a length of zero, I think that you should use operator +=

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: error C2106: '=' : left operand must be l-value

    Quote Originally Posted by Philip Nicoletti View Post
    Since the variable, two, has a length of zero, I think that you should use operator +=
    Exactly!
    Using CString::SetAt method could (in OP's context) lead to the application crash because of access violation!
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: error C2106: '=' : left operand must be l-value

    I didn't properly read the code ! However, if the requirement is that two should contain the contents of one upto but not including the Q then consider using .Find() and .Left().

    Code:
    #include <afx.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	CString one = " some text Q the rest of the text ";
    
    	int f = one.Find('Q');
    	CString two = (f < 0) ? one : one.Left(f);
    
    	cout << two << endl;
    }
    Last edited by 2kaud; February 25th, 2016 at 08:23 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: error C2106: '=' : left operand must be l-value

    Take a look at your code in post # 2, and consider what would happen if the variable one did not have a 'Q' in it (even assuming you used +=). 2kaud suggestion of using find bypasses these types of problems.

  9. #9
    Join Date
    Feb 2016
    Posts
    8

    Re: error C2106: '=' : left operand must be l-value

    Hi to all,
    Thank to all of you, that was a great help for me
    It finally worked in this way:
    ........
    while (one [x]!='Q')
    {
    two+=1;
    two .SetAt(y,one[x]);
    x+=1;
    y+=1;
    }
    ........
    works – happy days
    VictorN was right it crashed at the start, but when I added // two+=1;
    It worked
    Thanks to Philip for his advise...
    I am searching a string which would defiantly have the character I am looking for.
    The part of the string I need is in the middle of the cstring one this is why I didn't use // one.Find('Q')
    ...............
    again thanks a lot to all for the your help...

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: error C2106: '=' : left operand must be l-value

    I am searching a string which would defiantly have the character I am looking for.
    It is still good practice to deal with the case where it doesn't.

    If you want it coded using a while loop then you are appending 1 to the string and then using SetAt to change this to the required char! Consider
    Code:
    #include <afx.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	CString one = " some text Q the rest of the text ";
    	CString two;
    
    	for (int x = 0; (x < one.GetLength()) && (one[x] != 'Q'); ++x)
    		two += one[x];
    
    	cout << two << endl;
    }
    The part of the string I need is in the middle of the cstring one this is why I didn't use // one.Find('Q')
    So why are you setting two to the first part of one? If you want to extract part of string one then use .Find() and .Mid() (or .Right()). What part of the string do you actually want?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Feb 2016
    Posts
    8

    Re: error C2106: '=' : left operand must be l-value

    Quote Originally Posted by 2kaud View Post
    It is still good practice to deal with the case where it doesn't.

    If you want it coded using a while loop then you are appending 1 to the string and then using SetAt to change this to the required char! Consider
    Code:
    #include <afx.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	CString one = " some text Q the rest of the text ";
    	CString two;
    
    	for (int x = 0; (x < one.GetLength()) && (one[x] != 'Q'); ++x)
    		two += one[x];
    
    	cout << two << endl;
    }


    So why are you setting two to the first part of one? If you want to extract part of string one then use .Find() and .Mid() (or .Right()). What part of the string do you actually want?
    Finally I have used the solution you mention and it worked... the string I am working is a path string, something as: C:\Users\Public\Documents\some_folder\text.txt
    so what I need the part "some_folder" extracted and been a CString
    I have worked out how detect "\" before "some_folder" part, so what I needed was to copy everything before the next "\"...
    All worked, I mean I was able to detect where to start copping and where to stop....
    the actual copping the part of the CString one to CString two didn't work...
    But with the help from this site I was able to do it in two methods:
    1) using SetAt() function
    2) char x=one[n];
    two+=x;

    both methods work, I kept the second one...
    so, at the start I had two problems:
    1) two[y]=one[x] // never works
    2) CString two; // is empty string and it should not be empty

    thanks for the help
    regards
    Val

  12. #12
    Join Date
    Feb 2016
    Posts
    8

    Re: error C2106: '=' : left operand must be l-value

    I agree that it is good to have some code detecting the situation where it will be no 'Q' in the string.... It can't happen to my program, but I will add some procedure to detect that anywhere...
    Thanks
    Val

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: error C2106: '=' : left operand must be l-value

    the string I am working is a path string, something as: C:\Users\Public\Documents\some_folder\text.txt
    so what I need the part "some_folder" extracted and been a CString
    So why not ask this question to start with? Consider
    Code:
    #include <afx.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	CString one = "C:\\Users\\Public\\Documents\\some_folder\\text.txt";
    	int last = one.ReverseFind('\\');
    	int f1 = 0;
    
    	for (int f = 0; (f = one.Find('\\', f1)) != last; f1 = f + 1);
    
    	CString two = (last >= 0) ? one.Mid(f1, last - f1) : one;
    
    	cout << two << endl;
    
    }
    This displays
    Code:
    some_folder
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: error C2106: '=' : left operand must be l-value

    Quote Originally Posted by Lvalera View Post
    Finally I have used the solution you mention and it worked... the string I am working is a path string, something as: C:\Users\Public\Documents\some_folder\text.txt
    so what I need the part "some_folder" extracted and been a CString
    I have worked out how detect "\" before "some_folder" part, so what I needed was to copy everything before the next "\"...
    All worked, I mean I was able to detect where to start copping and where to stop....
    the actual copping the part of the CString one to CString two didn't work...
    But with the help from this site I was able to do it in two methods:
    1) using SetAt() function
    2) char x=one[n];
    two+=x;

    both methods work, I kept the second one...
    so, at the start I had two problems:
    1) two[y]=one[x] // never works
    2) CString two; // is empty string and it should not be empty

    thanks for the help
    regards
    Val
    Why not use CString::Mid to extract the portion you want?

  15. #15
    Join Date
    Feb 2016
    Posts
    8

    Re: error C2106: '=' : left operand must be l-value

    Quote Originally Posted by GCDEF View Post
    Why not use CString::Mid to extract the portion you want?
    I will try,
    thanks a lot...

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured