c++ - 我不断收到堆栈溢出错误,并尝试修复此问题 3 天。我正在为 CD 创建一个媒体库

标签 c++ vector stack overflow

<分区>

//长话短说,尝试做一个媒体库,但我 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 <<
            "*******************" << "\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");
    }
  }
}

//这是我的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()] 正在访问 vector Data 外部,这是未定义的行为,所以任何事情都有可能发生。

此外,我不知道您怎么想,反复调用 cdStorage::cdStorage(total); 确实如此,但它除了立即创建一个新的(匿名)对象外什么也没做扔掉了。

您创建的所有 cdStorage 都是使用默认(无参数)构造函数创建的,这使得 newTotal 完全未初始化,vector s 都是空的。您不能通过事后调用构造函数来修改它们(我怀疑这就是您想要完成的)。

因为 vector 是空的,当你说例如newTitles[x] = theTitle;,您正在访问无效内存,这意味着您的程序再次具有未定义的行为。

很难说这些是否是您问题的原因,但您可能应该先解决它们,然后再继续。

您可能应该复习一下 Fine Book 中有关构造函数和实例创建的章节。

关于c++ - 我不断收到堆栈溢出错误,并尝试修复此问题 3 天。我正在为 CD 创建一个媒体库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16628431/

相关文章:

Java Applet - For 循环递减组件堆栈

java - LinkedList与堆栈

c++ - Arduino UDP 错误 WiFiUdp.cpp

c++ - OO编程问题: Global Object

c++ - 运行 gcc -gcodeview

c++ - 如何创建以不同对象的模板容器为参数的模板类

c++ - 什么是NULL,需要声明吗?

vector - 如何从Openlayers 3的矢量层获取要素

java - 意外错误

python - ast 模块使用什么类型的树遍历?