所以我有三个带有功能的结构体...
struct Song{
std::string songTitle;
std::string songLength;
};
Song* createSong(string title, string length);
void displaySong(Song* s);
void destroySong(Song* s);
struct CD{
std::string cdArtist;
std::string cdTitle;
int cdYear;
int cdRate;
int cdNumSongs;
Song** songs;
};
CD* createCD(std::string artist, std::string title, int year, int rate, int numSongs);
void displayCD(CD* c);
void destroyCD(CD* c);
void addSong(CD* cd, std::string title, std::string length);
struct CDs{
CD** cdArray;
int cdMaxSize=1000;
int cdCurrentSize=0;
};
CDs* createCDs (const char* filename);
void displayCDs (CDs* c);
void destoryCDs (CDs* c);
我已经创建并测试了struct Song的功能以及struct CD的功能。但是,我在实现CD * createCDs(const char *文件名)时遇到了麻烦,该CD * createCDs从文件中取出内容并从这些CD中创建动态存储的数组。
到目前为止,这是我的代码,我检查了悬空的指针,并且当我的代码编译时,它确实很好。它刚运行时,会停止几秒钟并退出。从文本文件中读取数据也已经过测试,该部分可以独立工作。
到目前为止的代码...
CDs* createCDs(const char* filename){
CDs* cds = new CDs;
ifstream inFile(filename);
CD* tempcd;
CD** tempcdArray = new CD*[cds->cdCurrentSize];
CD** tempcdNewArray;
string tempArtist, tempTitle, tempSongTitle, tempSongLength;
int tempYear, tempRate, tempNumSongs;
while(getline(inFile, tempTitle)){ // WHEN IT GRABS NOTHING
cds->cdMaxSize++;
tempcdNewArray = new CD*[cds->cdMaxSize];
for(int i=0; i<cds->cdCurrentSize; i++){
tempcdNewArray[i] = tempcdArray[i];
delete[] tempcdArray;
}
getline(inFile, tempArtist);
inFile>>tempYear;
inFile>>tempRate;
inFile>>tempNumSongs;
inFile.ignore();
tempcd = createCD(tempArtist, tempTitle, tempYear, tempRate, tempNumSongs);
for(int i=0; i<tempNumSongs; i++){
getline(inFile, tempSongLength, ',');
getline(inFile, tempSongTitle);
addSong(tempcd, tempSongTitle, tempSongLength);
}
tempcdNewArray[cds->cdCurrentSize] = tempcd;
tempcdArray = new CD*[cds->cdMaxSize];
for(int i=0; i<cds->cdMaxSize; i++){
tempcdArray[i] = tempcdNewArray[i];
delete tempcdNewArray;
}
delete[] tempcd;
cds->cdCurrentSize++;
}
for(int i=0; i<cds->cdCurrentSize; i++){
cds->cdArray[i] = tempcdArray[i];
}
inFile.close();
return cds;
}
如果您需要知道, 文本文件布局中的一张CD是..
Eternal Tears of Sorrow (cd title)
Saivon Lapsi (cd artist)
2013 (cd year)
7 (cd rating)
13 (number of songs)
1:10,Saivo (length, title)
... (other songs after here)
我尝试跟踪悬空指针的代码,确保我“新建”了要返回的所有内存部分,等等。
答案 0 :(得分:1)
我将尽可能长时间远离new / delete,并且还将将对您的结构体使用的函数移入或至少使它们成为朋友函数。使用标准容器(如std :: vector)进行存储。我用流运算符替换了用于读取和显示对象的函数,以使其更易于使用标准流进行读取/写入。应该以指定的格式读取文件,然后将创建的对象以与原始文件中相同的格式流式传输到std :: cout。
#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
class Song {
std::string m_title;
std::string m_length;
public:
Song() : m_title(), m_length() {}
friend std::ostream& operator<<(std::ostream&, const Song&);
friend std::istream& operator>>(std::istream&, Song&);
};
std::ostream& operator<<(std::ostream& os, const Song& song) {
os << song.m_length << "," << song.m_title;
return os;
}
std::istream& operator>>(std::istream& is, Song& song) {
std::string tmp;
std::getline(is, tmp);
std::string::size_type comma = tmp.find(",");
if(comma != std::string::npos) {
std::copy(tmp.cbegin(), tmp.cbegin()+comma, std::inserter(song.m_length, song.m_length.end()));
std::copy(tmp.cbegin()+comma+1, tmp.cend(), std::inserter(song.m_title, song.m_title.end()));
}
return is;
}
class CD {
std::string m_artist;
std::string m_title;
int m_year;
int m_rate;
std::vector<Song> m_songs;
public:
CD() :
m_artist(),
m_title(),
m_year(),
m_rate(),
m_songs()
{}
size_t size() { return m_songs.size(); }
friend std::ostream& operator<<(std::ostream&, const CD&);
friend std::istream& operator>>(std::istream&, CD&);
};
std::ostream& operator<<(std::ostream& os, const CD& cd) {
os << cd.m_title << "\n" << cd.m_artist << "\n" << cd.m_year << "\n"
<< cd.m_rate << "\n" << cd.m_songs.size() << "\n";
for(const Song& song : cd.m_songs) {
os << song << "\n";
}
return os;
}
std::istream& operator>>(std::istream& is, CD& cd) {
int no_songs;
std::getline(is, cd.m_title);
std::getline(is, cd.m_artist);
is >> cd.m_year;
is >> cd.m_rate;
is >> no_songs;
is.ignore(); // discard rest of line
cd.m_songs.reserve(no_songs);
while(no_songs--) {
Song a_song;
is >> a_song;
cd.m_songs.emplace_back(std::move(a_song));
}
return is;
}
class CDs {
std::vector<CD> m_cds;
public:
CDs() : m_cds() {}
CDs(const std::string& Filename) :
m_cds()
{
std::fstream cdfile(Filename);
cdfile >> *this;
}
size_t size() { return m_cds.size(); }
friend std::ostream& operator<<(std::ostream&, const CDs&);
friend std::istream& operator>>(std::istream&, CDs&);
};
std::ostream& operator<<(std::ostream& os, const CDs& cds) {
for(const CD& cd : cds.m_cds) os << cd;
return os;
}
std::istream& operator>>(std::istream& is, CDs& cds) {
while( !is.eof() ) {
CD a_cd;
is >> a_cd;
if( !is.eof() ) cds.m_cds.emplace_back(a_cd);
}
return is;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
CDs cds;
for(const std::string& file : args) {
std::fstream fs(file);
fs >> cds;
}
std::cerr << "Number of CDs: " << cds.size() << "\n";
std::cout << cds;
return 0;
}