I've recently been working on a lab based on templates and using lists, and have been having specific trouble with a line of code that seems to not acknowledge the template file whatsoever, and as such gives three errors, all on the same line of code.
'listInterface' identifier not found
syntax error: missing ',' before '<'
'listInterface': base class undefined
This program is meant to have a name and gift list for each name attached to one another, however I can't even test if my final product works until this is solved. An example is even provided for me for this specific line, and nothing seems off about it whatsoever, I even went letter by letter to prove it.
mainList.h (The errors are where I indicate in comments on this file.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef MAINLIST_H
#define NICELIST_H
#include <list>
#include "listInterface.h"
using namespace std;
template<class ItemType>
//vvv Down here is the error, the line directly under this. vvv
class mainList : public listInterface<ItemType>
{
private:
std::string named;
list<ItemType> gifts;
std::string theGifts = ",";
public:
void setName(std::string name);
std::string getName() const;
void addGift(ItemType gift);
std::string convert(std::string theGifts);
};
#endif
|
listInterface.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#ifndef LISTINTERFACE_H
#define LISTINTERFACE_H
#include <string>
template<class ItemType>
class listInterface
{
public:
virtual void setName(std::string name) = 0;
virtual std::string getName() const = 0;
virtual void addGift(ItemType gift) = 0;
virtual std::string convert(std::string theGifts) = 0;
};
#endif
|
And while I'm confident the main file and mainlist CPP file have little to do with this error, I will include the anyway.
mainList.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include "mainList.h"
#include <string>
template<class ItemType>
void mainList<ItemType>::setName(std::string name)
{
named = name;
}
template<class ItemType>
std::string mainList<ItemType>::getName() const
{
return named;
}
template<class ItemType>
void mainList<ItemType>::addGift(ItemType gift)
{
gifts.push_back(gift);
}
template<class ItemType>
std::string mainList<ItemType>::convert(string theGifts) {
std::string result;
for (typename list<ItemType>::iterator i = gifts.begin(); i != gifts.end(); i++)
{
if (!result.empty()) {
result += theGifts;
}
result += ItemType(*i);
}
return result;
}
|
giftlist.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include "pch.h"
#include <iostream>
#include <string>
#include "mainList.h"
using namespace std;
int main()
{
list<mainList<string>> lists;
string lName, gName;
std::cout << "NAME: ";
getline(cin, lName);
while (!lName.empty())
{
mainList<string> niceList;
niceList.setName(lName);
cout << "Give " << lName << " a: " << endl;
do {
getline(cin, gName);
niceList.addGift(gName);
} while (!gName.empty());
lists.push_back(niceList);
cout << "NAME: ";
getline(cin, lName);
}
for (auto aList : lists) {
cout << aList.getName() << " " << aList.convert(" ") << endl;
}
}
|
Edit: Updated the listInterface to fix the unintentional capitalization, however the errors remain unchanged.