Interface will not acknowledge other files.

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.
Last edited on
listInterface
ListInterface

Spot the difference!
While yes, that is a mistake I made, I corrected it and the error continues unchanged.
lordfireguy3 wrote:
While yes, that is a mistake I made, I corrected it and the error continues unchanged.

No, I doubt that.



Here's your code as a single file. It compiles and starts, anyway.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <list>
#include <string>
using namespace std;

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;
};


template<class ItemType>
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);
};

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;
}


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;
	}
}
Last edited on
I don't know why it is happening, but it is. Your file works, however mine still has the same three errors whenever it is split into multiple files, even after rebuilding, the error remains even after I fixed the typo. I'll integrate them all into one file, but I am 100% certain something about the multiple files is screwing up.
Last edited on
Template implementations (definitions) can't be separated from the declarations.

https://isocpp.org/wiki/faq/templates
https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
Last edited on
Topic archived. No new replies allowed.