c++ - 使用类模板的未知覆盖说明符
问题描述
我正在为一个游戏编写代码,该游戏具有一个角色类,其中三个元素是其他类。其中之一是用于存放物品的背包。Backpack 是使用另一个名为 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;
}
背包.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
解决方案
推荐阅读
- ns-3 - ns-3 网络模拟器上的流量建模
- typescript - Nestjs 与自定义存储库中的 Typeorm 事务
- css - 添加工具提示而不添加更多标记的最简单方法
- javascript - 类型“字符串”不可分配给类型“从不”
- unity3d - Unity Firebase 安全
- c++ - SIMD 减少 4 个向量,没有 hadd
- java - 如何在浏览器堆栈中自动化 chrome 浏览器中的移动仿真?
- git - 创建的符号链接不响应文件内容更改
- typescript - 如何在 TypeScript 中自动完成对象键
- typescript - 如何推断作为打字稿中对象属性的数组的类型?