Hey all,
I'm writing a program that is an address book using an AVL Node structure (which was provided). I defined the contact class as the ItemType of the data to be inserted in each node. Initially I encountered a Segmentation fault that occured during the assignment from the AVLClass object returned from Read_DB() to the AVLClass object AVLTreeContacts. AVLClass has an overloaded assignment operator that copies all the nodes of the tree.
// Original screen output at runtime before writing overloaded = operator for contact class
Database successfully read
DEBUG: AVLClass overloaded = operator called
DEBUG: AVLClass CopyTree called
Segmentation fault
I figured this was because I needed an overloaded assignment operator for the contact class so I wrote one. Now i'm getting a compile error that i can't figure out. I've tried adding the "#include" and "using namespace ..." statements to make sure it can see it, but still can't figure it out. 2 Questions:
1) Is there a problem with my overloaded assignment operator? Can you copy a vector of vectors with the statement I have for the Vec_affs.
2) Any ideas on the compiler error? If I comment out my overloaded operator, the program compiles, but then i get the segmentation fault when the program tries to copy one node containing a contact class object to another node containing a contact class object
bstnode.o(.text+0x10c): In function `BSTNodeClass::GetInfo(ns_1::contact&) const':
bstnode.cpp:25: undefined reference to `ns_1::contact::operator=(ns_1::contact const&)'
collect2: ld returned 1 exit status
// FROM main.cpp
int main ()
{
// DECLARE OBJECTS & VARIABLES
AVLClass AVLTreeContacts;
// INITIAL READ OF DB FILE
//Read_DB( );
AVLTreeContacts = Read_DB( );
//AVLTreeContacts.Print();
Main_Menu();
return 0;
}
// FROM contact.h/.cpp
#include <cstdlib> // provides size_t
#include <string>
#include <vector>
#include <fstream>
using namespace std;
namespace ns_1
{
class contact
{
public:
// TYPEDEFS & MEMBER CONSTANTS
std::vector<string> fields; // only works if "using namespace std" exists, what's the problem?
std::vector<vector<string> > Vec_affs; // affiliate = name, phone, email
// CONSTRUCTOR
contact( ) { ; };
// MODIFICATION MEMBER FUNCTIONS
// CONSTANT MEMBER FUNCTIONS
contact& contact::operator=(const contact& record);
// NONMEMBER FUNCTIONS
};
}
namespace ns_1
{
contact& contact::operator=(const contact& record)
{
#ifdef DEBUG
cout << "DEBUG: Contact class overloaded = operator called " << endl;
#endif
if (this != &record) {
// Clear memory for all variables in Contact class
fields.clear();
Vec_affs.clear();
// copy vector (primary contact info) field values
fields(record.fields);
Vec_affs(record.Vec_affs);
//for (int i = 0; i < record.fields.size() ; ++i)
// this->fields.at(i) = record->fields.at(i); //- incorrect - need push_back
// copy 2D vector (affiliate) field values
//for (int a = 0; a < record.Vec_affs.size(); ++a) { // incorrect - need push_back
// this->Vec_aff
// for (int b = 0; a < record.Vec_affs.at(a).size(); ++b)
// this->Vec
}
return *this;
}
}
//FROM bstnode.h/cpp
class BSTNodeClass
{
protected:
ItemType Info;
BSTNodeClass * Left, * Right;
public:
BSTNodeClass(const ItemType & Item, BSTNodeClass * LeftPtr = NULL,
BSTNodeClass * RightPtr = NULL):
Info(Item), Left(LeftPtr), Right(RightPtr)
{
};
void GetInfo(ItemType & TheInfo) const;
friend class BSTClass;
friend class AVLClass;
};
typedef BSTNodeClass * BSTNodePtr;
void BSTNodeClass::GetInfo(ItemType & TheInfo) const //ItemType
{
TheInfo = Info; // assumes assignment works on this type
}
NOTE - The AVLClass calls functions in the bstnode and bstree files to do some of the heavy lifting.
Thanks for any and all help!!