我正在使用我制作的ArrayList创建一类HomeworkDatabase。我的代码中可能存在很多错误,因为我还是初学者。我只是不明白为什么它没有在主要初始化。这是我的代码:
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;
}
对于主要我得到错误:
HomeworkMain.cpp:11:9: error: ‘HomeworkDatabase’ was not declared in this scope
HomeworkMain.cpp:10:43: error: template argument 1 is invalid
答案 0 :(得分:0)
HomeworkDatabase.h
中的包含守卫发生错误:
#ifdef HomeworkDatabase_H
必须是:
#ifndef HomeworkDatabase_H
还有一些其他错误,一旦修复了包含警卫,这些错误都会变得可见。例如,您使用string
而不使用#include <string>
和std::
。