I'm not sure where most of my problems that cause this not to compile are coming from. I'm relatively new to this though, so it's probably obvious where I went wrong to others. This part of a project simulating placing cards information in a linked list, but having the code work correctly is something I can do myself. I just cannot figure out why I cannot get it to compile with the makefile, nor where the errors such as "ifstream not declared in this scope" come from.
proj3.cpp
#include<iostream>
#include <fstream>
#include "tsllist.h"
#include "card.h"
using namespace std;
int main()
{
//Declaration of Variables
TSLList<Card> myList;
//int elementOne;
//int elementTwo;
Card myCard;
char elementThree;
ifstream inFile;
inFile.open ("cards.txt");
inFile >> myCard.cardSuit; //Read in 1st element
inFile >> myCard.cardFace; //Read in 2nd elemen
inFile >> elementThree; //Read in 3rd element
while(!inFile.eof()){
if (inFile.eof()) break;
switch(elementThree){
case 'a' :
myList.insertInOrder(myCard);
myList.printAll();
break;
case 'd' :
myList.deleteVal(myCard);
myList.printAll();
break;
case 'D':
myList.deleteAllVal(myCard);
myList.printAll();
break;
default:
break;
}
inFile >> myCard.cardSuit; //Read in 1st element
inFile >> myCard.cardFace; //Read in 2nd elemen
inFile >> elementThree; //Read in 3rd element
}
inFile.close();
return 0;
}
tsllist.h
#ifndef TS_LINKED_LIST_H
#define TS_LINKED_LIST_H
template<class T>
class TSLList {
public:
// Constructor
TSLList() {
head = nullptr;
}
//D Destructor
~TSLList() {
//clearList();
TSLLNode *tmp = head;
while(tmp != nullptr)
{
//will delete each node pointed to by head
//and prints when doing so
head = head->next;
tmp->next = nullptr;
std::cout << "Deleting " << tmp << std::endl;
delete tmp;
tmp = head;
}
}
// prints the info content and address of each node in the list
void printAll() const {
for (TSLLNode *tmp = head; tmp != nullptr; tmp = tmp->next)
std::cout << "->[" << tmp->info << "," << tmp << "]";
std::cout << std::endl;
}
// Inserts node in ascending order
void insertInOrder(T val) {
TSLLNode *prev; // for previous node when parsing
TSLLNode *tmp; //pointer for current node when parsing list
TSLLNode *newNode = new TSLLNode;
newNode->info = val;
newNode->next = nullptr;
if(head==nullptr)
{
head = newNode;
}
else if(newNode->info <= head->info) //
{
newNode->next = head;
head = newNode;
}
else
{
tmp = head;
prev = nullptr;
while(tmp != nullptr && tmp->info < newNode->info)
{
prev = tmp;
tmp = tmp->next;
}
prev->next = newNode;
newNode->next = tmp;
}
// Deletes an occurrence of argument
int deleteVal(T val) {
TSLLNode *tmp;
TSLLNode *prev;
if(!head)
return val;
if(head->info == val)
{
tmp=head->next;
delete head;
head = tmp;
return val;
}
else
{
tmp = head;
while(tmp != nullptr && tmp->info != val)
{
prev = tmp;
tmp = tmp->next;
}
if(tmp)
{
prev->next = tmp->next;
delete tmp;
return val;
}
}
}
// Deletes all occurrences of argument
void deleteAllVal(T val) {
TSLLNode *tmp=head;
TSLLNode *prev=head;
while(tmp!=nullptr)
{
if(tmp->info == val)
{
if(tmp==head)
{
head = tmp->next;
delete tmp;
tmp = head;
}
else
{
prev->next = tmp->next;
delete tmp;
tmp = prev->next;
}
}
else
{
prev = tmp;
tmp = tmp->next;
}
}
return;
}
// Clears the list (deallocates memory)
//void clearList() {
//TSLLNode *tmp = head;
// while(tmp != nullptr)
// {
//will delete each node pointed to by head
//and prints when doing so
// head = head->next;
// tmp->next = nullptr;
// std::cout << "Deleting " << tmp << std::endl;
// delete tmp;
// tmp = head;
// }
// }
private:
//Node stored in linked list
struct TSLLNode {
TSLLNode(T el = T()) {
info = el;
next = nullptr;
}
int info;
TSLLNode *next;
};
TSLLNode *head; // head of the list
};
#endif
card.h
#ifndef CARD_H
#define CARD_H
#include <iostream>
using std::ostream;
// Enumerated type that represents the card suits
enum suit {diamonds, clubs, hearts, spades, joker};
class Card
{
public:
//default constructor - creates Joker card by calling 2-parameter constructor
Card() : Card(-1, joker) {};
//constructor that takes a card's face value (an integer) and its suit
// card face values: Ace=0, 2=1, 3=2, ... Q=11, K=12
Card (int, suit);
// compare and return true if face value of *this is less than that of cd, false otherwise
bool operator<(const Card& cd) const;
// compare and return true if face value of *this is greater than that of cd, false otherwise
bool operator>(const Card& cd) const;
// compare and return true if face value of *this is less than or equal to that of cd, false otherwise
bool operator<=(const Card& cd) const;
// compare and return true if face value of *this is greater than or equal to that of cd, false otherwise
bool operator>=(const Card& cd) const;
// compare and return true if *this has the same face value as cd, false otherwise
bool operator==(const Card& cd) const;
// compare and return true if *this has the a different face value than cd, false otherwise
bool operator!=(const Card& cd) const;
// declare ostream << a friend of this class and overload << operator to display card
friend ostream& operator << (ostream& os, const Card& cd)
{
switch (cd.cardFace)
{
case 10:
os <<"J";
break;
case 11:
os <<"Q";
break;
case 12:
os <<"K" ;
break;
case 0:
os <<"A" ;
break;
case -1:
os <<"j" ;
break;
default:
os << cd.cardFace + 1;
}
switch (cd.cardSuit)
{
case 0:
os << "D";
break;
case 1 :
os << "C";
break;
case 2:
os << "H";
break;
case 3:
os << "S";
break;
case 4:
os << "*";
break;
}
os << "[" << cd.pointValue << "]";
return os;
}
private:
suit cardSuit; // card's suit
int cardFace; // card's face value
int pointValue; // card's point value (from its face)
};
#endif
card.cpp
#include<iostream>
#include"card.h"
using namespace std;
//compare and return true if face value of *this is less than that of cd, false otherwise
bool Card::operator<(const Card& cd)
{
return cardFace<cd.cardFace;
}
// compare and return true if face value of *this is greater than that of cd, false otherwise
bool Card::operator>(const Card& cd)
{
return cardFace>cd.cardFace;
}
// compare and return true if face value of *this is less than or equal to that of cd, false otherwise
bool Card::operator<=(const Card& cd)
{
return cardFace<=cd.cardFace;
}
// compare and return true if face value of *this is greater than or equal to that of cd, false otherwise
bool Card::operator>=(const Card& cd)
{
return cardFace>=cd.cardFace;
}
// compare and return true if *this has the same face value as cd, false otherwise
bool Card::operator==(const Card& cd)
{
return cardFace==cd.cardFace;
}
// compare and return true if *this has the a different face value than cd, false otherwise
bool operator!=(const Card& cd)
{
return cardFace!=cd.cardFace;
}
makefile
## compiler
CXX = g++ -std=c++11
CXXFLAGS = -pedantic -g
proj3: proj3.o card.o
$(CXX) $(CXXFLAGS) proj3.o card.o -o proj3
NumberList.o: card.cpp card.h
$(CXX) $(CXXFLAGS) -c card.cpp
proj3.o: proj3.cpp card.h
$(CXX) $(CXXFLAGS) -c proj3.cpp
clean:
$(RM) proj3 *.o
Aucun commentaire:
Enregistrer un commentaire