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

标签 c++ non-static datamember

#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;,此时,我要停下来进一步查看。

关于c++ - 合并两个文件,非静态数据成员的使用无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33745720/

相关文章:

c++ - 从C++中的2个不同线程访问属于同一对象的不同数据成员

c++ - 带链接字符串的 Ncurses 菜单

c++将数据源函数作为参数传递

c++ - 从静态函数调用非静态变量

java - 无法从静态调用非静态方法 - 同一个类

c++ - 从 Vector 生成类数据成员

asp.net - WCF:是否有一个属性可以使 OperationContract 中的参数成为必需的?

c++ - 从 C++ 到 Perl

c++ - 如何使用 STL 在 C++ 中创建游戏保存文件格式

c++ - 适用于 Linux 的类似 Windows 的消息框 - 这个 C++/Motif 实现是否可行?