Reputation: 1
I've been working on this code for a while trying to test out my Company class with a test bed main. I get all the results I want, however upon finishing the main function the program crashes giving this message: (I apologize but I'm new here and I can't directly post the image yet.) I've been dealing with this error for probably about 4-5 hours spanning over a couple of days and everything I find doesn't seem to work. I feel like understand pointers as a whole better from the looking around I've done but I still can't seem to find what the problem is.
//COMPANY.H
#ifndef _COMPANY_H
#define _COMPANY_H
#include <stddef.h>
#include <fstream>
#include <iomanip>
enum { PHONE_LEN = 10 };
enum { MAX_NAME_LEN = 30 };
#define _TESTING_COMP
#ifdef _TESTING_COMP
#include <iostream>
using namespace std;
#endif
class Company
{
public:
Company();
~Company();
Company &operator =(Company const & c);
bool operator==(const Company & c)const {return (*name == *c.name);}
bool operator!=(const Company & c)const {return (*name != *c.name);}
bool operator<(const Company & c)const {return (*name < *c.name);}
friend istream & operator>>(istream & in, Company & c);
friend ostream & operator<<(ostream & out, const Company & c);
private:
char *name; // allocate memory (use new) - zero-terminated
char phone[PHONE_LEN]; // NOT zero-terminated (be careful!)
};
#endif
This is my header file with the function and data declarations.
//COMPANY.CPP
#include "Company.h"
Company::Company()
{
name = new char [MAX_NAME_LEN];
}
Company::~Company()
{
delete [] name;
}
Company &Company::operator =(Company const & c)
{
for (int i = 0; i < MAX_NAME_LEN; i++)
*(name + i) = *(c.name + i);
for (int i = 0; i < PHONE_LEN; i++)
phone[i] = c.phone[i];
return *this;
}
istream & operator>>(istream & in, Company & c)
{
in >> c.name;
for (int i = 0; i < PHONE_LEN; i++)
in >> c.phone[i];
return in;
}
ostream & operator<<(ostream & out, const Company & c)
{
//because value is a pointer the * is needed to input a value
out << c.name << setiosflags(ios::left) << setw(MAX_NAME_LEN) << "";
for (int i = 0; i < PHONE_LEN; i++)
out << c.phone[i];
out << endl;
return out;
}
#ifdef _TESTING_COMP
void main ()
{
char end;
Company test1, test2;
cin >> test1;
cout << test1;
cin >> test2;
cout << test2;
cout << "testing comp1 < comp2: expecting (0)" << (test1 < test2) << endl;
cout << "testing comp2 < comp1: expecting (1)" << (test2 < test1) << endl;
cout << "testing comp2 == comp1: expecting (0)" << (test2 == test1) << endl;
cout << "testing comp2 != comp1: expecting (1)" << (test2 != test1) << endl;
cout << "set test1 to = test2" << endl;
test1 = test2;
cout << test1;
cout << test2;
cout << "testing comp1 < comp2: expecting (0)" << (test1 < test2) << endl;
cout << "testing comp2 < comp1: expecting (0)" << (test2 < test1) << endl;
cout << "testing comp2 == comp1: expecting (1)" << (test2 == test1) << endl;
cout << "testing comp2 != comp1: expecting (0)" << (test2 != test1) << endl;
test1.~Company();
test2.~Company();
cin >> end;
}
#endif
and this is the cpp file that holds the extra code and the testbed main.
If you guys could help me out with this user error it would be much obliged. Even if you can't thanks for the time. I'm not entirely sure of what I should be looking for so these are the data values at the end of the program
I can post more if you guys need it thank you.
Upvotes: 0
Views: 67
Reputation: 19052
So much boilerplate that is unnecessary. By dynamically allocating name
there's a whole bunch of housekeeping that you're forced to do for no good reason (and is ultimately the cause of your crash). Also, all of your comparison functions will give you surprising results.
Here's your class refactored as cannonical C++:
#include <string>
class Company
{
public:
const std::string& get_name() const { return name; }
const std::string& get_phone() const { return phone; }
private:
std::string name;
std::string phone;
friend istream & operator>>(istream & in, Company & c);
friend ostream & operator<<(ostream & out, const Company & c);
};
bool operator==(const Company& lhs, const Company& rhs) { return lhs.name == rhs.name; }
bool operator!=(const Company& lhs, const Company& rhs) { return !(lhs == rhs); }
bool operator<(const Company& lhs, const Company& rhs) { return lhs.name < rhs.name; }
Upvotes: 1