C++ 我一直收到堆栈溢出错误,并已尝试修复了3天。我正在为CD创建媒体库
//长话短说,我试图建立一个媒体库,但我完全不明白为什么我不能让这些数据发挥作用。这是我的主要.cppC++ 我一直收到堆栈溢出错误,并已尝试修复了3天。我正在为CD创建媒体库,c++,vector,stack,overflow,C++,Vector,Stack,Overflow,//长话短说,我试图建立一个媒体库,但我完全不明白为什么我不能让这些数据发挥作用。这是我的主要.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 mai
#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 <<
"*******************" << "\n\n" <<
"********************" << endl <<
"* 1: Check Library *" << endl <<
"* 2: Add CD *" << endl <<
"* 3: Delete CD *" << endl <<
"* 4: Exit Program *" << endl <<
"********************" << "\n\n" <<
"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 << "\t\t\t" << 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...\n\n";
system("pause");
system("CLS");
}
}
}
//这是我的课程
#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
#包括
#包括
#包括
使用名称空间std;
#ifndef CDCLASS_H
#定义CDCLASS_H
类存储
{
私人:
无符号整数新总数;
向量newTitles;
矢量新时间;
弦乐新艺术家;
字符串newName;
公众:
//建造师
cdStorage();
//重载构造函数
CDS存储器(无符号整数);
//析构函数
~cdStorage();
//访问者
无符号int getNewTotal()常量;
string getNewTitles(unsigned int)const;//整数用于跟踪需要返回的元素。
双getNewTime(无符号整数)常量;
字符串getNewArtist()常量;
字符串getNewName()常量;
//突变子
void setNewTotal(无符号整数);
void setNewTitles(无符号整数,字符串);
void setNewTime(无符号整数,双精度);
void setNewArtist(字符串);
void setNewName(字符串);
};
#恩迪夫
这将访问向量的末尾,它只有Data.size()
元素,从零开始这是未定义的行为,可能导致问题
这可能不是问题所在,但正如您没有说过错误发生在哪里一样,很难知道。你有失败的程序,你应该能够调试它,并说它在哪里爆炸。。。您有三天的时间学习使用调试器
在你知道你在做什么之前,我建议你停止使用[x]
访问向量,转而使用at(x)
函数,该函数做同样的事情,但检查x
是否为有效索引且不大于向量的大小。如果这样做了,那么在第一个问题上就会出现异常,而不是未定义的行为和堆栈溢出
还有许多其他问题
将include-guard放在文件的顶部,而不是其他标题之后
切勿将使用名称空间
放在标题中的名称空间范围内
你继续这样做:
cdStorage::cdStorage(总计)代码>
那应该做什么?你为什么一直这么做
您应该在构造函数中使用成员初始值设定项,而不是在构造函数体中更改它们:
cdStorage::cdStorage(unsigned int theTotal)
{
newTotal = theTotal;
newTitles.resize(newTotal);
newTime.resize(newTotal);
}
i、 e.改为:
cdStorage::cdStorage(unsigned int theTotal)
: newTotal(theTotal), newTitles(theTotal), newTime(theTotal)
{ }
这将访问向量的末尾,它只有Data.size()
元素,从零开始这是未定义的行为,可能导致问题
这可能不是问题所在,但正如您没有说过错误发生在哪里一样,很难知道。你有失败的程序,你应该能够调试它,并说它在哪里爆炸。。。您有三天的时间学习使用调试器
在你知道你在做什么之前,我建议你停止使用[x]
访问向量,转而使用at(x)
函数,该函数做同样的事情,但检查x
是否为有效索引且不大于向量的大小。如果这样做了,那么在第一个问题上就会出现异常,而不是未定义的行为和堆栈溢出
还有许多其他问题
将include-guard放在文件的顶部,而不是其他标题之后
切勿将使用名称空间
放在标题中的名称空间范围内
你继续这样做:
cdStorage::cdStorage(总计)代码>
那应该做什么?你为什么一直这么做
您应该在构造函数中使用成员初始值设定项,而不是在构造函数体中更改它们:
cdStorage::cdStorage(unsigned int theTotal)
{
newTotal = theTotal;
newTitles.resize(newTotal);
newTime.resize(newTotal);
}
i、 e.改为:
cdStorage::cdStorage(unsigned int theTotal)
: newTotal(theTotal), newTitles(theTotal), newTime(theTotal)
{ }
Data[Data.size()]
正在访问向量Data
之外的数据,这是未定义的行为,因此任何事情都可能发生
另外,我不知道您反复调用cdStorage::cdStorage(total)会有什么想法
可以,但除了创建一个新的(匿名)对象并立即丢弃之外,它什么都不做
您创建的所有cdStorage
s都是使用默认(无参数)构造函数创建的,这使得newTotal
完全未初始化,并且vector
s都是空的。您不能通过事后调用构造函数来修改它们(我怀疑这就是您试图实现的目标)
因为向量是空的,当你说,例如,newTitles[x]=theTitle代码>,您正在访问无效内存,这意味着您的程序同样具有未定义的行为
很难说这些是否是问题的原因,但在继续之前,您可能应该先解决它们
您可能应该阅读您的好书中有关构造函数和实例创建的章节。数据[Data.size()]
正在访问向量数据之外的数据,这是未定义的行为,因此任何事情都可能发生
另外,我不知道您反复调用cdStorage::cdStorage(total)会有什么想法
可以,但除了创建一个新的(匿名)对象并立即丢弃之外,它什么都不做
您创建的所有cdStorage
s都是使用默认(无参数)构造函数创建的,这使得newTotal
完全未初始化,并且vector
s都是空的。您不能通过事后调用构造函数来修改它们(我怀疑这就是您试图实现的目标)
因为向量是空的,当你说,例如,newTitles[x]=theTitle代码>,您正在访问无效内存,这意味着您的程序同样具有未定义的行为
很难说这些是否是问题的原因,但在继续之前,您可能应该先解决它们
您可能应该回顾一下您的好书中有关构造函数和实例创建的章节。堆栈溢出发生在哪里?也要使用intmain
,不要使用名称空间x放置