2015-11-16 1 views
0
#include <iostream> 
#include <string> 

class DVD { 
public: 
    char m_strTitle[25]; 
    int m_nYearOfRelease; 
    char m_strGenre[25]; 
    char m_strRentalStatus[50]; 
} 

void Print() 
{ 
    using namespace std; 
    cout << "Title" << DVD::m_strTitle << "YearOfRelease" << DVD::m_nYearOfRelease << "Genre" << DVD::m_strGenre 
     << "RentalStatus" << DVD::m_strRentalStatus << endl; 
} 

Это мой первый файл, который генерирует 5 недопустимого использования нестатических ошибок членов данных, и им не совсем уверен, что это означает, так что если кто-то может мне точку в правильное направление, которое было бы оценено. Я также хочу объединить его с другим файлом, в котором я объявляю 10 типов DVD, как правило, я бы поставил их как один файл, но вопрос, который я должен выполнить для своего задания, просит, чтобы я создал массив из 10 DVD-дисков и заполнил их используя информацию из входного файла, так что вот и входной файл, который я сделал.Объединение двух файлов, неправильное использование элемента нестатических данных

#include <iostream> 
#include <cstring> 

// Set the Info for 10 DVDs 
void SetInfo(const char* strTitle, int nYearOfRelease, const char* strGenre, const char* strRentalStatus) { 
    strncpy(m_strTitle, strTitle, 25); 
    m_nYearOfRelease = nYearOfRelease; 
    strncpy(m_strGenre, strGenre, 25); 
    strncpy(m_strRentalStatus, strRentalStatus, 50); 
} 

int main() { 
    // Declare 10 DVDs 
    DVD cInception; 
    cInception.SetInfo("Inception", 2010, "Action", "In stock"); 

    DVD cFightClub; 
    cFightClub.SetInfo("Fight Club", 1999, "Action/Suspense", "Due back November12th"); 

    DVD cPulpFiction; 
    cPulpFiction.SetInfo("Pulp Fiction", 1994, "Action", "In Stock"); 

    DVD cTheDarkKnight; 
    cTheDarkKnight.SetInfo("The Dark Knight", 2008, "Drama", "In Stock"); 

    DVD cAmericanHustle; 
    cAmericanHustle.SetInfo("American Hustle", 2013, "Drama", "Due back December1st"); 

    DVD cSilverLiningsPlaybook; 
    cSilverLiningsPlaybook.SetInfo("Silver Linings Playbook", 2012, "Drama/Romance", "In Stock"); 

    DVD cTheHungerGames; 
    cTheHungerGames.SetInfo("The Hunger Games", 2012, "Adventure", "Due Back Today at 12pm"); 

    DVD cFurious7; 
    cFurious7.SetInfo("Furious 7", 2015, "Action", "One Left in Stock"); 

    DVD cSavingPrivateRyan; 
    cSavingPrivateRyan.SetInfo("Saving Private Ryan", 1998, "Drama/War", "Discontinued"); 

    DVD cGladiator; 
    cGladiator.SetInfo("Gladiator", 2000, "Action", "In Stock"); 

    // Print out DVD Info 
    cInception.Print(); 
    cFightClub.Print(); 
    cPulpFiction.Print(); 
    cTheDarkKnight.Print(); 
    cAmericanHustle.Print(); 
    cSilverLiningsPlaybook.Print(); 
    cTheHungerGames.Print(); 
    cFurious7.Print(); 
    cSavingPrivateRyan.Print(); 
    cGladiator.Print(); 

    return0; 
} 

ответ

1

Вы должны переместить void Print() {...} определение в определении DVD класса, так что он может выступать в качестве функции члена. Вам не понадобятся DVD:: префиксы для каждого участника.

Что касается файлов: Самый простой способ получить его работа положить кулак фрагмент кода в файл заголовка, второй исходный файл, в котором вы #include этот заголовок.

Вы также забыли ; после определения класса, я вижу return0;, и на этом этапе я останавливаюсь, чтобы посмотреть дальше.

0

Вы должны либо переместить Print() определение в DVD определение, или объявить его внутри DVD определения и добавить префикс DVD:: перед тем Print().

0

Вы не можете использовать DVD членов класса в этом контексте, чтобы иметь дело с вопросом вы должны восстановить свою Print функцию в этом Principe указать его в качестве DVD члена класса то же самое для SetInfo функции.

void DVD::Print() 
{ 
    using namespace std; 
    cout << "Title" << m_strTitle << "YearOfRelease" << m_nYearOfRelease << "Genre" << m_strGenre << 
    "RentalStatus" << m_strRentalStatus << endl; 
} 

Весь код декларации-дефиниции.

#include <iostream> 
#include <string> 

class DVD 
{ 
    public: 
    char m_strTitle[25]; 
    int m_nYearOfRelease; 
    char m_strGenre[25]; 
    char m_strRentalStatus[50]; 
    void Print(); 
    void SetInfo(const char *strTitle, int nYearOfRelease, const char *strGenre, const char *strRentalStatus); 
}; 

void DVD::Print() 
{ 
    using namespace std; 
    cout << "Title" << m_strTitle << "YearOfRelease" << m_nYearOfRelease << "Genre" << m_strGenre << 
    "RentalStatus" << m_strRentalStatus << endl; 
} 

// Set the Info for 10 DVDs 
// Also DVD class member function 
void DVD::SetInfo(const char *strTitle, int nYearOfRelease, const char *strGenre, const char *strRentalStatus) 
{ 
    strncpy(m_strTitle, strTitle, 25); 
    m_nYearOfRelease = nYearOfRelease; 
    strncpy(m_strGenre, strGenre, 25); 
    strncpy(m_strRentalStatus, strRentalStatus, 50); 
} 

int main() 
{ 
    //Declare 10 DVDs 
    DVD cInception; 
    cInception.SetInfo("Inception", 2010, "Action", "In stock"); 

    DVD cFightClub; 
    cFightClub.SetInfo("Fight Club", 1999, "Action/Suspense", "Due back November12th"); 

    DVD cPulpFiction; 
    cPulpFiction.SetInfo("Pulp Fiction", 1994, "Action", "In Stock"); 

    DVD cTheDarkKnight; 
    cTheDarkKnight.SetInfo("The Dark Knight", 2008, "Drama", "In Stock"); 

    DVD cAmericanHustle; 
    cAmericanHustle.SetInfo("American Hustle", 2013, "Drama", "Due back December1st"); 

    DVD cSilverLiningsPlaybook; 
    cSilverLiningsPlaybook.SetInfo("Silver Linings Playbook", 2012, "Drama/Romance", "In Stock"); 

    DVD cTheHungerGames; 
cTheHungerGames.SetInfo("The Hunger Games", 2012, "Adventure", "Due Back Today at 12pm"); 

    DVD cFurious7; 
    cFurious7.SetInfo("Furious 7", 2015, "Action", "One Left in Stock"); 

    DVD cSavingPrivateRyan; 
    cSavingPrivateRyan.SetInfo("Saving Private Ryan", 1998, "Drama/War", "Discontinued"); 

    DVD cGladiator; 
    cGladiator.SetInfo("Gladiator", 2000, "Action", "In Stock"); 

    //Print out DVD Info 

    cInception.Print(); 
    cFightClub.Print(); 
    cPulpFiction.Print(); 
    cTheDarkKnight.Print(); 
    cAmericanHustle.Print(); 
    cSilverLiningsPlaybook.Print(); 
    cTheHungerGames.Print(); 
    cFurious7.Print(); 
    cSavingPrivateRyan.Print(); 
    cGladiator.Print(); 

    return 0; // fix return statement member 
} 

Чтобы создать вашу программу в качестве разных единиц перевода.

Объявление класса должно быть в DVD.h

#ifndef __DVD_H__ 
#define __DVD_H__ 

#include <iostream> 
#include <string> 

class DVD 
{ 
    public: 
    char m_strTitle[25]; 
    int m_nYearOfRelease; 
    char m_strGenre[25]; 
    char m_strRentalStatus[50]; 
    void Print(); 
    void SetInfo(const char *strTitle, int nYearOfRelease, const char *strGenre, const char *strRentalStatus); 
}; 

#endif /* __DVD_H__ */ 

Defenition методов класса DVD в DVD.cpp

#include "DVD.h" 

void DVD::Print() 
{ 
    using namespace std; 
    cout << "Title" << m_strTitle << "YearOfRelease" << m_nYearOfRelease << "Genre" << m_strGenre << 
    "RentalStatus" << m_strRentalStatus << endl; 
} 

// Set the Info for 10 DVDs 
// Also DVD class member function 
void DVD::SetInfo(const char *strTitle, int nYearOfRelease, const char *strGenre, const char *strRentalStatus) 
{ 
    strncpy(m_strTitle, strTitle, 25); 
    m_nYearOfRelease = nYearOfRelease; 
    strncpy(m_strGenre, strGenre, 25); 
    strncpy(m_strRentalStatus, strRentalStatus, 50); 
} 

чем в main.cpp

#include <iostream> 
#include <string> 
#include "DVD.h" 

int main() 
{ 
    //Declare 10 DVDs 
    DVD cInception; 
    cInception.SetInfo("Inception", 2010, "Action", "In stock"); 

    DVD cFightClub; 
    cFightClub.SetInfo("Fight Club", 1999, "Action/Suspense", "Due back November12th"); 

    DVD cPulpFiction; 
    cPulpFiction.SetInfo("Pulp Fiction", 1994, "Action", "In Stock"); 

    DVD cTheDarkKnight; 
    cTheDarkKnight.SetInfo("The Dark Knight", 2008, "Drama", "In Stock"); 

    DVD cAmericanHustle; 
    cAmericanHustle.SetInfo("American Hustle", 2013, "Drama", "Due back December1st"); 

    DVD cSilverLiningsPlaybook; 
    cSilverLiningsPlaybook.SetInfo("Silver Linings Playbook", 2012, "Drama/Romance", "In Stock"); 

    DVD cTheHungerGames; 
cTheHungerGames.SetInfo("The Hunger Games", 2012, "Adventure", "Due Back Today at 12pm"); 

    DVD cFurious7; 
    cFurious7.SetInfo("Furious 7", 2015, "Action", "One Left in Stock"); 

    DVD cSavingPrivateRyan; 
    cSavingPrivateRyan.SetInfo("Saving Private Ryan", 1998, "Drama/War", "Discontinued"); 

    DVD cGladiator; 
    cGladiator.SetInfo("Gladiator", 2000, "Action", "In Stock"); 

    //Print out DVD Info 

    cInception.Print(); 
    cFightClub.Print(); 
    cPulpFiction.Print(); 
    cTheDarkKnight.Print(); 
    cAmericanHustle.Print(); 
    cSilverLiningsPlaybook.Print(); 
    cTheHungerGames.Print(); 
    cFurious7.Print(); 
    cSavingPrivateRyan.Print(); 
    cGladiator.Print(); 

    return 0; // fix return statement member 
} 

работать с файлом мы должны определить новый метод класса DVD

// Defile new class method 
void DVD::SetInfo(std::istream& stream) 
{   
    // Read title 
    stream.getline(m_strTitle, 25); 
    // Read year of release 
    stream >> m_nYearOfRelease 
    // Read genre 
    stream.getline(m_strGenre, 25); 
    // Read rental status 
    stream.getline(m_strRentalStatus, 50); 
} 

поэтому в main.cpp мы получили это

#include <iostream> 
#include <string> 
#include "DVD.h" 

int main() 
{ 
    std::ifstream input_file("filename.txt"); 
    DVD cInception; 
    cInception.SetInfo(input_file); 

    DVD cFightClub; 
    cFightClub.SetInfo(input_file); 

    DVD cPulpFiction; 
    cPulpFiction.SetInfo(input_file); 

    DVD cTheDarkKnight; 
    cTheDarkKnight.SetInfo(input_file); 

    DVD cAmericanHustle; 
    cAmericanHustle.SetInfo(input_file); 

    DVD cSilverLiningsPlaybook; 
    cSilverLiningsPlaybook.SetInfo(input_file); 

    DVD cTheHungerGames; 
    cTheHungerGames.SetInfo(input_file); 

    DVD cFurious7; 
    cFurious7.SetInfo(input_file); 

    DVD cSavingPrivateRyan; 
    cSavingPrivateRyan.SetInfo(input_file); 

    DVD cGladiator; 
    cGladiator.SetInfo(input_file); 

    //Print out DVD Info 

    cInception.Print(); 
    cFightClub.Print(); 
    cPulpFiction.Print(); 
    cTheDarkKnight.Print(); 
    cAmericanHustle.Print(); 
    cSilverLiningsPlaybook.Print(); 
    cTheHungerGames.Print(); 
    cFurious7.Print(); 
    cSavingPrivateRyan.Print(); 
    cGladiator.Print(); 

    return 0; // fix return statement member 
} 
+0

Спасибо, мой код теперь работает и отлично компилируется и отображает то, что я желаю, однако, что это могло означать, когда мой инструктор хочет меня, чтобы проверить его с помощью входной файл, я немного неясен на этом –

+0

@Scott Glendenning: Я тоже не совсем понимаю. Может быть, он хочет загрузить входные данные о DVD-дисках из файла. – Mykola

+0

Как бы это сделать, загрузив входные данные о DVD-дисках из файла? –

Смежные вопросы