no match for 'operator=' in vector[][]

This is a discussion on no match for 'operator=' in vector[][] within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <math.h> #include <vector> using namespace std; int main (void) { int sv = 0, fv = ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    no match for 'operator=' in vector[][]

    Code:
    #include <iostream>
    #include <math.h>
    #include <vector>
    using namespace std;
    
    int main (void) {
    
    int sv = 0, fv = 0;
    
    cout << "Start Value:" << endl;
    cin >> sv;
    cout << "End Value:" << endl;
    cin >> fv;
    cout << endl;
    
    int size = (int)sqrt(fv-sv)+5;
    
    vector <vector<int> > grid [size][size];
    vector <vector<int> > grid2 [15][15];
    
    int midx = (int)(sqrt(fv-sv)+5/2)-1, midy = (int)(sqrt(fv-sv)+5/2)-1;
    int down = 0, right = 0, left = 0, up = 0;
    int yy = 0, xx = 0;
    int count = 0;
    string dir = "down";
    
    for (int i = 0; i <= 5 ; i++) {
    
    if (dir=="down") {
    for (int i2 = 0; i2 < 1 ; i2++) {
    grid2[1][1] = 5;
    grid[0][0] = 5;
    grid [midx][midy] = i;
    }
    dir = "right";
    }
    
    }
    
    
    
    
    
    
    }
    When I compile the code above, I get this error "no match for 'operator=' in 'grid2[1][1] = 5' ". I'm clueless as to what's the problem... Thx!


    31 C:\Documents and Settings\Administrator\Desktop\Testing Files Folder\J42001.cpp no match for 'operator=' in 'grid2[1][1] = 5'

    note C:\Dev-Cpp\include\c++\3.4.2\bits\vector.tcc:131 candidates are: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>:perator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]

    32 C:\Documents and Settings\Administrator\Desktop\Testing Files Folder\J42001.cpp no match for 'operator=' in 'grid[0][0] = 5'

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You're using the wrong syntax for construction. To construct grid2 with an initial size of 15x15 use this syntax:
    Code:
    vector <vector<int> > grid2(15, vector<int>(15));
    That will create 15 vectors each containing 15 integers initialized to 0. You'll have to do something similar for the grid variable.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    ohmygods. Thank you !

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you don't need the vector to grow then why not just use a plain old array?
    Otherwise use the push_back() member function to add elements to the vector.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Better yet, std::tr1::array or boost::array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> If you don't need the vector to grow then why not just use a plain old array?

    That won't work for grid, since it has a dynamic size. Might as well use vector for both and initialize the size in the constructor since you can determine that size when you create the instance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 06:37 PM
  2. no match for 'operator>>'
    By Taka in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2009, 12:17 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. 2 array match
    By ajastru2000 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2003, 07:58 AM
  5. How do I match 2 files to print data.
    By sketchit in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-12-2001, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21