Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Invalid operands to binary expression

Invalid operands to binary expression

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpvisual-studioquestion
10 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13770707
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • U User 13770707

      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?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      U M 3 Replies Last reply
      0
      • L Lost User

        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.

        U Offline
        U Offline
        User 13770707
        wrote on last edited by
        #3

        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?

        L 2 Replies Last reply
        0
        • U User 13770707

          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?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 13802912 wrote:

          how do I use it in this part?

          Exactly the same as before.

          1 Reply Last reply
          0
          • U User 13770707

            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?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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++)[^].

            1 Reply Last reply
            0
            • L Lost User

              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.

              M Offline
              M Offline
              Member_14862667
              wrote on last edited by
              #6

              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<

              1 Reply Last reply
              0
              • L Lost User

                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.

                M Offline
                M Offline
                Member_14862667
                wrote on last edited by
                #7

                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<

                L 1 Reply Last reply
                0
                • M Member_14862667

                  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<

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You have declared:

                  void Find(int arr[], int n, int sum)

                  // and then you have:
                  cout<
                  But you cannot print something that is void, i.e something that does not exist.

                  M 1 Reply Last reply
                  0
                  • L Lost User

                    You have declared:

                    void Find(int arr[], int n, int sum)

                    // and then you have:
                    cout<
                    But you cannot print something that is void, i.e something that does not exist.

                    M Offline
                    M Offline
                    Member_14862667
                    wrote on last edited by
                    #9

                    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!

                    L 1 Reply Last reply
                    0
                    • M Member_14862667

                      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!

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Then don't return a value. Leave it as void and remove the cout call at the end of your main method. BTW in future please open a new question if you have a problem, rather than reopening a thread that is over two years old.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups