How are you guys, this is my first post on this website. I am extremely desperate. I have a C++ program and I am running into a problem:
I have a header file Unsorted.h which contains a class specification of UnsortedType. When I add it to my driver (#include "Unsorted.h") it doesn't recognize the class when I create an object (UnsortedType List;)
I even tried to do the #ifndef Unsorted_H define Unsorted_H, but then I get an error that it can't find beginning of program (main). I'm at a complete stand still, please help. Here are all my files for my program (Including others I have not mentioned since they work perfectly).
StudentType.h
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
|
#include <fstream>
#include "Unsorted.h"
using namespace std;
const int MAX_ITEMS = 4;
class StudentType
{
public:
void ItemType();
void Print(std::ostream&) const;
void Initialize(int number);
void setfirstname(string);
void setlastname(string);
void setramid(int);
string getfirstname();
string getlastname();
int getramid();
private:
int value;
string firstname;
string lastname;
int ramid;
};
|
StudentType.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 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
|
#include <fstream>
#include <iostream>
#include "StudentType.h"
using namespace std;
void StudentType::ItemType()
{
value = 0;
}
void StudentType::Initialize(int number)
{
value = number;
}
void StudentType::Print(std::ostream& out) const
// pre: out has been opened.
// post: value has been sent to the stream out.
{
out << value;
}
/*******************************Above code was prewritten*******************************************
*******************************Code below was self-created******************************************/
// Set objects
void StudentType::setfirstname(string temp)
{
firstname = temp;
}
void StudentType::setlastname(string temp)
{
lastname = temp;
}
void StudentType::setramid(int temp)
{
ramid = temp;
}
// Get objects
string StudentType::getfirstname()
{
return firstname;
}
string StudentType::getlastname()
{
return lastname;
}
int StudentType::getramid()
{
return ramid;
}
|
Unsorted.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifdef StudentType_H
#define StudentType_H
class UnsortedType // declares a class data type
{
public:
void UnsortedType();
void MakeEmpty( );
bool IsFull( ) const;
int GetLength( ) const; // returns length of list
void RetrieveItem( StudentType& item, bool& found );
void InsertItem( StudentType item );
void DeleteItem( StudentType item );
void ResetList( );
void GetNextItem( StudentType& item );
private:
int length;
StudentType info[MAX_ITEMS];
int currentPos;
};
#endif
|
Unsorted.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 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 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#ifdef Unsorted_H
#define Unsorted_H
#include "StudentType.h"
using namespace std;
void UnsortedType::UnsortedType()
{
length = 0;
}
bool UnsortedType::IsFull()
{
return (length == MAX_ITEMS);
}
bool UnsortedType::IsEmpty()
{
return (length == 0);
}
void UnsortedType::InsertItem(ItemType item)
// Post: item is in the list.
{
info[length] = item;
length++;
}
void UnsortedType::RetrieveItem(ItemType& item, bool& found)
// Pre: Key member(s) of item is initialized.
// Post: If found, item's key matches an element's key in the
// list and a copy of that element has been stored in item;
// otherwise, item is unchanged.
{
bool moreToSearch;
int location = 0;
found = false;
moreToSearch = (location < length);
while (moreToSearch && !found)
{
switch (item.ComparedTo(info[location]))
{ case LESS :
case GREATER : location++;
moreToSearch = (location < length);
break;
case EQUAL : found = true;
item = info[location];
break;
}
}
}
void UnsortedType::DeleteItem ( ItemType item )
// Pre: item’s key has been inititalized.
// An element in the list has a key that matches item’s.
// Post: No element in the list has a key that matches item’s.
{
int location = 0 ;
while (item.ComparedTo (info[location]) != EQUAL)
location++;
// move last element into position where item was located
info [location] = info [length - 1 ] ;
length-- ;
}
void PrintList(ofstream& dataFile, UnsortedType list)
// Pre: list has been initialized.
// dataFile is open for writing.
// Post: Each component in list has been written.
// dataFile is still open.
{
int length;
ItemType item;
list.ResetList();
length = list.GetLength();
for (int counter = 1; counter <= length; counter++)
{
list.GetNextItem(item);
item.Print(dataFile);
}
}
void UnsortedType::ResetList ( )
// Pre: List has been inititalized.
// Post: Current position is prior to first element.
{
currentPos = -1 ;
}
void UnsortedType::GetNextItem ( ItemType& item )
// Pre: List has been initialized. Current position is
// defined.
// Element at current position is not last in list.
// Post: Current position is updated to next position.
// item is a copy of element at current position.
{
currentPos++ ;
item = info [currentPos] ;
}
#endif
|
Main Driver (HW3Test.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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include "StudentType.h"
#include "Unsorted.h"
#include <string>
#include <fstream>
using namespace std;
int main()
{
string temp;
int ram;
string title;
string inputFile; // String declarations
string outputFile;
StudentType Student1;
StudentType Student2;
StudentType Student3;
StudentType Student4;
StudentType Student5;
cout << "Enter title for test run: "; // User-defined title
getline(cin, title);
cout << "Enter name of input file. Please include extention: "; // User enters input file (please enter hw3-input.txt)
getline(cin, inputFile);
cout << "Enter name of output file. Please include extention: "; // User enters output file (please enter hw3-output.txt)
getline(cin, outputFile);
cout << endl << endl;
ifstream readFile(inputFile.c_str()); // Opens file stream to read file
if(readFile.is_open())
{
readFile >> temp;
Student1.setfirstname(temp);
readFile >> temp;
Student1.setlastname(temp);
readFile >> ram;
Student1.setramid(ram);
}
UnsortedType List[MAX_ITEMS];
system("pause");
return 0;
}
|
The specific errors I get with this program are:
error C2065: 'UnsortedType' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'List'
error C2065: 'List' : undeclared identifier
The program is supposed to create 5 student objects under StudentType and get their first name, last name, and id number from a text file. Then they are supposed to be inserted in a list (UnsortedType).
I appreciate any help.