0

Still relatively new to vectors in C++, the aim of this function is to take 4 arguments, 3 of which define the (x , y , z) position of the data being written, and the 4th being the value that is to be written.

as Requested, a picture of the errors is listed:

Picture of code listed above

The issue is under the "push_back" code. the "." after yy.push and xx.push is giving the error "no instance of overloaded function".

If somebody could explain what this means and how to fix it I would greatly appreciate it! :)

double datawrite(vector<unsigned int> xx, vector<unsigned int> yy, 
vector<unsigned int> zz, double val) {
//Writes data to the 3d Vector

//finds coordinates for data
    vector< vector< vector<unsigned int > > > xx;
    vector< vector<unsigned int> > yy;
    vector<unsigned int> zz;

//Writes value at proper position
    zz.push_back(val);
    yy.push_back(zz);
    xx.push_back(yy);

    //outputs value from vector
    return val;
}
  • 3
    `zz` is a `vector` of `unsigned int` and you are trying to push a `double`. – Code-Apprentice Feb 21 '18 at 20:06
  • 8
    Don't name your local variables the same as the function arguments. – 001 Feb 21 '18 at 20:07
  • 3
    Please show the **exact** error message. Paraphrasing loses information that can be important for us to help you. – Code-Apprentice Feb 21 '18 at 20:08
  • 1
    Why are you passing coordinates as vectors? – mfkw1 Feb 21 '18 at 20:09
  • I hope this is code is only an exercise and I hope that at some point you will understand that this method is effectively the same as `double datawrite(double val) { return val;}` – 463035818_is_not_an_ai Feb 21 '18 at 20:44
  • 1
    is it possible that you want to set the value `val` at some index `(x,y,z)` of a 3d data structure? ...because your code is doing something quite different – 463035818_is_not_an_ai Feb 21 '18 at 20:47
  • 1
    You have more than one definition of `xx`, `yy` and `zz` – user253751 Feb 21 '18 at 22:44
  • @ kubawal: I am attempting to create a neural network, where the x and y coordinates are the node locations, and the z values represent the weights it has and which other neuron the weight is connected to.. EX [0,0,0] are coordinates [0,0,1] = weight, [0,0,2] = xdestination [0,0,3] = ydestination. – Christian Potts Feb 21 '18 at 22:59

1 Answers1

0

So you want a 3d matrix of doubles? First you need to create it:

#include <vector>
std::vector<vector<vector<double>>> matrix;

This creates a 3d matrix, but with 0 size. Next, when you add data to the matrix, you need to make sure the matrix is big enough:

// Co-ords are integers
double datawrite(int x, int y, int z, double val)
{
    // Make sure vectors are large enough
    if (matrix.size() < x+1) matrix.resize(x+1);
    if (matrix[x].size() < y+1) matrix[x].resize(y+1);
    if (matrix[x][y].size() < z+1) matrix[x][y].resize(z+1);

    // Store the value
    matrix[x][y][z] = val;
    return val;
}

However, this is a bit messy and leaves the matrix in an incomplete state. For example, if you call datawrite(2, 3, 4, 9.9); this may appear that all indexes < 2,3,4 would be valid, but they are not. For example trying to read matrix[0][0][0] will give you an error.

You could work around this with a dataread function that checks the sizes of the vectors before trying to read from them.

If you know ahead of time how large the matrix is, you can create the entire matrix at once like this:

vector<vector<vector<double>>> matrix(10, vector<vector<double>>(10, vector<double>(10)));

This creates a complete 10x10x10 matrix. This ensures all indexes < 10 will be valid. I prefer this method. Then your function becomes:

double datawrite(int x, int y, int z, double val)
{
    // Make sure indexes are valid
    if (x >= matrix.size() || y >= matrix[x].size() || z >= matrix[x][y].size()) {
        // Up to you what to do here.
        // Throw an error or resize the matrix to fit the new data
    }

    // Store the value
    matrix[x][y][z] = val;
    return val;
}
001
  • 13,291
  • 5
  • 35
  • 66
  • Thank you for your quick response, however it seems i'm still facing an error. When i try to push through a function "double hello = datawrite(0,0,0,1)" I get an error that says the "Vector subscript is out of range. – Christian Potts Feb 21 '18 at 23:45
  • 2
    `reserve` does NOT "make sure the matrix is big enough". It allocates memory to get ready for a bigger size, but without actually changing the size. `std::vector v; v.reserve(10); v[0] = 0;` is just as invalid as if you didn't have the `reserve`. – aschepler Feb 22 '18 at 00:00
  • @aschepler My mistake. I meant [`resize`](http://en.cppreference.com/w/cpp/container/vector/resize). I've updated the answer. Thanks. – 001 Feb 22 '18 at 00:49
  • Thank you very much for this solution! It is neat and compact and does everything I want! i would give you an up-vote but i cant yet, thanks friends! – Christian Potts Feb 22 '18 at 01:54