对象类的数组列表不会初始化

ArrayList of an object class won't initialize

本文关键字:初始化 列表 数组 对象      更新时间:2023-10-16

我正在制作一个类的HomeworkDatabase工作与我所做的数组列表。我的代码中可能有很多错误,因为我还是一个初学者。我只是不明白为什么它不在main中初始化。下面是我的代码:

HomeworkDatabase.h:

#ifdef HomeworkDatabase_H
#define HomeworkDatabase_H
class HomeworkDatabase
{
        private:
                string className;
                string homework;
                int dueDate;
       public:
             HomeworkDatabase(string className, string homework, int dueDate);
                HomeworkDatabase();
                string getClass();
                string getHomework();
                int getDueDate();
};
#endif

HomeworkDatabase.cpp:

#include "HomeworkDatabase.h"
#include <iostream>
#include <string>
using namespace std;
HomeworkDatabase::HomeworkDatabase( string className, string homework, int    dueDate)
{
        this->className = className;
        this->homework = homework;
        this->dueDate = dueDate;
}
HomeworkDatabase::HomeworkDatabase()
{
        this->dueDate = NULL;
}
string getClass::getClass()
{
        return className;
}
string getHomework::getHomework()
{
        return homework;
}
int getDueDate::getDueDate()
{
        return dueDate;

HomeworkArrayList.cpp:

#include <iostream>
#include <string>
#include "HomeworkDatabase.h"
using namespace std;
template <class HomeworkDatabase>
class HomeworkArrayList
{
    private:
            int maxSize;
            int curr;
            int listSize;
            HomeworkDatabase* homework;
    public:
            HomeworkArrayList(int size)
            {
                    maxSize = size;
                    listSize = 0;
                    curr = 0;
                    homework = new HomeworkDatabase[size];
            }
            ~HomeworkArrayList()
            {
                    delete [] homework;
            }
            void append(const HomeworkDatabase& object )
            {
                    homework[listSize] = object;
                    listSize++;
            }
            void frontOfList()
            {
                    curr = 0;
            }
            void backOfList()
            {
                    curr = listSize;
            }
            void prev()
            {
                    curr--;
            }
            void next()
            {
                    curr++;
            }
            int getSize()
            {
                    return listSize;
            }
 }

HomeworkMain.cpp:

#include <iostream>
#include <string>
#include "HomeworkArrayList.cpp"
#include "HomeworkDatabase.h"
using namespace std;
int main()
{
        HomeworkArrayList<HomeworkDatabase> today = HomeworkArrayList<HomeworkDatabase>(50);
        HomeworkDatabase one("Intro to Programming","Read Chapter 1", 12);
        return 0;
}

对于main,我得到了错误:

HomeworkMain.cpp:11:9: error: ‘HomeworkDatabase’ was not declared in this scope
HomeworkMain.cpp:10:43: error: template argument 1 is invalid

HomeworkDatabase.h中包含保护错误:

#ifdef HomeworkDatabase_H

必须是:

#ifndef HomeworkDatabase_H

还有其他一些错误,一旦包含保护被修复,这些错误都将变得可见。例如,您使用string而不使用#include <string>std::