ADT's, Overloading, and Classes trouble?

I'm trying to create an address book application, and HAS to use this AddressBook class and header file (unaltered):

const int ENTRY_SZ = 256;

class AddressBook

{

private:

char firstName_[ENTRY_SZ];

char lastName_[ENTRY_SZ];

int streetNum_;

char streetName_[ENTRY_SZ];

char city_[ENTRY_SZ];

char state_[ENTRY_SZ];

int zipCode_;


public:


static int entryCnt;

static char filePath[ENTRY_SZ];


void SetFirstName(const char fName[]);

void SetLastName(const char buff[]);

void SetStreetNum(const char buff[]);

void SetStreetNum(int num);

void SetStreetName(const char buff[]);

void SetCity(const char buff[]);

void SetState(const char buff[]);

void SetZipCode(const char buff[]);

void SetZipCode(int zip);


// Copies some properties into out arguments

void GetFirstName(char buff[], int sz) const;

void GetLastName(char buff[], int sz) const;


void AddEntryFromConsole();

void PrintToConsole();

void AppendToFile();


AddressBook& AddressBook::operator=(const AddressBook& obj);

};


bool operator== (const AddressBook& obj1, const AddressBook& obj2);


The program must use a fixed array of AdressBook objects and size it to 1000 elements. The code should look like this:

const int ADDR_BOOK_SZ = 1000;

AdressBook addrBook[ADDR_BOOK_SZ];


This application should display a console menu that looks like this:

Open an address book file

Add a new address book entry

Print the contents of current address book

Quit

When a) is selected, the program prompts the user to enter a file path for the address book file. It should open the file, read in the 7 lines of data for each entry into the next element of addrBook array. Reading of the file should continue until there are no more entries left in the file.

The b) option should prompt the user to enter the 7 lines for a new address book entry contained within the AddEntryFromConsole() method. This method should first check to see if an address book file has been opened yet through selection a). A function in the main function should also verify that the address book entry does not already exist by using the overloaded “==” operator. If it does not already exist, add that entry to the next element in the addrBook array using the overloaded assignment operator, ‘=’, and also append it to the address book file using the AppendToFile() method.

The c) option should print the current contents of the address book array, which should match the contents of the address book file. It should also print the number of entries currently contained.

Ok, so that's the task, and as for my question I seem to have difficuly refering to things like #include "AddressBook.h", I'll let you see the code I have:

AddressBook.h
******************
#include <string>

using namespace std;

const int ENTRY_SZ = 256;

class AddressBook
{
private:
char firstName_[ENTRY_SZ];
char lastName_[ENTRY_SZ];
int streetNum_;
char streetName_[ENTRY_SZ];
char city_[ENTRY_SZ];
char state_[ENTRY_SZ];
int zipCode_;

public:

static int entryCnt;
static char filePath[ENTRY_SZ];

void SetFirstName(const char fName[]);
void SetLastName(const char buff[]);
void SetStreetNum(const char buff[]);
void SetStreetNum(int num);
void SetStreetName(const char buff[]);
void SetCity(const char buff[]);
void SetState(const char buff[]);
void SetZipCode(const char buff[]);
void SetZipCode(int zip);

// Copies some properties into out arguments
void GetFirstName(char buff[], int sz) const;
void GetLastName(char buff[], int sz) const;

void AddEntryFromConsole();
void PrintToConsole();
void AppendToFile();

AddressBook& AddressBook::operator=(const AddressBook& obj);
};

bool operator== (const AddressBook& obj1, const AddressBook& obj2);

AddressBook.cpp
********************
#include <iostream>
#include "AddressBook.h"

AddressBook::AddressBook()
{
}

AddressBook::~AddressBook()
{
}

void AddressBook::SetFirstName(string name)
{
first_ = name;
}

string AddressBook::GetFirstName()
{
return first_;
}

void AddressBook::SetLastName(string name)
{
last_ = name;
}

string AddressBook::GetLastName()
{
return last_;
}

void AddressBook::SetId(long id)
{
id_ = id;
}

long AddressBook::GetId()
{
return id_;;
}

void AddressBook::Populate()
{
cout << "\nEnter first name: ";
cin >> first_;

cout << "\nEnter last name: ";
cin >> last_;

cout << "\nEnter id: ";
cin >> id_;
}

main.cpp
**********
#include <iostream>
#include "AddressBook.h"

using namespace std;

void FillArray(AddressBook clients[], int sz);
int GetMaxIdx(AddressBook clients[], int sz);

void main()
{
const int ARR_SZ = 30;
AddressBook clients[ARR_SZ];

FillArray(clients, ARR_SZ);

int x;
cin >> x;
}

void FillArray(AddressBook clients[], int sz)
{
for (int i = 0; i < sz; ++i)
{
clients[i].Populate();
}
}

int GetMaxIdx(AddressBook clients[], int sz)
{
int maxidx = 0;

for (int i = 0; i < sz; ++i)
{
if (clients[i].GetLastName() < clients[maxidx].GetLastName())
{
maxidx = i;
}
}

return maxidx;
}

The code is modeling essentially everything I need, I'm just trying to find any major/minor mistakes because it won't compile...

In general, I've also been having trouble refering to headers, like #include "AddressBook.h". ANY help would be greatly appreciated
what errors you are getting, can you post them.

#include "AddressBook.h" means the header should be in the same directory.. check that.
They're all mostly declaration errors, here's the compiler log:

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c AddressBook.cpp -o AddressBook.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

AddressBook.cpp:5: error: definition of implicitly-declared `AddressBook::AddressBook()'

AddressBook.cpp:5: error: declaration of `AddressBook::AddressBook()' throws different exceptions
AddressBook.h:8: error: than previous declaration `AddressBook::AddressBook() throw ()'
AddressBook.cpp:9: error: no `AddressBook::~AddressBook()' member function declared in class `AddressBook'

AddressBook.cpp:13: error: prototype for `void AddressBook::SetFirstName(std::string)' does not match any in class `AddressBook'
AddressBook.h:23: error: candidate is: void AddressBook::SetFirstName(const char*)
AddressBook.cpp: In member function `void AddressBook::SetFirstName(std::string)':
AddressBook.cpp:14: error: `first_' undeclared (first use this function)
AddressBook.cpp:14: error: (Each undeclared identifier is reported only once for each function it appears in.)
AddressBook.cpp: At global scope:
AddressBook.cpp:18: error: prototype for `std::string AddressBook::GetFirstName()' does not match any in class `AddressBook'
AddressBook.h:34: error: candidate is: void AddressBook::GetFirstName(char*, int) const
AddressBook.cpp: In member function `std::string AddressBook::GetFirstName()':
AddressBook.cpp:19: error: `first_' undeclared (first use this function)
AddressBook.cpp: At global scope:
AddressBook.cpp:23: error: prototype for `void AddressBook::SetLastName(std::string)' does not match any in class `AddressBook'
AddressBook.h:24: error: candidate is: void AddressBook::SetLastName(const char*)
AddressBook.cpp: In member function `void AddressBook::SetLastName(std::string)':
AddressBook.cpp:24: error: `last_' undeclared (first use this function)
AddressBook.cpp: At global scope:
AddressBook.cpp:28: error: prototype for `std::string AddressBook::GetLastName()' does not match any in class `AddressBook'
AddressBook.h:35: error: candidate is: void AddressBook::GetLastName(char*, int) const
AddressBook.cpp: In member function `std::string AddressBook::GetLastName()':
AddressBook.cpp:29: error: `last_' undeclared (first use this function)
AddressBook.cpp: At global scope:
AddressBook.cpp:33: error: no `void AddressBook::SetId(long int)' member function declared in class `AddressBook'

AddressBook.cpp: In member function `void AddressBook::SetId(long int)':
AddressBook.cpp:34: error: `id_' undeclared (first use this function)
AddressBook.cpp: At global scope:
AddressBook.cpp:38: error: no `long int AddressBook::GetId()' member function declared in class `AddressBook'
AddressBook.cpp: In member function `long int AddressBook::GetId()':
AddressBook.cpp:39: error: `id_' undeclared (first use this function)

AddressBook.cpp: At global scope:
AddressBook.cpp:43: error: no `void AddressBook::Populate()' member function declared in class `AddressBook'
AddressBook.cpp: In member function `void AddressBook::Populate()':
AddressBook.cpp:45: error: `first_' undeclared (first use this function)

AddressBook.cpp:48: error: `last_' undeclared (first use this function)
AddressBook.cpp:51: error: `id_' undeclared (first use this function)

make.exe: *** [AddressBook.o] Error 1

Execution terminated
you haven't declared the constructor/destructor and defining one.

check your declarations of the functions... like your function SetFirstName takes char and you you defining it with a string type. inside that function first_ is not a member, instead firstName_ is the member of the class.. and thats a char type.

all the errors are of these type only. i correct one of your error rest you try your self.

1
2
3
4
5
6
7
8
9
10
11
12
13
//.h file

string firstName_[ENTRY_SZ];

//... some code
void SetFirstName(const string& fName);


//.cpp file
void SetFirstName(const string& fName)
{
firstName_ = fName;
}


Topic archived. No new replies allowed.