0

I'm a novice coder, just a few months into learning C++. This is my second question on Stack Overflow, and I really hope that the answers will benefit me and others.

I started working on this program because I wanted to create a vector engine that could create a 1D vector, assign a value to any position in that 1D vector (by abstracting width and height to simulate 2D), and then print out said vector. The end goal of a project like this would be to later have a rendering engine that would convert the int values in this vector to image tiles on the screen using SDL or equivalent.

I previously made a program that could do something similar using a vector of objects each containing a character, but I didn't pass values by reference, so I really wanted to nail passing values by reference in this program.

The code compiles just fine, and the cout statements in the assignToVector function seem to indicate that the value is being assigned properly. But when I call the final print statement, the value that I want to pass into the vector is not properly outputted. I'm using vector.erase to clear the position before assigning a value to it, and vector.assign to input the value, if that helps narrow down the problem.

I really appreciate the time anyone spends answering this question! Thank you!

EDIT: The suggestion made by chris below fixed the first part of the problem (change std::cout << printerVector[*it] TO std::cout << *it).

However I find I needed to add a position--; after I create it in order to align the value properly. Basically, the inputted width and height values don't match their actual position on the grid. Any further help on this would be appreciated! I think it's an issue related to one dimensional vectors and using them as 2D.

//The purpose of this program is to create a one dimensional vector of integers
//that is equal to the desired screen size.  Look at the preprocessor defines
//to change these values.
//The program also contains a function to print the created vector for testing
//purposes.
#include <iostream>  //Needed for std::cout and std::endl
#include <vector>    //Needed for std::vector and others

#define CONSOLEWIDTH  80;
#define CONSOLEHEIGHT 25;

//This function is supposed to assign a value to a specific point in the 1D vector matrix.
//The posWidth and posHeight are used to compute the location with default console values, mimicking 2D.
void assignToVector(std::vector<int>& intVector, int posWidth, int posHeight, int assignValue);

//This is mostly just a testing function to ensure that the desired
//contents are properly stored in the vector.
void printVector(std::vector<int>& printerVector);

//Creates the test vector, prints it, modifies it, and prints it again
void setupAndRun();

int main()
{
    setupAndRun();
}

void assignToVector(std::vector<int>& intVector, int posWidth, int posHeight, int assignValue)
{
    int position = posWidth + posHeight*CONSOLEWIDTH;
    //std::cout << intVector[position] << std::endl;
    std::cout << position << std::endl;
    intVector.erase(intVector.begin() + position);
    //std::cout << intVector[position] << std::endl;
    std::cout << assignValue << std::endl;
    intVector.insert(intVector.begin() + position, 1, assignValue);
    std::cout << intVector[position] << std::endl;
}

void printVector(std::vector<int>& printerVector)
{
    for(std::vector<int>::iterator it = printerVector.begin(); it != printerVector.end(); it++)
    {
        std::cout << printerVector[*it];
    }
}

void setupAndRun()
{
    int width     = CONSOLEWIDTH;
    int height    = CONSOLEHEIGHT;
    //Creates a vector of size argument1 that each have a value of argument2
    //This syntax doesn't seem to work inside classes
    std::vector<int> testVector(width*height, 8);
    //80*25 8's
    printVector(testVector);
    //Suppossed to assign 200 to the 10th position of the first row of testVector
    assignToVector(testVector, 10, 0, 200);
    //Prints out 200
    std::cout << testVector[10] << std::endl;
    //Prints out default value
    std::cout << testVector[9]  << std::endl;
    //Doesn't have a 200 in it
    printVector(testVector);
}
4

0 回答 0