使用类模板的未知替代说明符

时间:2018-11-26 23:55:38

标签: c++ class object templates

我正在为一款游戏代码编写游戏,该游戏的角色类别带有其他三个类别的元素。其中之一是用于存放物品的背包。背包是使用另一个称为DynamicArray的类创建的,该类基本上是可以容纳其他对象的动态数组。我将动态数组类转换为类模板,以便该数组可以容纳任何对象,现在在我的Character类中无法识别Backpack类。

动态数组类模板:

#pragma once
#include <string>
#include <iostream>
using std::ostream;
using std::endl;
#include "potions.h"
#include "character.h"

template<typename TYPE>
class DynamicArray
{
public:
    // Constructors
    DynamicArray();
    ~DynamicArray();
    DynamicArray(const DynamicArray & copy);

    // Op Equals
    DynamicArray & operator =(const DynamicArray & rhs);

    // Getters
    int getElements();
    TYPE getElement(int index);

    // Manipulate and view contents
    void Insert(const TYPE & add);
    void Delete(const TYPE & rmv);
    void display();

    // Overloaded operators
    TYPE & operator [](int index);
    friend ostream & operator << (ostream & out, const DynamicArray & d);

private:
    // Member variables
    TYPE * m_array;
    int m_elements;

    // Find function
    int Find(const TYPE & target);
};

/**********************************************************************
* Purpose: Default constructor
*
* Postcondition:
* Initializes m_array and m_elements to nullptr and 0
************************************************************************/
template<typename TYPE>
DynamicArray<TYPE>::DynamicArray() : m_array(nullptr), m_elements(0)
{

}

/**********************************************************************
* Purpose: Destructor
*
* Postcondition:
* Delete m_array
************************************************************************/
template<typename TYPE>
DynamicArray<TYPE>::~DynamicArray()
{
    if (m_array != nullptr)
    {
        delete[] m_array;
    }
}

/**********************************************************************
* Purpose: Copy construcxtor
*
* Postcondition:
* Take an object as an argument and copy the contents to a new object
************************************************************************/
template<typename TYPE>
DynamicArray<TYPE>::DynamicArray(const DynamicArray & copy) : m_elements(copy.m_elements)
{
    int arrSize = copy.m_elements;
    m_array = new TYPE[arrSize];

    for (int i = 0; i < arrSize; i++)
    {
        m_array[i] = copy.m_array[i];
    }
}

/**********************************************************************
* Purpose: Op equals
*
* Postcondition:
* Perform a deep copy of one object to another using the = operator
************************************************************************/
template<typename TYPE>
DynamicArray<TYPE> & DynamicArray<TYPE>::operator =(const DynamicArray & rhs)
{
    if (this != &rhs)
    {
        delete[] m_array;

        m_elements = rhs.m_elements;
        m_array = new TYPE[m_elements];

        for (int i = 0; i < m_elements; i++)
        {
            m_array[i] = rhs.m_array[i];
        }
    }

    return *this;
}

/**********************************************************************
* Purpose: Elements getter
*
* Postcondition:
* Return the number of elements
************************************************************************/
template<typename TYPE>
int DynamicArray<TYPE>::getElements()
{
    return m_elements;
}

template<typename TYPE>
TYPE DynamicArray<TYPE>::getElement(int index)
{
    return m_array[index];
}

/**********************************************************************
* Purpose: Insert item into dynamic array
*
* Postcondition:
* Increment m_elements by 1
* Add a new potion to the dynamic array
************************************************************************/
template<typename TYPE>
void DynamicArray<TYPE>::Insert(const TYPE & add)
{
    if (m_elements != 0)
    {
        TYPE * temp = new TYPE[m_elements];

        for (int i = 0; i < m_elements; i++)
        {
            temp[i] = m_array[i];
        }

        delete[] m_array;

        m_array = new TYPE[m_elements + 1];

        for (int i = 0; i <= m_elements; i++)
        {
            if (i == m_elements)
            {
                m_array[i] = add;
            }
            else
            {
                m_array[i] = temp[i];
            }
        }

        delete[] temp;

        m_elements++;
    }
    else
    {
        m_elements++;

        m_array = new TYPE[m_elements]{ add };
    }
}

/**********************************************************************
* Purpose: Delete item from dynamic array
*
* Precondition: Array must contain at least one item
*
* Postcondition:
* Decrement the number of elements
* Find an item in the dynamic array
* Remove an item from the dynamic array
************************************************************************/
template<typename TYPE>
void DynamicArray<TYPE>::Delete(const TYPE & rmv)
{
    if (m_elements != 0)
    {
        int index = Find(rmv);

        if (index != -1)
        {
            TYPE * temp = new TYPE[(m_elements - 1)];

            for (int i = 0; i < m_elements; i++)
            {
                if (i < index)
                {
                    temp[i] = m_array[i];
                }
                else if (i > index)
                {
                    temp[(i - 1)] = m_array[i];
                }
            }

            delete[] m_array;

            m_elements--;

            m_array = new TYPE[m_elements];

            for (int i = 0; i < m_elements; i++)
            {
                m_array[i] = temp[i];
            }

            delete[] temp;
        }
    }
}

/**********************************************************************
* Purpose: Display the elements of the array
*
* Postcondition:
* Send the array elements out to the console
************************************************************************/
template<typename TYPE>
void DynamicArray<TYPE>::display()
{
    for (int i = 0; i < m_elements; i++)
    {
        m_array[i].display();
    }
}

/**********************************************************************
* Purpose: Overloaded subscript operator
*
* Postcondition:
* Return the array object given by the index
************************************************************************/
template<typename TYPE>
TYPE & DynamicArray<TYPE>::operator [](int index)
{
    return m_array[index];
}

/**********************************************************************
* Purpose: Overloaded Stream Insertion Operator
*
* Postcondition: Overloads the stream insertion operator in order to
* output the contents of the character using cout
*
************************************************************************/
template<typename TYPE>
ostream & operator << (ostream & out, const DynamicArray<TYPE> & d)
{
    for (int i = 0; i < d.m_elements; i++)
    {
        out << d.m_array[i] << endl;
    }

    return out;
}

/**********************************************************************
* Purpose: Find an item in the dynamic array
*
* Postcondition:
* Return the index of the found item
************************************************************************/
template<typename TYPE>
int DynamicArray<TYPE>::Find(const TYPE & target)
{
    int found = -1;

    for (int i = 0; i < m_elements; i++)
    {
        if (m_array[i] == target)
        {
            found = i;
        }
    }

    return found;
}

Backpack.h文件

#pragma once
#include "dynamicarray.h"
#include "coinPouch.h"
#include "string.h"
#include "potions.h"

class Backpack
{
public:
    // Constructors
    Backpack();
    Backpack(DynamicArray<Potion> potions);
    Backpack(const Backpack & copy);
    ~Backpack();

    // Overloaded operators
    Backpack & operator = (const Backpack & rhs);
    friend ostream & operator << (ostream & out, const Backpack & c);

    // Manipulate and view contents
    void addPotion(const Potion & add);
    void usePotion(const Potion & rm);
    void displayContents();

    // Getters
    int getNumber();
    DynamicArray<Potion> getPotions();
    Potion getPotion(int index);

private:
    DynamicArray<Potion> m_potions;
    int m_number;
};

Character.h文件

#pragma once
#include <iostream>
using std::ostream;
#include "string.h"
#include "coinPouch.h"
#include "backpack.h"
#include "dynamicarray.h"
#include "potions.h"


class Character
{
public:
    // Constructors
    Character();
    Character(String name);
    Character(String name, CoinPouch wallet, Backpack storage);
    Character(const Character & copy);

    // Destructor
    ~Character();

    // Overloaded operators
    Character & operator =(const Character & rhs);
    friend ostream & operator << (ostream & out, const Character & c);

    // Manipulate and display contents
    void purchase(int p, int g, int s, int c);
    void income(int p, int g, int s, int c);
    void addPotion(const Potion & toAdd);
    void checkBalance();
    void checkBackpack();
    void changeName(const String & newN);
    void display();

    // Getters and setters
    String getName();
    CoinPouch getWallet();
    Backpack getStorage();
    void setName(String name);
    void setWallet(CoinPouch wallet);
    void setStorage(Backpack storage);

    // Random Number Generator
    int randomNum();

private:
    String m_name;
    CoinPouch m_wallet;
    Backpack m_storage;

};

错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    cst136_lab1_ehayes  c:\users\elijah hayes\documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\character.h    49  
Error   C3646   'm_storage': unknown override specifier cst136_lab1_ehayes  c:\users\elijah hayes\documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\character.h    49  
Error   C2061   syntax error: identifier 'Backpack' cst136_lab1_ehayes  c:\users\elijah hayes\documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\character.h    41  
Error   C2059   syntax error: '('   cst136_lab1_ehayes  c:\users\elijah hayes\documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\character.h    38  
Error   C2238   unexpected token(s) preceding ';'   cst136_lab1_ehayes  c:\users\elijah hayes\documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\character.h    38  
Error   C3646   'getStorage': unknown override specifier    cst136_lab1_ehayes  c:\users\elijah hayes\documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\character.h    38  
Error   C2061   syntax error: identifier 'Backpack' cst136_lab1_ehayes  c:\users\elijah hayes\documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\character.h    16  

我只是从类模板开始,不知道会发生什么。试图解决这一问题一段时间,我们将不胜感激。谢谢!

编辑:我认为问题是循环依赖性,并且我想通过删除动态数组类中包含的Character.h和Potions.h来解决此问题。那是我唯一改变的。现在,我收到此错误:

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class DynamicArray<class Potion> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$DynamicArray@VPotion@@@@@Z) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Backpack const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVBackpack@@@Z)	cst136_lab1_ehayes	C:\Users\Elijah Hayes\Documents\cst136_lab1_ehayes-master\cst136_lab1_ehayes\backpack.obj	1	

0 个答案:

没有答案