2017-12-18 171 views
-1

我想做一個'表'來存儲遊戲的統計數據或技能值。
我最初試圖使用帶結構的多維數組,但我希望能夠隨時添加數據行。我做了一些研究:link,發現載體可能是我需要的。
我現在試圖做一個向量的向量結構,無濟於事。我不擅長與誤差d:
這裏是我的代碼:Vector向量的結構

#include <iostream> 
#include <vector> 
using namespace std; 

int main() 
{ 
    struct Skills 
    { 
     string type; 
     int value; 
    }; 
    vector< vector<Skills> > statTable; 
    statTable[0][0].type = "test"; 
    cout << statTable[0][0].type << endl; 
    return 0; 
} 

這裏是錯誤:https://i.imgur.com/lNqbgyW.png

提前感謝!

+3

Vector是空的也達到同樣的,加元素進入它。 – Incomputable

+1

請在您的問題中添加錯誤而不是鏈接(請閱讀如何在SO中提出問題的文檔)。 – Yannis

+1

我只是想繼續說這個,希望它最終會產生效果:請不要發佈代碼或錯誤的圖片。他們是文本,你正在寫一個文本框,只是粘貼文本。它在任何可能的方式中越來越好,越來越好。 – Useless

回答

0

我編譯這個代碼,但它給了我沒有錯誤,它已成功編譯但因爲你必須在數據正確填寫,因此,這裏是我的解決方案

#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
    struct Skills 
    { 
     string type; 
     int value; 
    }; 
    vector< vector<Skills> > statTable; 
    vector<Skills> v; 
    Skills x; 
    x.type = "test"; 
    v.push_back(x); 
    statTable.push_back(v); 
    cout << statTable[0][0].type << endl; 
    return 0; 
} 

希望給了我分割錯誤這可以幫助我使用C++ 98 btw。

+0

感謝所有的迴應!沒有意識到我必須首先製作矢量:P –

1

看來你正在使用一箇舊的編譯器。在這種情況下,主要將外部結構放置在全局空間中。

struct Skills 
{ 
    string type; 
    int value; 
}; 

int main() 
{ 
    //... 

此聲明

vector< vector<Skills> > statTable; 

載體statTable之後爲空。所以你可能不會使用下標操作符。

相反申報像

vector< vector<Skills> > statTable(1, std::vector<Skills>(1)); 

的向量後,你可以寫

statTable[0][0].type = "test"; 

可以通過下面的代碼片段

vector< vector<Skills> > statTable; 
statTable.push_back(vector<Skills>()); 
statTable[0].push_back(Skills()); 

statTable[0][0].type = "test"; 
cout << statTable[0][0].type << endl;