私は完全なC++の初心者です。 私はかなり基本的なようないくつかのことをしようとしています:特定のクラスのオブジェクトを作成し、ベクトルに格納し、そのオブジェクトの値を出力します。現時点では、私が格納している文字に関係なく長方形の文字を出力します。何かアドバイスや背景情報が高く評価されオブジェクトがベクトルで正しく格納/検索されない(C++)
#include <iostream>
#include <string>
#include <cstdlib>
#include <conio.h>
#include <time.h>
#include <windows.h>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
class terrainType
{
public:
string name;
char symbol;
int freq;
terrainType(string,char,int);
};
terrainType::terrainType(string name, char symbol, int freq)
{
name=name;
symbol=symbol;
freq=freq;
}
int main()
{
vector<terrainType> terrainTypes;
terrainType dirt("dirt",'.',1);
terrainTypes.push_back(dirt);
cout << terrainTypes[0].symbol;
return 0;
}
: は、ここでは、コードです。ありがとう!
あるいは、 'this-> name'などを通してデータメンバにアクセスしてください。もちろん、メンバの初期化はコンストラクタでうまくいきます。 – chris
ありがとうございました。私は変数名を変更し、それは完全に動作します。 – Austen
これは、データメンバーの先頭にアンダースコアを付ける理由の1つです。それは私が下線なしで、そして面倒なく、同じ名前を関数で使用することを可能にします。 – chris