没有合适的默认构造函数可用,尽管有默认构造函数

No Appropriate Default Constructor Available despite default constructor made?

本文关键字:构造函数 默认      更新时间:2023-10-16

试图让我自己的Map结构来存储我自己创建的'Strings ',经过8个小时左右的时间,最终得到了只有几个编译器错误(其中六个)。我花了过去的一小时四十分钟在网上搜索答案,却发现人们忘记了默认构造函数,并试图在我自己的程序中混淆它们。由于我不确定问题在哪里,我很抱歉发布了所有这些代码…我把我认为最相关的文件放在首位;我认为只有前三点是必要的。错误是

"SubdomainPart' : No appropriate default constructor available" for lines 12 and 20 of the Map.h file.

Map.h

// Map.h - Map template class declaration
// Written by -----
#pragma once
template<typename KEY_TYPE, typename VALUE_TYPE>
struct Map
{
public:
    // Default / initial constructor hybrid
    Map(int initialCapacity = 10)
    {
        Size = 0;
        Capacity = initialCapacity;
        Key;
        MappedValue;
        //Allocate the C-Array elements using HEAP
        Data = new VALUE_TYPE[Capacity];
    }
    struct iterator
    {
        KEY_TYPE * current;
        KEY_TYPE * prev;
        KEY_TYPE * next;
        iterator operator ++ ()
        {
            iterator it = this;
            iterator itNext = it.next;
            it.next = itNext.next; // pushes iterator forward.
            it.prev = it.current;
            it.current = it.next;
        }
        iterator operator -- ()
        {
            iterator it = this;
            iterator itPrev = it.prev;
            it.prev = itPrev.prev; // pushes iterator backward.
            it.next = it.current;
            it.current = it.prev;
        }
    };

    Map(const Map& copyFrom)
    {
    // Necessary to prevent the delete[] Data; statement in the assignment operator from 
    // freezing because Data has some garbage address in it.
        Data = NULL;
        *this = copyFrom; //'this' points to the current instance of the object. (in this case, 'Map')
    }
    // Destructor: MUST HAVE because we allocate memory
    ~Map()
    {
        delete[] Data;
    }
    Map& operator = (const Map& copyFrom)
    {
        // 0) delete the old one!
        delete[] Data;
        // 1) copy Size and Capacity
        Size = copyFrom.Size;
        Capacity = copyFrom.Capacity;
        // 2) Allocate Memory
        Map* Data = new Map[Capacity];
        // 3) Copy the Map Elements
        for(int i = 0; i<Size; i++)
            Data[i] = copyFrom.Data[i];
        return *this;
    }
    // Index Operator
    VALUE_TYPE& operator[] (KEY_TYPE key) const
    {
        return Data[key];
    }
    // Accessor functions: read-only access to Size and Capacity
    int GetSize() const //const does not modify ANY data members of the class (size, capacity, or data)
    {
        return Size;
    }
    int GetCapacity() const
    {
        return Capacity;
    }
    void PushBack(const VALUE_TYPE& newElement) //adds value to end of Map as default
    {
        if(Size >= Capacity)
            increaseCapacity(2 * Capacity);
        Data[Size] = newElement;
        Size++; // increases size of the array so it can be used later.
    }
    // Overloaded Add function, inserts a value at specified index, calls in "Insert" to do so.
    void Add(const VALUE_TYPE& newElement, int index)
    {
        if( (index<0) || (index > Size))
        {
            throw ("Index to insert is out of range");
        }
        //Make sure there's space!
        if (Size >= Capacity)
            increaseCapacity(2*Capacity); //increase size of array if too small!
        Insert(index, newElement);  
    }
    void Remove(int index) // index = index to be removed.
    {
        // Make sure it's inside the bounds
        if( (index<0) || (index > Size))
        {
            throw ("Index to Remove is out of range.");
        }
        // it's going to remove the unneeded space by having its capacity one above the Size. 
        Map* new_Data = new Map[Size];
        //Copy data onto new pointer section.
        for(int x = 0; x<Size; x++)
            new_Data[x] = Data[x];
        delete[] Data; //deallocates old memory and uneeded capacity slots.
        for(int x = index; x < (Size - 1); x++) //removes the value at index 'index.' Now Data has a capacity of the amount of slots used and one more for a NULL value.
            new_Data[x] = new_Data[x+1];
        Data = new_Data;
        Data[Size-1] = NULL;
        Size--;
    }
    void increaseCapacity(int new_capacity)
    {
        if(new_capacity>Capacity) 
        {
            if(new_capacity> 2* Capacity)
                Capacity = new_capacity;
        else
            Capacity *= 2;
        //create Map with a new capacity!
        Map* new_Map = new Map[Capacity];
        for(int x = 0; x<Size; x++)
        {
            new_Map[x] = Data[x];
        }
        //clear out old memory
        delete[] Data;
        //set data pointer to the new Map
        Data = new_Map;
        }
    }
    KEY_TYPE * Key; // Used to identify mapped values.
    VALUE_TYPE MappedValue; // The value actually contained.
private:
    int Size; // The count of actual C-Array elements used
    int Capacity; // The count of C-array elements allocated
    // The encapsulated C-array
    VALUE_TYPE * Data; // pointer of type 'DATA_TYPE' called data (will be name of our array).
    void Insert(const int index, const VALUE_TYPE& insertValue)
    {
        if( (index<0) || (index > Size))
        {
            throw out_of_range ("Index to insert is out of range");
        }
    //Time to shuffle the array down!
    for(int x = Size; x>index; x--)
    {
        Data[x] = Data[x-1];
    }
    //Insert the new item at index 'Index!'
    Data[index] = insertValue;
    Size++;
    }
};

SubdomainPart.h

// SubdomainPart.h - SubdomainPart validation class declaration
// Written by -------
#pragma once
#include "String.h"
using namespace std;
class SubdomainPart
{
public:
    // Takes the address and stores into the Address data member
    SubdomainPart(const String& address);
    // Returns true when the Address is valid or false otherwise
    virtual bool IsValid();
private:
    String Address;
};

SubdomainPart.cpp

// SubdomainPart.cpp - Subdomain validation class implementation
// Written by ---------
#pragma once
#include "SubdomainPart.h"
// Takes the address and stores into the Address data member
SubdomainPart::SubdomainPart(const String& address)
{
    Address = address;
}
// Returns true when the Address is valid or false otherwise
bool SubdomainPart::IsValid()
{
    int currentDotIndex = 0;
    int nextDotIndex = 0;
    int found = 0; // first index of a found invalid character
    int hyphenIndex = 0; // used to check hyphen rule
    // 1. Check the size, 255 total characters
    if(Address.GetLength() < 1 || Address.GetLength() > 255)
        return false;

    // Checks for valid amount of 1-63 characters between dots
    currentDotIndex = Address.FindFirstOf('.');
    if(currentDotIndex == 0 || currentDotIndex == Address.GetLength()-1)
        return false;
    else if(currentDotIndex!=(-1))
        nextDotIndex = Address.Find('.', currentDotIndex+1);
    else
        nextDotIndex = (-1); // if no '.' is found, ensures the following loop doesn't run.
    while(nextDotIndex!=(-1))
    {
        if((nextDotIndex-currentDotIndex) == 1 || (nextDotIndex-currentDotIndex) > 63)
            return false;
        currentDotIndex = nextDotIndex;
        nextDotIndex = Address.Find('.', currentDotIndex+1);
    }   
    // 2. Check for valid characters
    found = Address.FindFirstNotOf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-.");
    if(found!=(-1)) // if a character not listed above is found.
        return false;
    // 3. Check for dash rule
    // Making sure hyphens aren't located at the first or last index of a subdomain.
    hyphenIndex = Address.FindFirstOf('-');
    if(hyphenIndex == 0)
        return false;
    hyphenIndex = Address.FindLastOf('-');
    if(hyphenIndex == Address.GetLength()-1)
        return false;
    // Makes sure two hyphens aren't in a row.
    for(int x = 1; x<Address.GetLength(); x++)
        if(Address[x] == '-' && Address[x] == Address[x-1])
            return false;
    return true;
}

我在这个类中没有看到默认构造函数:

class SubdomainPart
{
public:
    // Takes the address and stores into the Address data member
    SubdomainPart(const String& address);
    // Returns true when the Address is valid or false otherwise
    virtual bool IsValid();
private:
    String Address;
};

请记住,这个映射构造函数是默认构造每个成员,而不是初始化它们:

Map(int initialCapacity = 10)
{
    Size = 0;
    Capacity = initialCapacity;
    Key;
    MappedValue;
    //Allocate the C-Array elements using HEAP
    Data = new VALUE_TYPE[Capacity];
}

您没有默认的 SubdomainPart构造函数,您只提供了一个复制构造函数。默认构造函数不带实参。

编译器抱怨SubdomainPart没有默认构造函数,确实没有。这是必需的,因为您的Map包含VALUE_TYPE类型的对象:

VALUE_TYPE MappedValue;

另外,您的Map构造函数包含非常奇怪的代码。我假设您实际上想使用初始化器列表:

Map(int initialCapacity = 10)
  : Key()
  , MappedValue()
  , Size(0)
  , Capacity(initialCapacity)
  , Data(new VALUE_TYPE[Capacity])
{}

问题是Data = new VALUE_TYPE[Capacity];部分。编译器生成代码来分配数组,并通过调用VALUE_TYPE的无参数构造函数实例化每个元素。由于SubdomainPart没有(因为您已经定义了一个自定义的),编译器会抛出一个错误。

编译器在map.h中报告错误的原因是它正是调用构造函数的地方。在SubdomainPart代码中没有使用它,它只是在那里定义的。