私はクラス内にいくつかのデータ型が定義されたA
クラスを持っています。プログラム内では、タイプA
のベクトルを定義し、テキストファイルからこのベクトルに読み込みます。std :: vector <class>はデバッグを行いますが、リリース時は無効にします
私がデバッグしているとき、ベクトルは "満ちる"と私はそこから値を読み取ることができます - すべてが計画どおりに動作します。しかし、リリース版をビルドして.exeを実行すると、ベクターは空になります。残りのプログラムは正常に動作しますが、値をプッシュしないだけです。
私はかなりC++を使い慣れていないので、それは私のコンストラクタ関数と関係がありますか、あるいはおそらくenum
をどのように処理するのでしょうか?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
enum class Type
{
Type1
};
Type convertStringToType(std::string input)
{
return Type::Type1;
}
class A
{
public:
int num;
std::string str;
Type typ;
A(int refNumber, std::string name, Type type)
{
num = refNumber;
str = name;
typ = type;
}
};
std::vector<A> readFileIntoVector(std::string filename)
{
std::ifstream readFile(filename);
std::vector<A> tempVector;
std::string tempNum = "";
std::string tempStr = "";
std::string tempTyp = "";
std::getline(readFile, tempNum, ',');
std::getline(readFile, tempStr, ',');
std::getline(readFile, tempTyp, ',');
while (readFile)
{
tempVector.push_back(A(std::stoi(tempNum), tempStr, convertStringToType(tempTyp)));
std::getline(readFile, tempNum, ',');
std::getline(readFile, tempStr, ',');
std::getline(readFile, tempTyp, ',');
}
return tempVector;
}
int main()
{
std::vector<A> exampleVector = readFileIntoVector("Text.txt");
if (exampleVector.empty() == true)
{
std::cout << "Vector is empty.";
system("PAUSE");
}
else
{
int a = 1;
do
{
std::cin >> a;
if (a == 0 || a == 1)
{
std::cout << exampleVector.at(a).num << "\n";
std::cout << exampleVector.at(a).str << "\n";
}
} while (a == 0 || a == 1);
return 0;
}
}
これはTEXT.TXTである:ここに私のMCVEある
1, String1, Type1,
2, String2, Type1,
"Text.txt"には何がありますか?あなたのデバッグとリリースのビルドが同じファイルを開いてもよろしいですか?デバッグ/リリースビルドが別のディレクトリ(Visual Studioなど)で生成されている場合は、 "Text.txt"がデバッグの出力ディレクトリにのみ存在する可能性があります。 –
私はそれが長すぎる場合はお詫び申し上げます...私は変更する必要があるint、文字列または列挙型かどうかはわかりません。私は数日前からこれを見つめていて、正直であるために苦労しています。 – Jonathan
プログラムでテキストファイルが見つかるのを確認しましたか?あなたが助けを受けようとしている場合、これは重要です。 – smac89