assignment of read-only location (vectors)

This is a discussion on assignment of read-only location (vectors) within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I just finished semester one at school, and I have been doing some coding exercises to keep sharp ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20

    assignment of read-only location (vectors)

    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!
    Last edited by jlangfo5; 12-16-2010 at 08:30 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20
    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!

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,421
    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.
    Last edited by Elysia; 12-17-2010 at 02:55 AM.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  3. fscanf function won't read my float value...?
    By qpsnhalfs in forum C Programming
    Replies: 8
    Last Post: 07-07-2009, 11:01 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

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