C++ 无法实例化抽象类,也无法将参数1从';常数大小';至';const BDictionary<;int,std::string>&';错误
我正在按照本项目的指示使用Visual Studio 2017 我已经为我的程序收到了四个错误,我已经研究了几天,无论我做了什么,我都不能让这些错误消失 我试过在BDictionary中创建多个构造函数,我检查了所有函数名,没有输入错误,我和其他人一起检查了这段代码,比较了他们的版本,看看为什么会出现这些错误。我只是担心在这一点上运行它,因为我可以从中找出哪些函数不起作用。任何和所有的帮助都将不胜感激 bagtestmain.cpp-无法更改此文件C++ 无法实例化抽象类,也无法将参数1从';常数大小';至';const BDictionary<;int,std::string>&';错误,c++,visual-studio-2017,C++,Visual Studio 2017,我正在按照本项目的指示使用Visual Studio 2017 我已经为我的程序收到了四个错误,我已经研究了几天,无论我做了什么,我都不能让这些错误消失 我试过在BDictionary中创建多个构造函数,我检查了所有函数名,没有输入错误,我和其他人一起检查了这段代码,比较了他们的版本,看看为什么会出现这些错误。我只是担心在这一点上运行它,因为我可以从中找出哪些函数不起作用。任何和所有的帮助都将不胜感激 bagtestmain.cpp-无法更改此文件 #include <string>
#include <string>
#include <sstream>
#include "ABag.h"
#include "BDictionary.h"
using namespace std;
const size_t DICTIONARY_SIZE = 20;
void PauseScreen(); //Used to pause screen output
/*
* Tests BDictionary with int and string objects only.
*/
int main(int argc, char** argv) {
cout << "Student Name -- CSIS 215 Programming Assignment 1 -- Bag Dictionary" << endl << endl;
BDictionary<int, string> myIntStrDict(DICTIONARY_SIZE);
//Error C2259 'BDictionary<int,std::string>': cannot instantiate abstract class 27
//Error C2664 'BDictionary<int,std::string>::BDictionary(const BDictionary<int,std::string> &)': cannot convert argument 1 from 'const size_t' to 'const BDictionary<int,std::string> &' 27
BDictionary<string, int> myStrIntDict(DICTIONARY_SIZE);
//Error C2259 'BDictionary<std::string,int>': cannot instantiate abstract class 28
//Error C2664 'BDictionary<std::string,int>::BDictionary(const BDictionary<std::string,int> &)': cannot convert argument 1 from 'const size_t' to 'const BDictionary<std::string,int> &' 28
#包括
#包括
#包括“ABag.h”
#包括“BDictionary.h”
使用名称空间std;
const size\u t DICTIONARY\u size=20;
void PauseScreen()//用于暂停屏幕输出
/*
*仅使用int和string对象测试BDictionary。
*/
int main(int argc,字符**argv){
cout=0;i-->//自上而下遍历每个对象
{
if(data[i]==item)//如果值相等,则返回该项
{
项目=数据[i];
位置--;
for(int j=i;j=0;i--)//循环对象并返回值
{
if(数据[i]==返回值)
{
returnValue=data[i];//返回找到的KVpair
返回true;
}
}
}
返回false;
}
//检查行李顶部。如果行李为空,则返回
//false并保持“item”不变;否则,返回true并更新
//“物品”和袋子里的东西。
布尔检查顶(E&item)常数
{
如果(位置==-1)
{
返回false;
}
其他的
{
项目=数据[位置];
返回true;
}
}
//倒空袋子
void emptyBag()
{
position=-1;//将位置下移到-1,从某种意义上说,删除元素
}
//使用+=操作员向行李添加物品
布尔运算符+=(常量和加数)
{
return addItem(addend);//无需重写addItem,从这里调用即可
}
//了解袋子的大小
int size()常量
{
返回位置+1;//返回实际大小
}
//获得袋子的容量
int bagCapacity()常量
{
返回能力;
}
//包装方法:添加、移除、操作员+=、尺寸等。
};
#endif/*ABAG_H*/
BDictionary.h
#ifndef ABAG_H
#define ABAG_H
#include "book.h"
#include "bagADT.h"
#include <string>
#include <stdlib.h>
template <typename E>
class ABag : public Bag<E> {
private:
int position, capacity;
const int max = 10;
E* data;
public:
// constructors/destructor
ABag()
{
max = defaultSize;
capacity = position = 0;
data = new E[max];
}
~ABag()
{
delete[] data;
}
// Insert a new item into the bag -- return false if fails and true if
// successful
bool addItem(const E& item)
{
if (bagCapacity < 10)
{
position++;
data[position]; //incorrect
capacity++;
return true;
}
else
{
return false;
}
}
// Looks for 'item' in the bag and if found updates 'item' with the
// bag value and returns true. Otherwise 'item' is left unchanged
// and the method returns false.
bool remove(E& item)
{
if (position == -1) // check if bag is full
{
return false;
}
else
{
for (int i = position; i >= 0; i--) // loop through each object top to bottom
{
if (data[i] == item) // if the values are equal, then return the item
{
item = data[i];
position--;
for (int j = i; j <= position; j++)
{
data[j] = data[j + 1]; // shift down the objects on top
}
}
}
return true;
}
}
// Removes the top record from the bag, puts it in returnValue, and
// returns true if the bag is not empty. If the bag is empty the
// function returns false and returnValue remains unchanged.
bool removeTop(E& returnValue)
{
if (inspectTop(returnValue))
{
return false;
}
position--; //return
return true;
}
// Finds the record using returnValue and if the record is found updates
// returnValue based on the contents of the bag and returns true. If the
// record is not found the function returns false. Works just like remove()
// except that the found record is not removed from the bag.
bool find(E& returnValue) const
{
if (position == -1)
{
return false;
}
else
{
for (int i = position; i >= 0; i--) // loops through objects and returns value
{
if (data[i] == returnValue)
{
returnValue = data[i]; // returns the KVpair found
return true;
}
}
}
return false;
}
// Inspect the top of the bag. If the bag is empty return
// false and leave 'item' unchanged; otherwise, return true and update
// 'item' with the contents of the bag.
bool inspectTop(E& item) const
{
if (position == -1)
{
return false;
}
else
{
item = data[position];
return true;
}
}
// empties the bag
void emptyBag()
{
position = -1; // Moves the position down to -1, in a sense removing the elements
}
// use the += operator to add an item to the bag
bool operator+=(const E& addend)
{
return addItem(addend); // No need to rewrite addItem, just call it from here
}
// get the size of the bag
int size() const
{
return position + 1; // returns the actual size
}
// get the capacity of the bag
int bagCapacity() const
{
return capacity;
}
// bag methods: addItem, remove, operator+=, size, etc.
};
#endif /* ABAG_H */
#ifndef BDICTIONARY_H
#define BDICTIONARY_H
#include "ABag.h"
#include "dictionaryADT.h"
#include "kvpair.h"
#include "book.h"
#include <string>
using namespace std;
template <typename Key, typename E>
class BDictionary : public Dictionary<int, string>{
public:
// constructors/destructor
BDictionary()
{
max = defaultSize;
capacity = -1;
position = -1;
dictionary = new E[max];
} // Default constructor
BDictionary(const Key& k, const Key& e)
{
max = defaultSize;
capacity = position = 0;
}
~BDictionary()
{
delete[] data.
} // Base destructor
// Reinitialize dictionary
void clear()
{
dictionary->emptyBag(); // Calls ABag's emptyBag
}
// Insert a record
// k: The key for the record being inserted.
// e: The record being inserted.
// Return true if insert is successful and false otherwise
bool insert(const Key& k, const E& e)
{
KVpair<int, string> item(k, e);
if (dictionary->addItem(item))
{
return true;
}
else
{
return false;
}
}
// Looks for a record using the key and if found does the following:
// - updates the E& rtnVal
// - removes the record from the dictionary
// - returns true
// If the record is not found the function returns false.
bool remove(const Key& k, const E& rtnVal)
{
KVpair<int, string> item(k, rtnVal);
if (dictionary->remove(item))
{
return true;
}
else
{
return false;
}
}
// Takes an arbitrary record from the dictionary and does the following:
// - updates the E& returnValue
// - removes the record from the dictionary
// - returns true
// If the dictionary is empty the function returns false.
bool removeAny(E& returnValue)
{
/*Key* k;
k = new Key();
KVpair<Key, E> fill(*k, returnValue);
if (dictionary->removeTop(fill)) // Use of removeTop since we can delete ANY arbitrary element
{
returnValue = fill.value();
delete k;
return true;
}
else
{
delete k;
return false;
}*/
KVpair<int, string> item(0, "");
if (dictionary->removeTop(item))
{
//cout << "REMOVEANY: Removed key " << item.key() << ", which contained the value " << item.value() << endl;
return true;
}
else
{
return false;
}
}
// Looks for a record using the key and if found does the following:
// - updates the E& returnValue
// - returns true
// If the record is not found the function returns false.
bool find(const Key& k, E& returnValue) const
{
KVpair<int, string> item(k, returnValue);
if (dictionary->find(item))
{
//std::cout << "FIND: The value at key " << item.key() << " is " << item.value() << endl;
return true;
}
else
{
return false;
}
}
// Return the number of records in the dictionary.
int size()
{
return dictionary->size(); // calls size function from ABag
}
// methods: clear, insert, remove, removeAny, find, size, etc.
private:
//Pointer to a ABag object. You'll need to instantiate the bag in your constructor:
// dictionary = new ABag<KVpair<Key, E>>(size) or something similar depending on how
// you've implemented your ABag constructor(s).
//This pointer gives you access to the bag which stores your data and provides the
//functions you need to build your dictionary.
ABag<KVpair<int, string>>* dictionary;
int capacity, position;
const size_t max;
};
#endif /* BDICTIONARY_H */
#如果不使用BDICTIONAL#
#定义BDICTIONARY_H
#包括“ABag.h”
#包括“dictionaryADT.h”
#包括“kvpair.h”
#包括“book.h”
#包括
使用名称空间std;
模板
B类词典:公共词典{
公众:
//构造函数/析构函数
b词典()
{
最大值=默认大小;
容量=-1;
位置=-1;
字典=新的E[max];
}//默认构造函数
B索引(常数键和k、常数键和e)
{
最大值=默认大小;
容量=位置=0;
}
~BDictionary()
{
删除[]数据。
}//基析构函数
//重新初始化字典
无效清除()
{
dictionary->emptyBag();//调用ABag的emptyBag
}
//插入记录
//k:正在插入的记录的键。
//e:正在插入的记录。
//如果插入成功,则返回true;否则返回false
bool插件(常数键和k、常数E和E)
{
KVpair项(k,e);
如果(字典->附加项(项目))
{
返回true;
}
其他的
{
返回false;
}
}
//使用键查找记录,如果找到,则执行以下操作:
//-更新E&rtnVal
//-从字典中删除记录
//-返回true
//如果未找到记录,函数将返回false。
bool移除(常数键和k、常数E和rtnVal)
{
KVpair项目(k,rtnVal);
如果(字典->删除(项目))
{
返回true;
}
其他的
{
返回false;
}
}
//从字典中获取任意记录并执行以下操作:
//-更新E&RETURN值
//-从字典中删除记录
//-返回true
//如果字典为空,则函数返回false。
布尔删除(E&returnValue)
{
/*键*k;
k=新键();
KVpair填充(*k,返回值);
if(dictionary->removeTop(fill))//使用removeTop,因为我们可以删除任意元素
{
returnValue=fill.value();
删除k;
返回true;
}
其他的
{
删除k;
返回false;
}*/
KVpair项(0,“”);
如果(字典->移除(项目))
{
//您的Dictionary
类可以声明在哪里?它有默认构造函数吗?如果没有,这是一个问题,因为您没有在bdictional
的构造函数中初始化它。请提供一个示例,您基本上没有覆盖“dictionaryADT.h”抽象方法(或者在继承树上的某个地方-您有一个虚拟方法,但尚未转化为实际方法)。#包括“ABag.h”#包括“dictionaryADT.h”#包括“kvpair.h”#包括“book.h”C++包含了告诉我你没有使用C++库中的标准类。你的<代码>字典<代码>类声明了什么?它有默认的构造函数吗?如果不是,那就是一个问题,因为你不在代码> BDATACANEX/COD>的构造中把它初始化。请提供一个你基本上没有覆盖的“字典Adt.h”。我猜是抽象的方法