C++ 复制构造函数出错

C++ 复制构造函数出错,c++,memory-management,copy-constructor,C++,Memory Management,Copy Constructor,在尝试为阵列分配新内存时,我的程序一直在染色。通过调用addword函数。任何帮助都会很好 完整代码下载@ #包括//标准IO操纵器库 #包括 #包括//标准输入/输出库 #include//标准字符串库 #include//错误处理库 #包括“pgm2.h” 使用名称空间std; 字典::字典() { wordptr=新值类型[指令容量]; deffptr=新值类型[指令容量]; 使用=0; } 字典::字典(常量字典和其他) { 值类型*wordptr; 值类型*deffptr; wordp

在尝试为阵列分配新内存时,我的程序一直在染色。通过调用addword函数。任何帮助都会很好

完整代码下载@

#包括//标准IO操纵器库
#包括
#包括//标准输入/输出库
#include//标准字符串库
#include//错误处理库
#包括“pgm2.h”
使用名称空间std;
字典::字典()
{
wordptr=新值类型[指令容量];
deffptr=新值类型[指令容量];
使用=0;
}
字典::字典(常量字典和其他)
{
值类型*wordptr;
值类型*deffptr;
wordptr=新值_类型[其他.容量];
deffptr=新值\类型[其他.容量];
used=其他。used;
容量=其他。容量+1;
副本(wordptr,other.wordptr+used,other.wordptr);
副本(deffptr,other.deffptr+已使用,other.deffptr);
}
字典::~字典()
{
删除[]wordptr;
删除[]deffptr;
}
//功能
无效字典::保留(大小类型新容量)
{
值\类型*较大\字;
值\类型*较大\定义;
如果(新容量==容量)
return;//分配的内存大小已经正确
如果(新容量<已使用)
新容量=已使用;
较大的字=新值类型[新容量];
较大的_deff=新值_类型[新容量];
副本(wordptr,wordptr+已用,较大的单词);
副本(deffptr,deffptr+已使用,较大的deff);
删除[]wordptr;
删除[]deffptr;
wordptr=较大的单词;
deffptr=更大的deff;
容量=新的容量;
}
无效字典::addword(常量值类型和单词,常量值类型和定义)
{
如果(已使用==容量)
储备(使用+1);
wordptr[used]=单词;
deffptr[used]=deff;
++使用;
}
void dictionary::containsPat(值\类型patt)
{
i型;;
大小和类型查找;
int=0;
对于(i=0;icout您的复制构造函数正在设置局部变量而不是成员,因此成员指针保持未初始化状态


另外,声明应该使用
const dictionary&
,因为您不应该修改正在复制的词典。

我看不到您在代码中的任何地方重置
容量
…可能会导致您的保留()调用不被调用。

请添加字典类声明。它包含在下载链接中,但请给我一分钟,我也会通过它。您需要一个运算符=。@AlexChamberlain是正确的,请参阅。您的代码也充满了内存泄漏的可能性。
#include <iomanip>    //Standard IO Manipulators Library
#include <algorithm>
#include <iostream>   //Standard input/output library
#include <string>     //Standard string Library
#include <cassert>    //Error handeling Library
#include "pgm2.h"
using namespace std;
    dictionary::dictionary()
    {
        wordptr = new value_type[DICTONARY_CAPACITY];
        deffptr = new value_type[DICTONARY_CAPACITY];
        used = 0;
    }

    dictionary::dictionary(const dictionary& other)
    {
        value_type *wordptr;
        value_type *deffptr;
        wordptr = new value_type[other.capacity];
        deffptr = new value_type[other.capacity];
        used = other.used;
        capacity = other.capacity + 1;
        copy(wordptr, other.wordptr + used, other.wordptr);
        copy(deffptr, other.deffptr + used, other.deffptr);

    }

    dictionary::~dictionary()
    {
        delete [] wordptr;
        delete [] deffptr;
    }

    //Functions
    void dictionary::reserve(size_type new_capacity)
    {
        value_type *larger_word;
        value_type *larger_deff;
        if(new_capacity == capacity)
            return; //There allocated memory is already the correct size
        if(new_capacity < used)
            new_capacity = used;

        larger_word = new value_type[new_capacity];
        larger_deff = new value_type[new_capacity];
        copy(wordptr, wordptr + used, larger_word);
        copy(deffptr, deffptr + used, larger_deff);
        delete [] wordptr;
        delete [] deffptr;
        wordptr = larger_word;
        deffptr = larger_deff;
        capacity = new_capacity;
    }
    void dictionary::addword(const value_type& word, const value_type& deff)
    {
        if(used == capacity)
            reserve(used+1);
        wordptr[used] = word;
        deffptr[used] = deff;
        ++used;
    }

    void dictionary::containsPat (value_type patt)
    {
        size_type i;
        size_type finds;
        int found = 0;
        for(i = 0; i < used; i++)
        {
            finds = wordptr[i].find(patt);

            if (finds != std::string::npos)
            {
                cout << wordptr[i] << "  -  " << deffptr[i] << endl;
                found = 1;
            }
        }
        if (found == 0)
            cout << "*** No words contain the pattern '" << patt << "'" << endl;
    }
    void dictionary::endPat(value_type patt)
    {
        size_type i;
        size_type finds;
        int found = 0;
        size_type pos;

        for(i = 0; i < used; i++)
        {
            finds = wordptr[i].find(patt);

            pos = wordptr[i].length() - patt.length();

            if (finds == pos)
            {
                cout << wordptr[i] << "  -  " << deffptr[i] << endl;
                found = 1;
            }
        }
        if (found == 0)
            cout << "*** No words end with pattern '" << patt << "'" << endl;
    }
    void dictionary::startPat(value_type patt)
    {
        size_type i;
        size_type finds;
        int found = 0;
        for(i = 0; i < used; i++)
        {
            finds = wordptr[i].find(patt);

            if (finds == 0)
            {
                cout << wordptr[i] << "  -  " << deffptr[i] << endl;
                found = 1;
            }
        }
        if (found == 0)
            cout << "*** No words begin with pattern '" << patt << "'" << endl;
    }

    void dictionary::print()
    {
        size_type i;
        cout << "----------------------------------------------------------------------------------------------------------" << endl;
        cout << "---                             Dictionary Words and Meanings                                          ---" << endl;
        cout << "----------------------------------------------------------------------------------------------------------" << endl;
        for(i = 0; i < used; i++)
        {
            cout << wordptr[i] << "   -   " << deffptr[i] << endl;
        }
        cout << "There are " << used << " words in the dictionary!" <<     endl;

    }
#include <iomanip>    //Standard IO Manipulators Library
#include <algorithm>
#include <iostream>   //Standard input/output library
#include <string>     //Standard string Library
#include <cassert>    //Error handeling Library
using namespace std;

class dictionary
{
public:
    typedef string value_type;
    typedef std::size_t size_type;
    static const size_type DICTONARY_CAPACITY = 25;

    //Default Constructor
    dictionary();
    dictionary(dictionary& other);
    //Destructor
    ~dictionary();

    //Functions
    void reserve(size_type new_capacity);
    void addword(const value_type& word, const value_type& deff);
    void startPat (value_type patt);
    void endPat(value_type patt);
    void containsPat(value_type patt);

    void print();

private:
    value_type *wordptr;
    value_type *deffptr;
    size_type used;
    size_type capacity;
};