Array Based List - error in dynamic array creation

时间:2018-09-22 23:06:46

标签: c++ arrays class arraylist

So I have an assignment from my computer science professor to create an array based list on a user defined data type.

The error I'm getting is this:

In file included from Main.cpp:7:0:
AddressBook.h: In constructor ‘addressBookType::addressBookType(int)’:
AddressBook.h:54:37: error: no matching function for call to ‘extPersonType::extPersonType()’
     list = new extPersonType[maxSize];
                                     ^

I made a code similar to this in lab, and it worked fine when dynamically creating an array.

Here is my main.cpp file:

// Name: Tristan Raine
// COSC 2437.002
// Assignment 3
// This program tests an array based list
#include <iostream>
#include "ExtPerson.h"
#include "AddressBook.h"

int main()
{
    cout << endl; 

    addressBookType OnlineAddressBook(3);

    extPersonType object1("Family", "361-421-3845", "7824 Yorktown Blvd.", "Corpus Christi", "Texas", "79234", "Tim",
    "Smith", 12, "December", 2025);

    extPersonType object2("Friend", "361-523-7531", "7842 Beaver Ln", "Dallas", "Texas", "78541", "John",
    "Deer", 9, "September", 2018);

    extPersonType object3("Business Associate", "361-454-2343", "3853 Lucky Rd.", "Las Vegas", "California", "94313", "Jack",
    "Ryan", 23, "March", 2005);

    extPersonType object4("Family", "361-432-9764", "6525 Rabbit St.", "Denver", "Colorado", "54231", "Vanessa",
    "Daniels", 03, "March", 2011);

    OnlineAddressBook.insert(object1);
    OnlineAddressBook.insert(object2);
    OnlineAddressBook.insert(object3);
    OnlineAddressBook.insert(object4);

    OnlineAddressBook.printArray();
}

Here is my AddressBook.h with the functions based on creating an array based list:

#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <iostream>
#include <cassert>
#include "ExtPerson.h"
using namespace std;

class addressBookType
{
    public:
        int seqSearch(extPersonType& item);
        void insert(extPersonType& insertItem);
        void printArray() const;
        void removeAt(int location);
        addressBookType(int size = 100);
        ~addressBookType();
    protected:
        extPersonType *list;
        int length;
        int maxSize;
};

int addressBookType::seqSearch( extPersonType& item) 
{
    int loc;
    bool found = false;
    for (loc = 0; loc < length; loc++)
        if (list[loc].getPhone() == item.getPhone() && list[loc].getIdentity() == item.getIdentity() && 
        list[loc].getFirstName() == item.getFirstName() && list[loc].getLastName() == item.getLastName() && 
        list[loc].getDay() == item.getDay() && list[loc].getMonth() == item.getMonth() && list[loc].getYear() == item.getYear() &&
        list[loc].getStreet() == item.getStreet() && list[loc].getCity() == item.getCity() && list[loc].getState() == item.getState() &&
        list[loc].getZipcode() == item.getZipcode())
        {
            found = true;
            break;
        }
    if (found)
        return loc;
    else
        return -1;
}

addressBookType::addressBookType(int size)
{
    if (size <= 0)
    {
        cerr << "The array size must be positive. Creating "
        << "an array of size 100. " << endl;
        maxSize = 100;
    }
    else
        maxSize = size;
    length = 0;
    list = new extPersonType[maxSize];
    assert(list != NULL);
}

addressBookType::~addressBookType()
{
    delete [] list;
}

void addressBookType::insert(extPersonType& insertItem)
{
    int loc;
    if (length == 0) //list is empty
        list[length++] = insertItem; //insert the item and
    //increment the length
    else if (length == maxSize)
        cerr << "Cannot insert in a full list." << endl;
    else
    {
        loc = seqSearch(insertItem);
        if (loc == -1) //the item to be inserted
            //does not exist in the list
            list[length++] = insertItem;
        else
            cerr << "the item to be inserted is already in "
            << "the list. No duplicates are allowed." << endl;
    }
}

void addressBookType::removeAt(int location)
{
    if(location < 0 || location >= length)
        cout << "The location of the item to be removed is out of range" << endl;
    else
    {
        for(int i = location; i < length - 1; i++)
            list[i] = list[i+1];
        length--;
    }

}

void addressBookType::printArray() const
{
    for (int i = 0; i < length; i++)
    {
        list[i].print();
    }
    cout << endl;

}

#endif

Here is my ExtPerson.h file:

// Header file for extPersonType class which has its' function prototypes and variables
#ifndef EXTPERSON_H
#define EXTPERSON_H
#include <string>
#include "Address.h"
#include "Person.h"
#include "Data.h"
using namespace std;

// extPersonType is derived from all of these classes
class extPersonType: public addressType, public personType, public dataType
{
    private:
        string phone;
        string ident;
        int *ptr;
    public:
        // Constructor, inline member function
        extPersonType(string i, string ph, string s, string c, string st, string z, string fn,
        string ln, int d, string m, int y) : addressType(s, c, st, z), personType(fn, ln), dataType(d, m, y)
        {
            phone = ph;
            ident = i;
        }

        // setPhone function prototype
        void setPhone(string ph);

        // setIdentity function prototype
        void setIdentity(string i);

        // print function prototype
        void print();

        // getPhone function prototype
        string getPhone()
        {
            return phone;
        }

        // getIdentity function prototype
        string getIdentity()
        {
            return ident;
        }

        // Assign function
        void operator=(extPersonType &right)
        {
            phone = right.phone;
            ident = right.ident;
            firstname = right.firstname;
            lastname = right.lastname;
            street = right.street;
            city = right.city;
            state = right.state;
            zipcode = right.zipcode;
            month = right.month;
            day = right.day;
            year = right.year;
        }

        // Checks if objects are equal
        bool operator==(extPersonType &right)
        {
            return(phone == right.phone && ident == right.ident && firstname == right.firstname && lastname == right.lastname
            && street == right.street && city == right.city && state == right.state && zipcode == right.zipcode &&
            month == right.month && day == right.day && year == right.year);
        }

        // Copy constructor
        extPersonType (const extPersonType &obj) : addressType(obj), personType(obj), dataType(obj)
        {
            cout << "Copy constructor active." << endl;
            phone = obj.phone;
            ident = obj.ident;
        }
};

#endif

Here is my ExtPerson.cpp file:

// Implementation file for the extPersonType class, with its' functions
#include <iostream>
#include "ExtPerson.h"
#include "Address.h"
#include "Person.h"
#include "Data.h"

// sets phone to ph
void extPersonType::setPhone(string ph)
{
    phone = ph;
}


// sets ident to i
void extPersonType::setIdentity(string i)   
{
    ident = i;
}


// Prints first name, last name, street, city, state, zipcode, month,
// day, year, phone, and identity       
void extPersonType::print()
{
    cout << "The person's info is listed below:" << endl;
    cout << "First name: " << firstname << endl;
    cout << "Last name: " << lastname << endl;
    cout << "Street: " << street << endl;
    cout << "City: " << city << endl;
    cout << "State: " << state << endl;
    cout << "Zipcode: " << zipcode << endl;
    cout << "Date: " << month << " " << day << ", " << year << endl;
    cout << "Phone number: " << phone << endl;
    cout << "Person type: " << ident << endl;
}

Here is my Person.h file:

// Header file for personType class, with its' prototypes and variables
#ifndef PERSON_H
#define PERSON_H
#include <string>
using namespace std;

class personType
{
    protected:
        string firstname;
        string lastname;
    public:
        // personType constructor
        personType(string fn, string ln);

        // copy constructor
        personType(const personType &obj)
        {
            firstname = obj.firstname;
            lastname = obj.lastname;
        }

        // setFirstName prototype
        void setFirstName(string fn);

        // setLastName prototype
        void setLastName(string ln);

        // getFirstName function prototype
        string getFirstName()
        {
            return firstname;
        }

        // getLastname function prototype
        string getLastName()
        {
            return lastname;
        }

        // print prototype
        void print();
};

#endif

Here is my Person.cpp file:

// Implementation file for personType class, with its' function definitions
#include <iostream>
#include "Person.h"

// personType constructor
personType::personType(string fn, string ln)
{
    firstname = fn;
    lastname = ln;
}

// sets firstname to fn 
void personType::setFirstName(string fn)
{
    firstname = fn;
}

// sets lastname to ln  
void personType::setLastName(string ln)
{
    lastname = ln;
}

// prints the first and last name       
void personType::print()
{
    cout << "First name: " << firstname << endl;
    cout << "Last name: " << lastname << endl;
}

Here is my Data.h:

// Header file for dataType class, with its' prototypes and variables
#ifndef DATA_H
#define DATA_H
#include <string>
using namespace std;

class dataType
{
    protected:
        int day;
        string month;
        int year;
    public:
        // dataType constructor
        dataType(int d, string m, int y);

        // copy constructor
        dataType(const dataType &obj)
        {
            day = obj.day;
            month = obj.month;
            year = obj.year;
        }

        // setDay prototype
        void setDay(int d);

        // setMonth prototype
        void setMonth(string m);

        // setYear prototype
        void setYear(int y);

        // getMonth function prototype
        string getMonth()
        {
            return month;
        }

        // getDay function prototype
        int getDay()
        {
            return day;
        }

        // getYear function prototype
        int getYear()
        {
            return year;
        }

        // print prototype
        void print();
};

#endif

Here is my Data.cpp file:

// Implementation file for dataType class, with its' function definitons
#include <iostream>
#include "Data.h"

// dataType constructor
dataType::dataType(int d, string m, int y)
{
    day = d;
    month = m;
    year = y;
}

// sets day to d
void dataType::setDay(int d)
{
    day = d;
}

// sets month to m  
void dataType::setMonth(string m)
{
    month = m;
}

// sets year to y   
void dataType::setYear(int y)
{
    year = y;
}

// prints the date  
void dataType::print()
{
    cout << "Date: " << month << " " << day << ", " << year << endl;
}

Here is my Address.h file:

    // Header file for addressType class, with its' prototypes and variables
    #ifndef ADDRESS_H
    #define ADDRESS_H
    #include <string>
    using namespace std;

    class addressType
    {
        protected:
            string street;
            string city;
            string state;
            string zipcode;
        public:
            // addressType constructor
            addressType(string s, string c, string st, string z);

            // copy constructor
            addressType(const addressType &obj)
            {
                street = obj.street;
                city = obj.city;
                state = obj.state;
                zipcode = obj.zipcode;
            }

            // setStreet function prototype
            void setStreet(string s);

            // setCity function prototype
            void setCity(string c);

            // setState function prototype
            void setState(string st);

            // setZipcode function prototype
            void setZipcode(string z);

            // getStreet function prototype
            string getStreet()
            {
                return street;
            }

            // getCity function prototype
            string getCity()
            {
                return city;
            }

            // getState function prototype
            string getState()
            {
                return state;
            }

            // getZipcode function prototype
            string getZipcode()
            {
                return zipcode;
            }

            // print function prototype
            void print();
    };

    #endif

Here is my Address.cpp file:

// Implementation file for addressType class, with its function definitions
#include <iostream>
#include "Address.h"

// addressType constructor
addressType::addressType(string s, string c, string st, string z)
{
    street = s;
    city = c;
    state = st;
    zipcode = z;
}

// sets street variable to s    
void addressType::setStreet(string s)
{
    street = s;
}

// sets city to c   
void addressType::setCity(string c)
{
    city = c;
}

// sets state to st 
void addressType::setState(string st)
{
    state = st;
}

// sets zipcode to z    
void addressType::setZipcode(string z)
{
    zipcode = z;
}

// prints street, city, state, and zipcode      
void addressType::print()
{
    cout << "Street: " << street << endl;
    cout << "City: " << city << endl;
    cout << "State: " << state << endl;
    cout << "Zipcode: " << zipcode << endl;
}

Any ideas on how I'm getting this error? Thanks in advance.

1 个答案:

答案 0 :(得分:0)

new extPersonType[maxSize]创建一个包含maxSize默认构造的extPersonType对象的数组。问题是extPersonType没有默认的构造函数。

您需要在extPersonType中定义默认构造函数,或者分配未初始化的存储,然后在插入extPersonType对象时使用placement-new初始化对象。