Invalid operands to binary expression
-
I have this code, which works on CodeBlocks, but when I try to run it in visual studio code, I get and error saying:
invalid operands to binary expression ('std::ostream' (aka 'basic_ostream >') and 'std::string' (aka 'basic_string, allocator >'))
this is the code:
#include
using namespace std;class field{
public:
void Create_Area(int area){
string field[area][area];//assign "+ " to all rows and collums and print the area for(int y = 0; y < area; y++){ for(int x = 0; x < area; x++){ field\[y\]\[x\] = "+ "; cout << field\[y\]\[x\]; } cout << endl; } }
};
What to do to fix this?
-
I have this code, which works on CodeBlocks, but when I try to run it in visual studio code, I get and error saying:
invalid operands to binary expression ('std::ostream' (aka 'basic_ostream >') and 'std::string' (aka 'basic_string, allocator >'))
this is the code:
#include
using namespace std;class field{
public:
void Create_Area(int area){
string field[area][area];//assign "+ " to all rows and collums and print the area for(int y = 0; y < area; y++){ for(int x = 0; x < area; x++){ field\[y\]\[x\] = "+ "; cout << field\[y\]\[x\]; } cout << endl; } }
};
What to do to fix this?
You cannot create an array on the stack using values that are not known at compile time. You need to change your code to:
void Create\_Area(int area){ string field\[\]\[\] = new string\[area\]\[area\];
Also, I would suggest not using the class name for an array in that way. And the
field
variable is not available outside of the method that creates it. -
You cannot create an array on the stack using values that are not known at compile time. You need to change your code to:
void Create\_Area(int area){ string field\[\]\[\] = new string\[area\]\[area\];
Also, I would suggest not using the class name for an array in that way. And the
field
variable is not available outside of the method that creates it.I have changed that, but now how do I use it in this part?
Field\[y\]\[x\] = "+ "; cout << Field\[y\]\[x\];
also, I don't really understand what you have done in the new string[area][area] part. Why use string instead of field?
-
I have changed that, but now how do I use it in this part?
Field\[y\]\[x\] = "+ "; cout << Field\[y\]\[x\];
also, I don't really understand what you have done in the new string[area][area] part. Why use string instead of field?
-
I have changed that, but now how do I use it in this part?
Field\[y\]\[x\] = "+ "; cout << Field\[y\]\[x\];
also, I don't really understand what you have done in the new string[area][area] part. Why use string instead of field?
Member 13802912 wrote:
how do I use it in this part?
Exactly the same as before.
Member 13802912 wrote:
the new string[area][area] part. Why use string instead of field?
Because you are trying to create an array of
string
objects. See new Operator (C++)[^]. -
You cannot create an array on the stack using values that are not known at compile time. You need to change your code to:
void Create\_Area(int area){ string field\[\]\[\] = new string\[area\]\[area\];
Also, I would suggest not using the class name for an array in that way. And the
field
variable is not available outside of the method that creates it.invalid operands to binary expression ('std::ostream' (aka 'basic_ostream') and 'void')
cout<
The above error is what im getting and my code is this.Need your help.Thanks!
#include
void Find(int arr[], int n, int sum)
{
unordered_set s;
int temp;
for (int i = 0; i < n; i++)
{
temp = sum - arr[i];
if (s.find(temp) != s.end())
cout<< arr[i]<<"+"<< temp<<" ";
s.insert(arr[i]);
}
}
int main()
{
int arr[] = {1,2,3,4,5,6,6,5,56,7,11,13} ;
int n = sizeof(arr)/sizeof(arr[0]);
int sum = 12;
cout< -
You cannot create an array on the stack using values that are not known at compile time. You need to change your code to:
void Create\_Area(int area){ string field\[\]\[\] = new string\[area\]\[area\];
Also, I would suggest not using the class name for an array in that way. And the
field
variable is not available outside of the method that creates it.fatal error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream') and 'void')
this is the error i am getting.What should i do?
#include
void Find(int arr[], int n, int sum)
{
unordered_set s;
int temp;
for (int i = 0; i < n; i++)
{
temp = sum - arr[i];
if (s.find(temp) != s.end())
cout<< arr[i]<<"+"<< temp<<" ";
s.insert(arr[i]);
}
}
int main()
{
int arr[] = {1,2,3,4,5,6,6,5,56,7,11,13} ;
int n = sizeof(arr)/sizeof(arr[0]);
int sum = 12;
cout< -
fatal error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream') and 'void')
this is the error i am getting.What should i do?
#include
void Find(int arr[], int n, int sum)
{
unordered_set s;
int temp;
for (int i = 0; i < n; i++)
{
temp = sum - arr[i];
if (s.find(temp) != s.end())
cout<< arr[i]<<"+"<< temp<<" ";
s.insert(arr[i]);
}
}
int main()
{
int arr[] = {1,2,3,4,5,6,6,5,56,7,11,13} ;
int n = sizeof(arr)/sizeof(arr[0]);
int sum = 12;
cout< -
You have declared:
void Find(int arr[], int n, int sum)
// and then you have:
cout<
But you cannot print something that isvoid
, i.e something that does not exist.So what should i do Instead of void i used
int Find() {.... return 0; }
But at the end of my input extra zero is coming which i dont want. Thank you!
-
So what should i do Instead of void i used
int Find() {.... return 0; }
But at the end of my input extra zero is coming which i dont want. Thank you!