std::vector<class> 在调试时填充但在发布时不填充
std::vector<class> fills on debug but not on release
我有一个 class A
,其中定义了多种数据类型。在程序中,我定义了一个 A
类型的向量,然后从一个文本文件中读入这个向量。
当我调试时,矢量 "fills up" 我可以从中读取值 - 一切都按计划进行。但是,当我构建发布版本和 运行 .exe 时,向量为空。程序的其余部分工作正常,只是没有推送值。
我是 C++ 的新手,所以我假设这与我的构造函数有关,或者可能与我处理 enum
? 的方式有关。这是我的 MCVE:
#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:
1, String1, Type1,
2, String2, Type1,
有两种可能:
正如您提到的,在调试模式下进行调试时您能够获得正确的值,您有可能能够从 .txt 文件中正确读取并在向量中获得正确的值,但是当您在发布模式下调试时,您无法在监视列表中看到正确的值设置,因为即使您的向量获得正确的值,您也可能会发现在发布模式下包含垃圾值的监视列表。
你没有得到 .txt 文件,但在那种情况下,即使在调试版本中你也不应该得到 velue。
正如我在评论中提到的,最可能的问题是当您为项目创建发布版本时,程序依赖的文本文件未包含在相应的文件夹中。
要修复它,您必须自己将此文件包含在该文件夹中,或者找到一种方法向 VS 表明该程序依赖于该文本文件,这样它会自动为您复制到发布文件夹中。我不使用 VS,所以我不知道最后一部分的可能性如何,但我希望你能理解。
我有一个 class A
,其中定义了多种数据类型。在程序中,我定义了一个 A
类型的向量,然后从一个文本文件中读入这个向量。
当我调试时,矢量 "fills up" 我可以从中读取值 - 一切都按计划进行。但是,当我构建发布版本和 运行 .exe 时,向量为空。程序的其余部分工作正常,只是没有推送值。
我是 C++ 的新手,所以我假设这与我的构造函数有关,或者可能与我处理 enum
? 的方式有关。这是我的 MCVE:
#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:
1, String1, Type1,
2, String2, Type1,
有两种可能:
正如您提到的,在调试模式下进行调试时您能够获得正确的值,您有可能能够从 .txt 文件中正确读取并在向量中获得正确的值,但是当您在发布模式下调试时,您无法在监视列表中看到正确的值设置,因为即使您的向量获得正确的值,您也可能会发现在发布模式下包含垃圾值的监视列表。
你没有得到 .txt 文件,但在那种情况下,即使在调试版本中你也不应该得到 velue。
正如我在评论中提到的,最可能的问题是当您为项目创建发布版本时,程序依赖的文本文件未包含在相应的文件夹中。
要修复它,您必须自己将此文件包含在该文件夹中,或者找到一种方法向 VS 表明该程序依赖于该文本文件,这样它会自动为您复制到发布文件夹中。我不使用 VS,所以我不知道最后一部分的可能性如何,但我希望你能理解。