我一直得到堆栈溢出错误,并试图修复这个3天.我正在为cd创建一个媒体库
I keep getting stack overflow error and have tried fixing this for 3 days. I am creating a media library for CDs
//长话短说,试图做一个媒体库,但我在一个100%的完全损失,为什么我不能得到这个数据的工作。这是我的Main.cpp
#include "CDclass.h"
bool fills = true;//Public Switch to turn on/off autofill of "Data" classes.
bool runner = true;//Public switch that helps with the program functionality(DO NOT EDIT)
void main()
{
int decision;
unsigned int total = 5;
vector<string> titles;
vector<double> time;
string artist;
string name;
titles.resize(total);
time.resize(total);
vector<cdStorage> Data;//A vector of classes
cdStorage newCD;
Data.push_back(newCD);
Data.push_back(newCD);
Data.push_back(newCD);//This was used as a sizing test and it works.
cdStorage::cdStorage(total);
//I used this to loop without restarting main.
while(runner == true)
{
if(fills == true)//Autofill to get the program running
{
artist = "Bunny";
name = "Bread";
for(unsigned int x = 0; x < Data.size(); x++)
{
cdStorage::cdStorage(total);
Data[x].setNewArtist(artist);
Data[x].setNewName(name);
for(unsigned int y = 0; y < total; y++)
{
titles[y] = "TestfieldBanana!";
time[y] = 12.13;
Data[x].setNewTitles(y, titles[y]);
Data[x].setNewTime(y, time[y]);
}
}
fills = false;
}
cout << Data[0].getNewArtist() << endl;
cout << "*******************" << endl <<
"*Media Awesomsauce*" << endl <<
"*******************" << "nn" <<
"********************" << endl <<
"* 1: Check Library *" << endl <<
"* 2: Add CD *" << endl <<
"* 3: Delete CD *" << endl <<
"* 4: Exit Program *" << endl <<
"********************" << "nn" <<
"Decision:_";
cin >> decision;
//The majority of all of this is very self explanatory.
if(decision == 1)
{
for(unsigned int x = 0; x < Data.size(); x++)
{
cdStorage::cdStorage(total);
cout << Data[x].getNewName() << "t";
cout << Data[x].getNewArtist() << "t";
for(unsigned int y = 0; y < total; y++)
{
//int length = Data[x].getNewName().length();
cout << "ttt" << Data[x].getNewTitles(y);
cout << "t" << Data[x].getNewTime(y) << endl;
}
}
}else if(decision == 2)
{
Data.push_back(newCD);
system("CLS");
cout << "What is the name of the CD: ";
cin >> name;
cout << "nWhat is the name of the Artist: ";
cin >> artist;
cout << "nHow many songs are there: ";
cin >> total;
cdStorage::cdStorage(total);
titles.resize(total);
time.resize(total);
Data[Data.size()].setNewName(name);
Data[Data.size()].setNewArtist(artist);
cout << "What are the song titles and lengths:n";
for(unsigned int x = 0; x < total; x++)
{
cout << "Title " << x+1 << ": ";
getline (cin, titles[x]);
cout << "Length(Example: 3.36 for 3 mins and 36 seconds): ";
cin >> time[x];
cout << endl;
Data[Data.size()].setNewTitles(x, titles[x]);
Data[Data.size()].setNewTime(x, time[x]);
}
}else if(decision == 3)
{
}else if(decision == 4)
{
runner = false;
}else
{
system("CLS");
cout << "Error: You must choose a number between 1-5...nn";
system("pause");
system("CLS");
}
}
}
//这是我的CDWorks.cpp
#include "CDclass.h"
//Constructor
cdStorage::cdStorage(){};
//Overloaded Constructor
cdStorage::cdStorage(unsigned int theTotal)
{
newTotal = theTotal;
newTitles.resize(newTotal);
newTime.resize(newTotal);
}
//Accessors
unsigned int cdStorage::getNewTotal() const
{
return newTotal;
}
string cdStorage::getNewTitles(unsigned int x) const
{
return newTitles[x];
}
double cdStorage::getNewTime(unsigned int x) const
{
return newTime[x];
}
string cdStorage::getNewArtist() const
{
return newArtist;
}
string cdStorage::getNewName() const
{
return newName;
}
//Mutators
void cdStorage::setNewTotal(unsigned int theTotal)
{
newTotal = theTotal;
}
void cdStorage::setNewTitles(unsigned int x, string theTitle)
{
newTitles[x] = theTitle;
}
void cdStorage::setNewTime(unsigned int x, double theTime)
{
newTime[x] = theTime;
}
void cdStorage::setNewArtist(string theArtist)
{
newArtist = theArtist;
}
void cdStorage::setNewName(string theName)
{
newName = theName;
}
//Destructor
cdStorage::~cdStorage(){}
//这是我的CDClass.h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#ifndef CDCLASS_H
#define CDCLASS_H
class cdStorage
{
private:
unsigned int newTotal;
vector<string> newTitles;
vector<double> newTime;
string newArtist;
string newName;
public:
//Constructor
cdStorage();
//Overloaded Constructor
cdStorage(unsigned int);
//Destructor
~cdStorage();
//Accessors
unsigned int getNewTotal() const;
string getNewTitles(unsigned int) const;//The integer is to track which element needs returned.
double getNewTime(unsigned int) const;
string getNewArtist() const;
string getNewName() const;
//Mutators
void setNewTotal(unsigned int);
void setNewTitles(unsigned int, string);
void setNewTime(unsigned int, double);
void setNewArtist(string);
void setNewName(string);
};
#endif
Data[Data.size()]
正在访问向量Data
之外,这是未定义的行为,因此任何事情都可能发生。
同样,我不知道你认为反复调用cdStorage::cdStorage(total);
做什么,但它不做任何事情,除了创建一个新的(匿名)对象,立即扔掉。
您创建的所有cdStorage
s都是使用默认(无参数)构造函数创建的,这使得newTotal
完全未初始化,vector
s都是空的。你不能通过调用构造函数来修改它们(我怀疑这就是你想要完成的)。
由于向量为空,当您说例如newTitles[x] = theTitle;
时,您正在访问无效内存,这意味着您的程序再次具有未定义的行为。
很难说这些是不是你的问题的原因,但你应该先解决它们,然后再继续。
你应该复习一下你的Fine Book中关于构造函数和实例创建的章节。
Data[Data.size()].setNewName(name);
访问vector的末尾,它只有Data.size()
个元素,从0开始。这是未定义的行为,可能导致问题。
这可能不是问题所在,但由于您没有说明错误发生在哪里,因此很难知道。你有一个失败的程序,你应该能够调试它,并指出它在哪里爆炸……你有三天的时间来学习使用调试器!
直到你知道你在做什么,我建议你停止使用[x]
来访问向量,并切换到使用at(x)
函数,它做同样的事情,但检查x
是一个有效的索引,不大于向量的大小。如果你这样做了,那么你就会在第一个问题上得到一个异常,而不是未定义的行为和堆栈溢出。
还有很多其他的问题…
把include守卫放在文件的顶部,而不是在其他头文件之后。
永远不要把using namespace
放在标题的命名空间范围内。
你继续这样做:
cdStorage::cdStorage(total);
那应该是什么,为什么你一直这样做?
应该在构造函数中使用成员初始化式,而不是在构造函数体中修改它们:
cdStorage::cdStorage(unsigned int theTotal)
{
newTotal = theTotal;
newTitles.resize(newTotal);
newTime.resize(newTotal);
}
。这样做:
cdStorage::cdStorage(unsigned int theTotal)
: newTotal(theTotal), newTitles(theTotal), newTime(theTotal)
{ }
- 为什么我的代码在输出中增加了93天
- 了解每月第一天函数的代码
- 以天C++为单位的两个时间戳之间的差异
- C++ Chrono 确定一天是否是周末?
- 编写了一个C++代码来检查表达式是否具有平衡括号并且我的代码未运行.我已经卡了一天了
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 圣诞节的第N天,C++递归
- 在批准 C++20 之前,我应该如何使用自纪元以来的天数值?
- std::get_time 没有解析一天
- 无法在 openGL 中渲染天空盒
- 以秒为单位将时间从纪元转换为一年中的某一天?
- 如何将 cdparanoia 读取的 CD 音频数据移交给 ALSA 播放器?
- 解决一年中的一天 c++
- Qt - 如何在 30 天后删除日志文件,文件名不固定
- 使用函数的圣诞节C++12天
- Strftime在iOS上返回一周中不正确的一天
- 事件(开始,结束),1天的最大事件.C++ 中的性能问题
- 这一年C++天过去了
- 为什么一个天真的'iter_swap`电位比``swap''慢得多
- 我一直得到堆栈溢出错误,并试图修复这个3天.我正在为cd创建一个媒体库