Having trouble trying to figure out how to output complex data type to a console. My line of code:
cout << C.ValueAt(0) << endl;
gives me error C2679 as follows:
c:\Documents and Settings\csc9\My Documents\231Ex07HMB\231Ex07DHMB.cpp(64): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
Code:
//.cpp file
#include "231Ex07LHMB.h"
#include <conio.h>
#include <string>
#include <iostream>
using namespace std;
struct Auto{
int year;
string make;
string model;
};
void ShowAuto(ostream& sout, Auto a) {
sout << "\tYear: " << a.year << endl;
sout << "\tMake: " << a.make << endl;
sout << "\tModel: " << a.model << endl;
}
ostream& operator<<(ostream& sout, Auto a) {
ShowAuto(sout, a);
return sout;
}
int main()
{
//Puts LList Auto data in C
LList<Auto> C;
//Test empty(): Expect "List is empty."
cout << "Test empty() before put():\n\t";
if(C.empty())
cout << "List is empty.\n\n";
else
cout << "List is not empty.\n\n";
//Datatype Auto, Identifier D
Auto D;
D.year = 1998;
D.make = "Volvo";
D.model = "S70";
//Test for invalid position
cout << "Test put() error message for invalid position:\n";
C.put(-1, D);
C.put(22, D);
//test put()
C.put(0, D);
//Test ValueAt()
cout << "\nTest ValueAt():\n\t";
//**ERROR HERE
cout << C.ValueAt(0) << endl;
//Test empty() after put() - Expect: "List is not empty."
cout << "\nTest empty() after put():\n\t";
if(C.empty())
cout << "List is empty.\n\n";
else
cout << "List is not empty.\n\n";
//Test for erase()
cout << "Test erase():\n";
while(!C.empty())
{
C.erase(0);
}
}
//.h file
#include <iostream>
using namespace std;
#define NULL 0
#define SIZE 256
template <typename T>
class LList {
struct Node {
T Data;
Node *next;
};
public:
//Constructor
LList();
//Destructor
~LList();
//empty()
bool empty() const;
//put()
void put(int pos, T item);
//ValueAt()
void ValueAt(const long pos);
//erase()
void erase(int pos);
//display() operation
void display(ostream & out) const;
private:
int mySize;
T myList[SIZE];
Node * first;
};
//Definition of Display
template<typename T>
void LList<T>::display(ostream & out) const
{
for(int i = 0; i < mySize; i++)
out << myList[i] << " ";
}
//Constructor
template<typename T>
LList<T>::LList()
{
first = NULL;
mySize = 0;
}
//Define Deconstructor
template<typename T>
LList<T>::~LList()
{}
//Define empty()
template<typename T>
bool LList<T>::empty() const
{
return(mySize == 0);
}
//Define put()
template<typename T>
void LList<T>::put(int pos, T item)
{
if(mySize == SIZE)
{
cerr << "\t*** No space for list element -- terminating "
"execution ***\n";
exit(1);
}
if (pos < 0 || pos > mySize)
{
cerr << "\t*** Illegal location to insert -- " << pos
<< ". List unchanged. ***\n";
return;
}
//First shift array to the right to make room for item
for(int i = mySize; i > pos; i--)
myList[i] = myList[i - 1];
//Now insert item at position pos and increase list size
myList[pos] = item;
mySize++;
}
//Define ValueAt
template<typename T>
void LList<T>::ValueAt(const long pos)
{
if(mySize == 0)
{
cerr << "\t*** List is empty ***\n";
return;
}
if(pos < 0 || pos > mySize)
{
cerr << "\t*** Illegal location to retrieve value *** " << pos << ".\n";
return;
}
return myList[0];
}
//Define erase()
template<typename T>
void LList<T>::erase(int pos)
{
if(mySize == 0)
{
cerr << "\t*** List is empty ***\n";
return;
}
if(pos < 0 || pos > mySize)
{
cerr << "Illegal location to delete -- " << pos
<< ". List unchanged ***\n";
return;
}
//Shift array elements left to close the gap
for(int i = pos; i < mySize; i++)
myList[i] = myList[i + 1];
//Decrease list size
mySize--;
}