# assignment of read-only location (vectors)

• 12-16-2010
jlangfo5
Hey guys, I just finished semester one at school, and I have been doing some coding exercises to keep sharp over the winter break, and I have been some new errors on this exercise. This exercise came from a "top-coder" practice problem that was used a few years ago.

Here is the problem statement:
Code:

```........ A chessboard pattern is a pattern that satisfies the following conditions: The pattern has a rectangular shape. The pattern contains only the characters '.' (a dot) and 'X' (an uppercase letter X). No two symbols that are horizontally or vertically adjacent are the same. The symbol in the lower left corner of the pattern is '.' (a dot). You are given two ints rows and columns. Write a method that computes the chessboard pattern with these dimensions, and returns it in a vector <string>. The elements of the return value correspond to rows of the pattern. Specifically, the first character of the last element of the return value represents the lower left corner```
I addressed the problem by writing this function here:
Code:

```vector <string> makeChessboard (int w, int h){     string row = "";     vector<string>Board();     int i = 0;     for(int height = 0; height < h; height++){         while(i<(w*h)){             for(int j = 0; j<w; j++){                 if (i % 2 == 0){                     row[j] = '.';                 }                 else{                 row[j] = 'X';                 }                 i++;             }         }         Board[height] = row;     }     return Board; }```
I am getting many errors on line 23 were I say
Code:

` Board[height] = row;`
, including:
Code:

```|23|warning: pointer to a function used in arithmetic |23|error: assignment of read-only location '*(Board + ((unsigned int)height))'| |23|error: cannot convert 'std::string' to 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >()' in assignment| |25|error: conversion from 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > (*)()' to non-scalar type 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >' requested|```
I have no clue why I am getting those errors, I have never ran into the first warning before, could someone please shed some light on what I am doing wrong? I tried using push back also to no avail. I don't know why it is complaining about trying to convert between types. If you guys could help out with this I would appreciate it a ton! Thanks a ton guys! I appreciate it!
• 12-16-2010
tabstop
Code:

`vector<string>Board();`
Here you declare Board to be a function returning a vector<string> (much like the function you're in, as a matter of fact), not an object of that type. If you want an empty constructor list, then simply leave off the parentheses.
• 12-16-2010
jlangfo5
Well thanks tabstop! I was able to fix that and it then compiled, but then everyone's favorite, the segfault appeared. I tried to test my function to see if it would work correctly, with this code here.
Code:

```#include <iostream> #include <vector> using namespace std; vector <string> makeChessboard (int w, int h){     string row = "";     vector<string>Board;     int i = 0;     for(int height = 0; height < h; height++){         while(i<(w*h)){             for(int j = 0; j<w; j++){                 if (i % 2 == 0){                     row[j] = '.';                 }                 else{                 row[j] = 'X';                 }                 i++;             }         }         Board[height] = row;     }     return Board; } int main() {     int h = 8;     int w = 8;     vector<string>chess;     cout<< "making chess";     chess = makeChessboard (h, w);     cout << "chess made";     for(int i = 0; i<chess.size(); i++){     cout << chess[i] << endl;}     return 0; }```
It outputs "making chess", but crashes when it tries to do the assignment. So, it never gets to "chess made". Any takers? Thanks once again guys!
• 12-17-2010
Elysia
You are using the vector without first resizing it to appropriate size. Also, you forgot to include <string>.
Same thing with the string. You are trying to access individual characters when it's empty.
Logic is also wrong. You seem to be doing it in a overcomplicated way. I'd sit down and think a little more about how to accomplish it.
• 12-17-2010
jlangfo5
Thanks Elysia! It compiled after I used push back on the string instead of trying to assign to it like i was! Now I just have to fix a logic that error that isn't so bad, and it will be working properly! I'll post it when i have it working right!