对象类的数组列表不会初始化
ArrayList of an object class won't initialize
我正在制作一个类的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::
。
相关文章:
- C++类 - 初始化列表 - 递归 - 按值传递
- 在初始化列表之外手动调用基类的构造函数
- C++:带有大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?
- std::map与谓词与初始化列表
- 类内初始化与构造函数初始化列表的顺序
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- C++初始化列表与分配值
- C++初始化列表中的向量集大小或调整大小
- 在构造函数初始化列表中使用 std::variant
- emplace_back初始化列表错误,当初始化列表在独立变量上工作时
- 解释了构造函数成员初始化列表
- 使用初始化列表时如何获取私有数据?
- 用初始化列表和超类构造函数声明子类构造函数的正确方式
- 如何在成员初始化列表中声明共享指针
- 庞大的初始化列表,如何修复"fatal error C1060: compiler is out of heap space"
- 我可以检查初始化列表中设置的构造函数主体中的变量吗
- 使用整数初始化列表初始化长双精度的向量
- 是否可以在C++中使用初始化列表设置数组的特定成员?