Hello JayGln,
I was going to ask what IDE you are using, but when I followed the link to gibhub I found my answer.
You should never put
using namespace std;
in a header. It is bad enough to use it at all, hopefully in the next class you will learn not to use it, but in a header file it can cause even more problems. Give this a read
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ I think you are OK with the newer files, but I did see this in one of the older files.
Let us start with this example of yours:
1 2 3 4 5 6 7
|
// Node stores data in linked list.
template<class T>
struct Node
{
T info;
Node<T> *linkP;
};
| |
or to look at it another way:
1 2 3 4 5 6
|
// Node stores data in linked list.
struct Node
{
Sale info;
NodeSale*linkP;
};
| |
"info" is an object of class "Sale" to allow you access to its functions.
The other line is trying to create something I do not believe will work. "linkP" just needs to be a pointer of type "Node" so it knows where to go next in the linked list.
Now when you write
std::cout << aVariable;
where "aVariable" is a "bool", "char", "some type of int", "double" or "std::string" or even a C style char array string the "cout" knows how to deal with these types of variables.
When you do
cout << " " << traverseP->info;
"info" is an object of a class and the "cout" does not know what you want to send to the screen or how to format it. In this case you can either replace the "cout" with a function call to a class member function to do the printing to the screen or overload the insertion (<<) operator which sets up something that "cout" can understand. Something I thought of, but did not have a chance to try yet:
cout << " " << traverseP->info.getTransNum();
At least this would return something that "cout" can deal with.
The overloaded operator (<<) I showed you can be changed to print whatever you need. This could be done on one line or several. I believe that making any overloaded operator a friend of the class is the proper way. Just to see what would happen I tried:
1 2 3 4 5 6 7 8 9 10 11 12
|
// Equality operator so that products can be compared.
bool operator ==(const Sale &prod1,
const Sale &prod2);
// Postcondition: Compare as equal if have same id.
// Less than operator so that products can be compared.
bool operator <(const Sale &prod1,
const Sale &prod2);
// Postcondition: Compare as less than if id of first is
// less than id of second.
std::ostream& operator <<(std::ostream& out, const Sale sales); // <--- Added.
| |
This worked, but all three should be made a "friend" of the class if I understand it correctly.
The other thing I noticed with your files. "Sale.h" and "Sale.cpp" if fine and done the way it should be keeping the code separate from the class definition. Files that cause a problem, as an example, "searchList.h" and "searchList.cpp". When you start the header file with:
1 2 3 4 5 6 7
|
template<class T>
class SearchList
{
public:
//constructor
SearchList();
| |
It tends to work best when the functions immediately follow the class definition. This has to do with the use of
template<class T>
. I have found the trying to use two different files is always a problem.
When I set up version of the program I created a file "SearchList.h" and put the class definition along with the class function to make proper use if the
template<class T>
. The way you posted your code I took it to be one file as it should be. I can see how including the "SearchList.cpp" file in "main" brings together both "SearchList.h" and "SearchList.cpp" into one place, but this not the best way to do this.
You wrote:
I have some other things that I have to change as well like changing the 2d vector to dynamic 2d array. |
Unless you have to use a dynamic array I would stay with the 2D vector.
The "LNK 2019 error" is not an easy error to understand, but as I have recently learned, and in simpler terms, it means that you are trying to call a function in one file and the function is defined in another file, but there is no connection between the two files and the compiler does not know about the function that is defined in another file. A lot of the time this is due to misspelled words not matching or the offending file could just be missing a header file.
I know I have done some differently than you have, but when I set up the overloaded (<<) it did not give me any problems. With out seeing the code where you made the changes and how you did it along with what file(s) the errors are occurring in it is challenging to know what to do to fix the problem.
First I would start by putting the functions of "SearchList.cpp" at the end of the "SearchList.h" file and the same for "Queue.cpp" file. Then in "main", I call the file "main" because it contains the "main" function, but it could be called anything, I would use:
1 2 3
|
#include "Sale.h"
#include "Queue.h"
#include "searchList.h"
| |
It has been said the the include file order should make no difference, but sometimes it does.
Another possibility is that putting templated class and functions in different files could be causing a problem.
I was thinking about taking the files from "github" and creating a new solution until I started thinking about some of the files like "SearchList.cpp". Is this file part of the project or just a file? If part of the project it is very likely that this file is compiled before "main". It could also be a reason for the "LNK 2019 error".
BTW I use VS2017 which should be the same or similar to what you are using.
If I have missed anything let me know.
Hope that helps,
Andy