Can't use function to change values in 2d vector of structs

I'm trying to create a 2 dimensional vector of structs within a class and then define a function to change a string contained within the structs.

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <sstream>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::stringstream;

const int worldSizeX(20);
const int worldSizeY(20);

stringstream inputStream;
string inputString = "";
int inputInt = 0;
int inputInt1 = 0;
int tempInt = 0;

class TestMap {

    struct MyTestStruct {
        string testString;
    };

    typedef vector<MyTestStruct> StructColumn;
    typedef vector<StructColumn> StructMatrix;
    StructMatrix myTestVector(StructMatrix(worldSizeX), StructColumn(worldSizeY));

public:void setTestSumm (string newSumm, int x, int y) {
        myTestVector[x][y].testString = newSumm;
    }

public:string getTestSumm (int x, int y) {
        return myTestVector[x][y].testString;
    }

};

int main() {

    TestMap mapTest;

    cout << "Enter a short place description:\n";
    getline(cin, inputString);
    cout << "Enter a number between 0 and 19:\n";
    //cin.ignore();
    cin >> inputInt;
    cout << "Enter another number between 0 and 19\n";
    cin >> inputInt1;
    mapTest.setTestSumm(inputString, inputInt, inputInt1);
    cout << "Summary: " << mapTest.getTestSumm(inputInt, inputInt1) << "\n";

}


and here are the errors I get when I try to build:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/scrambledlogic/projects/test003'
"/usr/bin/gmake"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/test003
gmake[2]: Entering directory `/home/scrambledlogic/projects/test003'
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In member function 'void TestMap::setTestSumm(std::string, int, int)':
main.cpp:32:23: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
         myTestVector[x][y].testString = newSumm;
                       ^
main.cpp: In member function 'std::string TestMap::getTestSumm(int, int)':
main.cpp:36:30: error: invalid types '<unresolved overloaded function type>[int]' for array subscript
         return myTestVector[x][y].testString;
                              ^
gmake[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
gmake[2]: Leaving directory `/home/scrambledlogic/projects/test003'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/scrambledlogic/projects/test003'
gmake: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 263ms)


I had the code working at one point after taking everything out of the TestMap class, however I haven't been able to replicate that success.
What are you trying to do on line 29?
You look like you're probably a java/C# programmer. You don't need to put public: before every public function. Here's what you meant to put I believe. I renamed some variables just because I didn't understand which one was x and which one was y.

Notice the super weird syntax on the constructor. when you want to make a vector of a certain size the constructor to use is:
 
vector<T>(n, T());


so when you want a 20x20 vector of vectors of T() you have to say:
1
2
3
vector<vector<T>> (20,
    vector<T>(20, T())
);


which can be confusing but basically you're creating a vector<T> of size 20 using the T() constructor. Then you do that 20 times for a vector<vector<T>>.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <sstream>
#include <vector>

using namespace std; //just do this if you intend to only use std objects and functions

const int X = 20;
const int Y = 20;

class TestMap {
//private:
    struct MyTestStruct {
        string testString;
    };

   vector<vector<MyTestStruct>> myTestVector;

public:
    TestMap() : myTestVector (vector<vector<MyTestStruct>>(X, vector<MyTestStruct>(Y, MyTestStruct()))) {}

    void setTestSumm (const string& newSumm, int x, int y) { //const string& if not modifying
        myTestVector.at(x).at(y).testString = newSumm; //use .at() for range checking
    }

    string getTestSumm (int x, int y) const { //put const if you don't modify member variables
        return myTestVector.at(x).at(y).testString;
    }

};

int main() {
    string input;
    string place;
    int inputX = 0;
    int inputY = 0;

    TestMap mapTest;

    cout << "Enter a short place description:\n";
    getline(cin, place);
    
    cout << "Enter a number between 0 and 19:\n";
    getline(cin, input);
    stringstream(input) >> inputX;

    cout << "Enter another number between 0 and 19\n";
    geline(cin, input);
    stringstream(input) >> inputY;

    mapTest.setTestSumm(place, inputX, inputY);
    cout << "Summary: " << mapTest.getTestSumm(inputX, inputY) << "\n";

}


I hope this helps! you may want to go look up STL templated constructors?
Ah, ok, I see now. Yeah definitely need to brush up on my constructors (and much else), I'm jumping back into c++ after first dabbling in it about 6-7 years ago. And yeah, definitely more familiar with Java. Thank you much, problem solved!
Topic archived. No new replies allowed.