为什么我的数组值消失了?

时间:2015-03-19 17:30:58

标签: c++ arrays oop dynamic-memory-allocation

我的主要目标是填充一系列索引。这些索引将从多个 Wall 对象中检索,每个对象都包含自己的6个索引数组。

这些 Wall 对象存储在两个数组中,一个用于行,另一个用于列。

这是 Wall 类:

#ifndef WALL_H
#define WALL_H


#include "Angel.h"
#include <vector>

using namespace std;
class Wall
{
    public:
        Wall();
        Wall(int index, int orientation);
        ~Wall();
        Wall(const Wall& other);
        Wall& operator=(const Wall& other);

        int * getIndices();

        int indices[6];
        void testWall();


    protected:
    private:

        void makeRow(int index);
        void makeColumn(int index);

};

#endif // WALL_H






#include "Wall.h"

Wall::Wall()
{
    //ctor
}

//This function checks orientation and call the appropriate method
Wall::Wall(int index, int orientation)
{

    if(orientation == 0)
    {
        makeRow(index);
    }
    else
    {
        makeColumn(index);
    }

}
//this function prints the contents of the array of indices
void Wall::testWall()
{

    for (int i = 0; i < 6; ++i)
        cout << indices[i] << " ";

    cout << "\n";
}

//This function returns the array
int * Wall::getIndices()
{

    return indices;

}

//This function takes an index as an argument and populates the array of indices
//for a row wall based on that index.
void Wall::makeRow(int index)
{

    int indexAbove = index + 121;

    indices[0] = (index);
    indices[1] = (indexAbove);
    indices[2] = (indexAbove - 1);
    indices[3] = (indexAbove - 1);
    indices[4] = (index -1);
    indices[5] = (index);

    testWall();
}
//This function takes an index as an argument and populates the array of indices
//for a column wall based on that index. It then tests the wall
void Wall::makeColumn(int index)
{

    int indexAbove = index + 121;

    indices[0] = (index);
    indices[1] = (index - 11);
    indices[2] = (indexAbove - 11);
    indices[3] = (indexAbove - 11);
    indices[4] = (indexAbove);
    indices[5] = (index);

    testWall();

}

Wall::Wall(const Wall& other)
{
    //copy ctor
}
 Wall::~Wall()
{
}

Wall& Wall::operator=(const Wall& rhs)
{
    if (this == &rhs) return *this; // handle self assignment
    //assignment operator
    return *this;
}

当我填充 Walls 的数组时,我会测试这些值以验证输入是否正确并将其打印出来。

//This function populates 2 arrays of Wall objects. One for rows and one for columns.
void makeGrid()
{
    //Access to lower level vertices
    for(int i = 0; i < 11; ++i)
        for( int j = 0; j < 11; ++j){

                int index = (i * 11) + j;

                printf("for index %d, these are the related indices\n", index );

                if(j != 0){
                    rows[index] = Wall(index, 0);
                }
                if(i != 0){
                    columns[index] = Wall(index, 1) ;
                }
        }
}

然而,当我稍后检索它们时,值全部变为零,就好像我的数组都未初始化或创建新对象一样。我不知道我哪里出错了!

//This function tests the indices array contained within each wall of the rows array.
void getIndices()
{
    int index = 0;
    int * verts = NULL;

    for (int i = 0; i < numRows; ++i)
        rows[i].testWall();
/*

        verts = rows[1].getIndices();
        for(int j = 0; j < 6; j++){
            indices[index] = rows[i].indices[j];
            //cout << indices[index] << " ";
            index++;
        }
    }

    for (int i = 0; i < numColumns; i++){
        verts = columns[i].getIndices();
        for(int j = 0; j < 6; j++){
            indices[index] = verts[j];
            cout << verts[j] << " ";
            index++;
        }
    }
*/
}

编辑: 这些是我使用

的数组的声明
const int numVertices = 242, numColors = 242, numRows = 110, numColumns = 110;
const int numIndices = (numRows + numColumns) * 6;

vec4 vertices[numVertices];
vec4 colors[numColors];
Wall rows[numRows];
Wall columns[numColumns];
GLint indices[numIndices];






57 178 177 177 56 57
57 46 167 167 178 57
for index 58, these are the related indices
58 179 178 178 57 58
58 47 168 168 179 58
for index 59, these are the related indices
59 180 179 179 58 59
59 48 169 169 180 59
for index 60, these are the related indices
60 181 180 180 59 60
60 49 170 170 181 60
for index 61, these are the related indices
61 182 181 181 60 61
61 50 171 171 182 61
for index 62, these are the related indices
62 183 182 182 61 62
62 51 172 172 183 62
for index 63, these are the related indices
63 184 183 183 62 63
63 52 173 173 184 63
for index 64, these are the related indices
64 185 184 184 63 64
64 53 174 174 185 64
for index 65, these are the related indices
65 186 185 185 64 65
65 54 175 175 186 65
for index 66, these are the related indices
66 55 176 176 187 66
for index 67, these are the related indices
67 188 187 187 66 67
67 56 177 177 188 67
for index 68, these are the related indices
68 189 188 188 67 68
68 57 178 178 189 68
for index 69, these are the related indices
69 190 189 189 68 69
69 58 179 179 190 69
for index 70, these are the related indices
70 191 190 190 69 70
70 59 180 180 191 70
for index 71, these are the related indices
71 192 191 191 70 71
71 60 181 181 192 71
for index 72, these are the related indices
72 193 192 192 71 72
72 61 182 182 193 72
for index 73, these are the related indices
73 194 193 193 72 73
73 62 183 183 194 73
for index 74, these are the related indices
74 195 194 194 73 74
74 63 184 184 195 74
for index 75, these are the related indices
75 196 195 195 74 75
75 64 185 185 196 75
for index 76, these are the related indices
76 197 196 196 75 76
76 65 186 186 197 76
for index 77, these are the related indices
77 66 187 187 198 77
for index 78, these are the related indices
78 199 198 198 77 78
78 67 188 188 199 78
for index 79, these are the related indices
79 200 199 199 78 79
79 68 189 189 200 79
for index 80, these are the related indices
80 201 200 200 79 80
80 69 190 190 201 80
for index 81, these are the related indices
81 202 201 201 80 81
81 70 191 191 202 81
for index 82, these are the related indices
82 203 202 202 81 82
82 71 192 192 203 82
for index 83, these are the related indices
83 204 203 203 82 83
83 72 193 193 204 83
for index 84, these are the related indices
84 205 204 204 83 84
84 73 194 194 205 84
for index 85, these are the related indices
85 206 205 205 84 85
85 74 195 195 206 85
for index 86, these are the related indices
86 207 206 206 85 86
86 75 196 196 207 86
for index 87, these are the related indices
87 208 207 207 86 87
87 76 197 197 208 87
for index 88, these are the related indices
88 77 198 198 209 88
for index 89, these are the related indices
89 210 209 209 88 89
89 78 199 199 210 89
for index 90, these are the related indices
90 211 210 210 89 90
90 79 200 200 211 90
for index 91, these are the related indices
91 212 211 211 90 91
91 80 201 201 212 91
for index 92, these are the related indices
92 213 212 212 91 92
92 81 202 202 213 92
for index 93, these are the related indices
93 214 213 213 92 93
93 82 203 203 214 93
for index 94, these are the related indices
94 215 214 214 93 94
94 83 204 204 215 94
for index 95, these are the related indices
95 216 215 215 94 95
95 84 205 205 216 95
for index 96, these are the related indices
96 217 216 216 95 96
96 85 206 206 217 96
for index 97, these are the related indices
97 218 217 217 96 97
97 86 207 207 218 97
for index 98, these are the related indices
98 219 218 218 97 98
98 87 208 208 219 98
for index 99, these are the related indices
99 88 209 209 220 99
for index 100, these are the related indices
100 221 220 220 99 100
100 89 210 210 221 100
for index 101, these are the related indices
101 222 221 221 100 101
101 90 211 211 222 101
for index 102, these are the related indices
102 223 222 222 101 102
102 91 212 212 223 102
for index 103, these are the related indices
103 224 223 223 102 103
103 92 213 213 224 103
for index 104, these are the related indices
104 225 224 224 103 104
104 93 214 214 225 104
for index 105, these are the related indices
105 226 225 225 104 105
105 94 215 215 226 105
for index 106, these are the related indices
106 227 226 226 105 106
106 95 216 216 227 106
for index 107, these are the related indices
107 228 227 227 106 107
107 96 217 217 228 107
for index 108, these are the related indices
108 229 228 228 107 108
108 97 218 218 229 108
for index 109, these are the related indices
109 230 229 229 108 109
109 98 219 219 230 109
for index 110, these are the related indices
110 99 220 220 231 110
for index 111, these are the related indices
111 232 231 231 110 111
111 100 221 221 232 111
for index 112, these are the related indices
112 233 232 232 111 112
112 101 222 222 233 112
for index 113, these are the related indices
113 234 233 233 112 113
113 102 223 223 234 113
for index 114, these are the related indices
114 235 234 234 113 114
114 103 224 224 235 114
for index 115, these are the related indices
115 236 235 235 114 115
115 104 225 225 236 115
for index 116, these are the related indices
116 237 236 236 115 116
116 105 226 226 237 116
for index 117, these are the related indices
117 238 237 237 116 117
117 106 227 227 238 117
for index 118, these are the related indices
118 239 238 238 117 118
118 107 228 228 239 118
for index 119, these are the related indices
119 240 239 239 118 119
119 108 229 229 240 119
for index 120, these are the related indices
120 241 240 240 119 120
120 109 230 230 241 120
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 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0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Process returned 0 (0x0)   execution time : 2.445 s
Press any key to continue.

请帮帮我!谢谢!

1 个答案:

答案 0 :(得分:4)

如果你的拷贝构造函数只有:

Wall::Wall(const Wall& other)
{
    //copy ctor
}

这是问题的根源。让编译器为您生成它或正确实现它。

Wall::Wall(const Wall& other)
{
    for ( int i = 0; i < 6; ++i )
    {
       indices[i] = other.indices[i];
    }
}

赋值运算符遇到同样的问题(Thanks @πάνταῥεῖ)。