合并两个文件,无效使用非静态数据成员

Combining Two Files, Invalid use of nonstatic data member

本文关键字:无效 数据成员 静态 文件 两个 合并      更新时间:2023-10-16
#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;
}
您需要

void Print() {...}定义移动到DVD类定义中,以便它可以充当成员函数。然后,您不需要为每个成员DVD::前缀。

关于文件:让它工作的最简单方法是将第一个代码片段放入头文件中,第二个是源文件,您可以在其中#include该头。

你也忘记了类定义之后;,我看到了return0;,在这一点上,我停下来进一步看。

您必须将

Print()定义移动到DVD定义中,或者在DVD定义中声明它并在Print()之前添加DVD::前缀。

您不能在该上下文中使用DVD类成员来处理问题,您必须在该原则中重建Print函数以将其指定为类成员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__ */

在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);
}

比在主要.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);
}

所以在主要.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
}