0

//长话短说,试图做一个媒体库,但我对为什么我无法让这些数据正常工作感到 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
4

2 回答 2

2

Data[Data.size()]正在访问 vector 之外Data,这是未定义的行为,因此任何事情都可能发生。

另外,我不知道您认为重复调用cdStorage::cdStorage(total);会做什么,但它除了创建一个立即被丢弃的新(匿名)对象之外什么也没做。

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

由于向量是空的,当您说 eg 时newTitles[x] = theTitle;,您正在访问无效内存,这意味着您的程序再次具有未定义的行为。

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

您可能应该查看 Fine Book 中关于构造函数和实例创建的章节。

于 2013-05-18T20:33:29.800 回答
1
    Data[Data.size()].setNewName(name);

这访问了向量的末尾,它只有Data.size()元素,从零开始。 这是未定义的行为,可能会导致问题。

这可能不是问题,但是由于您没有说错误发生在哪里,所以很难知道。你有失败的程序,你应该能够调试它并说出它在哪里爆炸......你有三天的时间来学习使用调试器!

在您知道自己在做什么之前,我建议您停止使用[x]来访问向量并切换到使用该at(x)函数,该函数执行相同的操作,但会检查该x索引是否有效且不大于向量的大小。如果你这样做了,那么你会在第一个问题上遇到异常,而不是未定义的行为和堆栈溢出。

还有很多其他问题...

将您的包含保护放在文件的顶部,而不是其他标题之后。

永远不要将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)
{ }
于 2013-05-18T20:25:47.523 回答