This question already has answers here:
vector of class without default constructor
(3 answers)
Object array initialization without default constructor
(14 answers)
C++ compiler complaining about no default constructor
(1 answer)
Closed 2 years ago.
As I currently study Java in University but I am working on a C++ project. It is a text game in which I'm trying to create an empty vector of Item objects which I'm later going to add with the addItem(Item newItem) method of the Room object. The problem is that when in the constructor I try to set it so that every time a room is created, a new vector of Item objects is created with a capacity of the vector of 20, it gives me this error:
'Item::Item': no appropriate default constructor available
Here is the code:
Item.hpp:
#include <iostream>
#include <string>
#ifndef Item_HPP
#define Item_HPP
class Item {
std::string description;
public:
Item(std::string description);
std::string getDescription();
void setDescription(std::string description);
void use();
};
#endif
Item.cpp:
#include "Item.hpp"
Item::Item(std::string description) {
this->description = description;
}
std::string Item::getDescription() {
return this->description;
}
void Item::setDescription(std::string description) {
this->description = description;
}
void Item::use() {
std::cout << "You are using item: " << description << std::endl;
}
Room.hpp:
#include <iostream>
#include <string>
#include <vector>
#include "Item.hpp"
#ifndef Room_HPP
#define Room_HPP
class Room {
std::string description;
static const int MAX_ITEMS = 20;
std::vector <Item> items;
int numberOfItems;
public:
Room(std::string description);
std::string getDescription();
void addItem(Item newItem);
std::vector<Item> getItems();
Item getItem(std::string description);
};
#endif
Room.cpp:
#include "Room.hpp"
Room::Room(std::string description) {
this->description = description;
this->items = std::vector<Item>(20);
this->numberOfItems = 0;
}
std::string Room::getDescription() {
return this->description;
}
void Room::addItem(Item newItem) {
if (numberOfItems < MAX_ITEMS) {
this->items[numberOfItems] = newItem;
numberOfItems++;
}
}
std::vector<Item> Room::getItems() {
return this->items;
}
Item Room::getItem(std::string description) {
int i = 0;
bool found = false;
Item *target = NULL;
while (!found && i < numberOfItems) {
if (items[i].getDescription() == description) {
target = &items[i];
found = true;
}
++i;
}
return *target;
}
You can firstly just reserve() buffer for 20 elements and later add elements via push_back().
Room::Room(std::string description) {
this->description = description;
this->items = std::vector<Item>();
this->items.reserve(20);
this->numberOfItems = 0;
}
void Room::addItem(Item newItem) {
if (numberOfItems < MAX_ITEMS) {
if (numberOfItems == static_cast<int>(this->items.size())) {
this->items.push_back(newItem);
} else if (numberOfItems < static_cast<int>(this->items.size())) {
this->items[numberOfItems] = newItem;
} else {
// unsupported to create gap between items
}
numberOfItems++;
}
}
Related
I'm pretty new to C++ and am trying to teach myself how to implement a hash table(I know I could use unordered_map, but I'm challenging myself). Right now I have a vector of structs(cell) that holds person-specific information. I have a PrintTable function that I want to use to print out each struct member of every item in the table. However, I can't seem to access the specific members of the struct(cell). What am I doing wrong here?
#include <iostream>
#include <string>
#include <vector>
struct cell
{
std::string name;
int age;
std::string weapon;
};
class HashTable
{
private:
std::vector<cell> *table;
int total_elements;
int getHash(int key)
{
return key % total_elements;
}
public:
HashTable(int n)
{
total_elements = n;
table = new std::vector<cell>[total_elements];
}
void SearchTheTable(int hashIndex);
void AddItem(std::string name, int age, std::string weapon);
void RemoveItem();
void PrintTable();
};
void HashTable::SearchTheTable(int hashIndex)
{
int x = getHash(hashIndex);
std::cout << x;
}
void HashTable::AddItem(std::string name, int age, std::string weapon)
{
cell newCell = { name, age, weapon };
table->push_back(newCell);
}
void HashTable::RemoveItem()
{
}
void HashTable::PrintTable()
{
for (int i = 0; i < table->size; i++)
{
std::cout << table[i].name; // Right here I get an error that says: class "std::vector<cell, std::allocator<cell>>" has no member "name".
}
}
int main()
{
HashTable theTable(5);
theTable.AddItem("Ryan", 27, "Sword");
theTable.AddItem("Melony", 24, "Axe");
theTable.PrintTable();
}
I am having an issue where every spot where I make a function call, such as item.GetName(), on an item in the vector<ItemToPurchase> items , I get the error message: no matching function for call to vector<ItemToPurchase,__default_alloc_template<false,0> >::at (int &) I've been looking all over and don't even uderstand what the "no matching function for call to" even means.
Example g++ message
ShoppingCart.cpp: In method `bool ShoppingCart::CheckCartForItem(class
string)':
ShoppingCart.cpp:22: no matching function for call to
`vector<ItemToPurchase,__default_alloc_template<false,0> >::at (int &)'
ShoppingCart.h:
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include "ItemToPurchase.h"
#include <vector>
class ShoppingCart
{
public:
ShoppingCart(string customerName = "none", string dateCreated = "January 1, 2016");
*other functions*
bool CheckCartForItem(string itemName);
void AddItemToCart(ItemToPurchase item);
*more functions*
private:
*other variables*
std::vector<ItemToPurchase> items;
};
#endif
ShoppingCart.cpp
#include "ShoppingCart.h"
#include <iostream>
ShoppingCart::ShoppingCart(string customerName, string dateCreated)
{
this->customerName = customerName;
this->dateCreated = dateCreated;
}
bool ShoppingCart::CheckCartForItem(string itemName)
{
if (items.size() == 0) //if cart is empty
return false;
for (int i = 0; i < items.size(); i++)
{
if (items.at(i).GetName() == itemName) //<- Problem here
return true;
}
return false;
}
void ShoppingCart::AddItemToCart(ItemToPurchase item)
{
if (CheckCartForItem(item.GetName()) == false) //if item not in cart
items.push_back(item);
else
cout << "Item is already in cart. Nothing added." << endl;
}
ItemToPurchase.h
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
#include <string>
using namespace std;
class ItemToPurchase
{
public:
ItemToPurchase(string name = "none", string description = "none", double
price = 0.0, int quantity = 0);
void SetName(string);
string GetName();
*other functions*
private:
string itemName;
*other variables*
};
#endif // ITEMTOPURCHASE_H
ItemToPurchase.cpp
#include "ItemToPurchase.h"
#include <iostream>
#include <iomanip>
ItemToPurchase::ItemToPurchase(string name, string description, double
price, int quantity)
{
itemName = name;
itemDescription = description;
itemPrice = price;
itemQuantity = quantity;
}
void ItemToPurchase::SetName(string name){ itemName = name; }
string ItemToPurchase::GetName() { return itemName; }
Since in this case you don't use the index variable, you can iterate with a range-based for loop:
for (auto& item : items) {
if (item.GetName() == itemName) {
[...]
std::vector:at expects a std::vector::size_type (which is typically defined as std::size_t). Changing the type of i should make it compatible when using the index-based accessor:
for (std::vector<ItemToPurchase>::size_type i = 0; i < items.size(); ++i) {
if (items.at(i).GetName() == itemName) {
[...]
I'm new to C++, and I've spent a few hours trying to determine why the "execute.ListAllBooks()" function in main.cpp seems to be including the string assigned to the libraryName variable (which is declared in Library.h and initialized in the constructor in Library.cpp) within the array entitled "BookList". The last element of the array lists as "MyLibrary", when I expect it to be an empty string. An additional question I have is why the array seems to have 11 elements in it. I previously coded in Java, and I would expect there to be ten elements considering I initialized the array with this line "public: std::string BookList[10]". Any help would be appreciated. Thanks!
main.cpp
#include <iostream>
#include "Library.h"
int main() {
Library execute("MyLibrary");
execute.AddBook("Book 1");
execute.AddBook("Book 2");
execute.AddBook("Book 3");
execute.AddBook("Book 4");
execute.AddBook("Book 5");
execute.AddBook("Book 6");
execute.AddBook("Book 7");
execute.AddBook("Book 8");
execute.AddBook("Book 9");
execute.AddBook("Book 10");
execute.ListAllBooks();
return 0;
}
Library.cpp
#include <iostream>
#include "Library.h"
using namespace std;
Library::Library(const string &name) {
libraryName = name;
length = 0;
}
bool Library::AddBook(const string &name) {
counter = 0;
int arraySize = sizeof(BookList)/sizeof(*BookList);
while(counter < arraySize) {
if(BookList[counter] == name) {
return false;
}
counter++;
}
if(length >= arraySize) {
return false;
}
else {
length++;
BookList[length - 1] = name;
return true;
}
}
bool Library::RemoveBook(const std::string &name) {
counter = 0;
while(counter < sizeof(BookList)/sizeof(*BookList)) {
if(BookList[counter] == name) {
BookList[counter] = "";
length--;
while(counter < length - 1) {
BookList[counter] = BookList[counter + 1];
BookList[counter + 1] = "";
counter++;
}
return true;
}
counter++;
}
return false;
}
void Library::ListAllBooks() const {
int length = sizeof(BookList);
int counter = 0;
while(counter < length) {
cout << BookList[counter] + "," << endl;
counter++;
}
}
Library.h
#include <iostream>
#ifndef ASS1_LIBRARY_H
#define ASS1_LIBRARY_H
class Library {
public: std::string BookList[10];
private: std::string libraryName;
private: int length;
private: int counter;
public: explicit Library(const std::string &name);
// Add a new book,
// return true for success, false if book already in library
bool AddBook(const std::string &name);
// Remove a book
// return true for success, false if book not in library
bool RemoveBook(const std::string &name);
// List all books in library
public: void ListAllBooks() const;
// Return true if book in library, false otherwise
bool IsInLibrary(const std::string &name) const;
};
// friend function
std::ostream &operator<<(std::ostream &out, const Library &lib);
#endif //ASS1_LIBRARY_H
The line int length = sizeof(BookList); is incorrect.
sizeof returns the number of bytes an object takes to store in memory. It just so happens that the size of Libary is 11. When BookList[11] is accessed you are overflowing the array and reading the next string in memory which is libraryName because of the order they were defined in your class.
You could change you class definition to:
class Library {
public: std::string BookList[10];
public: std::string hello "Hello";
...
And accessing BookList[11] would result in Hello.
It looks like you are already calculating the correct length in AddBook so deleting the line int length = sizeof(BookList); all togethor should fix your problem.
On a somewhat related note, you might look into using std::array to help prevent erros like this in the future.
I first get an object A from the vector, then I call the erase method to destroy that object in my vector because I don't need it anymore. However, from the debugger, I found that the object A I got before calling erase method is also destroyed. I don't understand that because I thought that what I got is a copy of that object and erase method should have nothing to do with my object A.
Code
Class Unit
Header file
#ifndef UNIT_H
#define UNIT_H
#include <iostream>
class Unit
{
protected:
int id;
public:
Unit::Unit(int num = -1);
virtual ~Unit() = default;
virtual int getID();
};
#endif
CPP file
#include "Unit.h"
Unit::Unit(int num)
{
id = num;
}
int Unit::getID()
{
return id;
}
Class Box
Header file
#ifndef BOX_H
#define BOX_H
#include <string>
#include <iostream>
#include "Unit.h"
class Box : public Unit
{
private:
std::string* type;
int* val;
public:
Box::Box();
~Box();
int getVal();
std::string getName();
int getID() override;
};
#endif
CPP file
#include <time.h>
#include "Box.h"
Box::Box() : Unit(5)
{
int tmp = rand() % 3;
if (tmp == 0)
{
type = new std::string("hp"); // health cur
val = new int(rand() % 10 + 1);
}
else if (tmp == 1)
{
type = new std::string("exp"); // skill level or health max
val = new int(rand() % 5 + 1);
}
else
{
type = new std::string("punish"); // minus health cur
val = new int(-1);
}
}
Box::~Box()
{
delete type;
delete val;
}
int Box::getVal()
{
return *val;
}
std::string Box::getName()
{
return *type;
}
int Box::getID()
{
return id;
}
main file
using namespace std;
int main()
{
Box test;
std::vector<Box> bag;
bag.push_back(test);
Box tmp = bag[0];
bag.erase(bag.begin() + 0);
cout << tmp.getVal();
system("pause");
return 0;
}
Below is the screenshot from the debugger and because I don't have 10 reputations, I can't display it directly.
before
after
As you can see, the "type" and "val" data member of class Box is modified.
Check out this page about the return type from the index call
http://en.cppreference.com/w/cpp/container/vector/operator_at
I believe that you may have a reference, not a different object.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm working on implementing a chain data structure, using a specific set of pre-made templates. Upon compilation, I'm receiving the error in the title.
arrayList.h:
#ifndef arrayList_h
#define arrayList_h
#include <iostream>
#include "linearList.h"
using namespace std;
template<class T>
class arrayList : public linearList<T>{
public:
//constructor, copy constructor, destructor
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T>&);
~arrayList() {delete [] element;};
//ADT methods
bool empty() const {return listSize == 0;};
int size() const {return listSize;};
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out) const;
//additional method
int capacity() const {return arrayLength;};
protected:
void checkIndex(int theIndex) const;
//throw illegalIndex if theIndex is invalid
T* element; //1D array to hold list elements
int arrayLength; //capacity of 1D array
int listSize; //number of elements in list
};
#endif
chain.h:
#ifndef chain_h
#define chain_h
#include <iostream>
#include "linearList.h"
#include "chainNode.h"
using namespace std;
template<class T>
class chain : public linearList<T>{
public:
// constructor and destructor
chain(int initialCapacity = 10);
//chain(const chain<T>&);
~chain();
// ADT methods
bool empty() const {return listSize == 0;}
int size() const {return listSize;}
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out) const;
protected:
void checkIndex(int theIndex) const;
chain<T>* firstNode;
int listSize;
};
#endif
chainNode.h:
#ifndef chainNode_h
#define chainNode_h
#include <iostream>
template <class T>
struct chainNode
{
// data members
T element;
chainNode<T> *next;
// methods
chainNode() {}
chainNode(const T& element)
{this->element = element;}
chainNode(const T& element, chainNode<T>* next)
{this->element = element;
this->next = next;}
};
#endif
chain.cpp:
#include "chain.h"
#include "person.h"
using namespace std;
template<class T>
chain<T>::chain(int initialCapacity){
// Constructor.
/*if (initialCapacity < 1){
ostringstream s;
s << "Initial capacity = "
<< initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}*/
firstNode = NULL;
listSize = 0;
}
template<class T>
chain<T>::~chain(){
// Chain destructor. Delete all nodes
// in chain.
while (firstNode != NULL){
// delete firstNode
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
template<class T>
T& chain<T>::get(int theIndex) const{
// Return element whose index is theIndex.
checkIndex(theIndex);
// move to desired node
chainNode<T>* currentNode = firstNode;
for (int i = 0; i < theIndex; i++)
currentNode = currentNode->next;
return currentNode->element;
}
template<class T>
int chain<T>::indexOf(const T& theElement) const{
// search the chain for theElement
chainNode<T>* currentNode = firstNode;
int index = 0; // index of currentNode
while (currentNode != NULL && currentNode->element != theElement){
// move to next node
currentNode = currentNode->next;
index++;
}
// make sure we found matching element
if (currentNode == NULL)
return -1;
else
return index;
}
template<class T>
void chain<T>::erase(int theIndex){
checkIndex(theIndex);
chainNode<T>* deleteNode;
if (theIndex == 0){
// remove first node from chain
deleteNode = firstNode;
firstNode = firstNode->next;
}
else{
// use p to get to beforeNode
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;
deleteNode = p->next;
p->next = p->next->next;
}
listSize--;
delete deleteNode;
}
template<class T>
void chain<T>::insert(int theIndex, const T& theElement){
if (theIndex < 0 || theIndex > listSize){
// THROW ILLEGAL EXCEPTION
}
if (theIndex == 0) // insert at front
firstNode = new chainNode<T>(theElement, firstNode);
else{
// find predecessor of new element
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;
// insert after p
p->next = new chainNode<T>(theElement, p->next);
}
listSize++;
}
Person.h:
#ifndef Person_h
#define Person_h
#include <string>
#include <sstream>
using namespace std;
class Person{
public:
//Variables
string birthdate;
string first_name;
string last_name;
string hometownID;
string hometownName;
string userID;
string name;
//Constructors
Person();
Person(string birthdate, string first_name, string last_name, string hometownID, string hometownName, string userID);
//Methods
string getBirthdate();
void setBirthdate(string birthdate);
string getFirst_name();
void setFirst_name(string first_name);
string getLast_name();
void setLast_name(string last_name);
string getName();
void setName(string name);
string getHometownID();
void setHometownID(string hometownID);
string getHometownName();
void setHometownName(string hometownName);
string getUserID();
void setUserID(string userID);
int compare(Person& p, int criteria);
//Comparisons
friend bool operator== (Person p1, Person p2);
friend bool operator!= (Person &p1, Person &p2);
friend bool operator> (Person &p1, Person &p2);
friend bool operator>= (Person &p1, Person &p2);
friend bool operator< (Person &p1, Person &p2);
friend bool operator<= (Person &p1, Person &p2);
friend ostream& operator<<(ostream& os, const Person& p);
};
#endif
Person.cpp: I've cut this down quite a bit. I currently don't have anything having to do with Node within this.
#include "Person.h"
#include <sstream>
#include <string>
using namespace std;
Person::Person(){
birthdate = "";
name = "";
hometownID = "";
hometownName = "";
userID = "";
}
Person::Person(string birthdate, string first_name, string last_name, string hometownID, string hometownName, string userID){
this->birthdate = birthdate;
this->first_name = first_name;
this->last_name = last_name;
this->hometownID = hometownID;
this->hometownName = hometownName;
this->userID = userID;
name = last_name+ ", " +first_name;
}
//mostly get/set methods after here, nothing having to do with node.
main.cpp:
#include "arrayList.cpp"
#include "block_allocator.h"
#include "chain.cpp"
#include "chainNode.h"
#include "json.h"
#include "linearList.h"
#include "Person.h"
#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <time.h>
using namespace std;
//ArrayList helper methods
arrayList<Person> arrayListStructure(string fileName);
int arrayListSort(int criteria, arrayList<Person>* list);
void arrayListReverseSort(int criteria, arrayList<Person>* list);
int arrayListFlip(arrayList<Person>* list);
//Chain helper methods
chain<Person> chainStructure(string fileName);
//Hashtable helper methods
int main(int argc, const char * argv[]){
//get fileName
cout << "Please enter a filename:" << endl;
string fileName;
cin >> fileName;
//get data structure type
cout << "Please choose a data structure:" << endl;
cout << " 1. Arraylist" << endl;
cout << " 2. Chain" << endl;
cout << " 3. Hashtable" << endl;
int dataStructure;
cin >> dataStructure;
cout << "Please choose a criteria:" << endl;
cout << " 1. Name" << endl;
cout << " 2. Birthday" << endl;
cout << " 3. Location" << endl;
int criteria;
cin >> criteria;
arrayList<Person> friends;
chain<Person> friendChain;
//parse file into data structure
switch(dataStructure){
case 1: //Arraylist
//edited out, irrelevant
/*case 2:
//Chain
//code goes here
/*switch(criteria){
case 1: //Name
case 2: //Birthday
case 3: //Location
}
break;*/
/*case 3:
//Hashtable
//code goes here
break;
*/
}
}
//Helper methods for chain
chain<Person> chainStructure(string fileName){
//create initial (empty) chain
chain<Person> friends;
//open input file
ifstream fileInput(fileName);
//turn input stream into string
string inputStr((istreambuf_iterator<char>(fileInput)), istreambuf_iterator<char>());
//parse file content into json object
char *errorPos = 0;
char *errorDesc = 0;
int errorLine = 0;
block_allocator allocator(1 << 10);
json_value *root = json_parse(const_cast<char*>(inputStr.c_str()), &errorPos, &errorDesc, &errorLine, &allocator);
//Take value of first element
json_value *list = root->first_child;
//Outer loop addresses each friend's JSON object
for(json_value *it = list->first_child; it; it = it->next_sibling){
string first_name, last_name, birthdate, hometownID, hometownName, userID;
//Inner loop looks at each key/value pair within each friend object
for(json_value *friendKeys = it->first_child; friendKeys; friendKeys = friendKeys->next_sibling){
//grab first name
if(!string(friendKeys->name).compare("first_name")){
first_name = friendKeys->string_value;
}
//grab last name
else if(!string(friendKeys->name).compare("last_name")){
last_name = friendKeys->string_value;
}
//grab birthday and trim to 5 characters
else if(!string(friendKeys->name).compare("birthday")){
birthdate = friendKeys->string_value;
birthdate = birthdate.substr(0, 5);
}
//grab hometown info
else if(!string(friendKeys->name).compare("hometown")){
for(json_value *hometownKeys = friendKeys->first_child; hometownKeys; hometownKeys = hometownKeys->next_sibling){
if(!string(hometownKeys->name).compare("id")){
hometownID = hometownKeys->string_value;
}
if(!string(hometownKeys->name).compare("name")){
hometownName = hometownKeys->string_value;
}
}
}
//grab userID
else if(!string(friendKeys->name).compare("id")){
userID = friendKeys->string_value;
}
}
if(birthdate != "" && first_name != "" && last_name != "" && hometownID != "" && hometownName != "" && userID != ""){
//Create new Person in chain
Person person(birthdate, first_name, last_name, hometownID, hometownName, userID);
friends.insert(friends.size(), person);
}
}
//return friends;
return friends;
}
//Helper methods for hashtable
So I know this is a huge wall of text, but I'm really not sure where this disconnect is, and I didn't want to provide too little information. Any help or advice would be greatly appreciated, as I'm very new to C++, and even more inexperienced with using the template system.
EDIT: linearList.h:
#ifndef linearList_h
#define linearList_h
#include <iostream>
using namespace std;
template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
// return true iff list is empty
virtual int size() const = 0;
// return number of elements in list
virtual T& get(int theIndex) const = 0;
// return element whose index is theIndex
virtual int indexOf(const T& theElement) const = 0;
// return index of first occurence of theElement
virtual void erase(int theIndex) = 0;
// remove the element whose index is theIndex
virtual void insert(int theIndex, const T& theElement) = 0;
// insert theElement so that its index is theIndex
virtual void output(ostream& out) const = 0;
// insert list into stream out
};
#endif
In chain.h, the chain<T> template has this for a member:
chain<T>* firstNode;
I'm pretty sure that should be:
chainNode<T>* firstNode;
There may be other errors (or maybe not), but that appears the likely one causing your current issue that is the subject of this question.
Side Bar: This thing needs a serious refactor to use the containers and algorithms from the standard library (std::vector<T>, std::list<T>, etc...) Just consider it.