-
February 24th, 2016, 09:09 PM
#1
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
-
February 24th, 2016, 11:06 PM
#2
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....
-
February 25th, 2016, 02:28 AM
#3
Re: error C2106: '=' : left operand must be l-value
use either
char two[50]
or
CString * two and use new .. delete.
-
February 25th, 2016, 04:21 AM
#4
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)
-
February 25th, 2016, 06:12 AM
#5
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 +=
-
February 25th, 2016, 06:52 AM
#6
Re: error C2106: '=' : left operand must be l-value
Originally Posted by Philip Nicoletti
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
-
February 25th, 2016, 07:36 AM
#7
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)
-
February 25th, 2016, 10:27 AM
#8
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.
-
February 25th, 2016, 09:28 PM
#9
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...
-
February 26th, 2016, 06:53 AM
#10
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)
-
February 26th, 2016, 05:57 PM
#11
Re: error C2106: '=' : left operand must be l-value
Originally Posted by 2kaud
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
-
February 26th, 2016, 06:05 PM
#12
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
-
February 27th, 2016, 05:44 AM
#13
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
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)
-
February 27th, 2016, 07:29 AM
#14
Re: error C2106: '=' : left operand must be l-value
Originally Posted by Lvalera
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?
-
February 27th, 2016, 02:17 PM
#15
Re: error C2106: '=' : left operand must be l-value
Originally Posted by GCDEF
Why not use CString::Mid to extract the portion you want?
I will try,
thanks a lot...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|