Help with classes
Jun 17, 2014 at 4:39am UTC
I am getting a long error when I try and compile my multiset.cpp file. Something about the vector I am using. Error looks as follows:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/vector:62:0,
from multiset.h:5,
from multiset.cpp:1:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Item; _Args = {}]’:
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_uninitialized.h:495:43: required from ‘static void std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Item*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/stl_uninitialized.h:544:33: required from ‘void std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Item*; _Size = long unsigned int]’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
//multiset.h file
#ifndef MULTISET_H_
#define MULTISET_H_
#include "item.h"
#include <vector>
using namespace std;
class Multiset{
Multiset();
private :
vector<Item> items;
public :
void add_item(string name, int quantity);
void remove_item(string name);
};
#endif
//multiset.cpp file
#include "multiset.h"
#include <vector>
using namespace std;
Multiset::Multiset(){
items.resize(0);
}
void Multiset::add_item(string name, int quantity){
items.push_back(Item(name, quantity));
}
void Multiset::remove_item(string name){
for (int i = 0; i< items.size(); i++){
if ( name == items[i].get_name()){
items.erase(items.begin());
}
}
}
Jun 17, 2014 at 5:35am UTC
Post the definition of Item.
Jun 17, 2014 at 5:38am UTC
Here it is. Thanks for any help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
//item.h
#ifndef ITEM_H_
#define ITEM_H_
class Item{
//Item(string name, int quantity);
private :
std::string name;
int quantity;
public :
Item(std::string name, int quantity);
std::string get_name();
int get_quantity();
};
#endif
//item.cpp
#include <vector>
#include <string>
#include "item.h"
Item::Item(std::string name, int quantity){
this ->name = name;
this ->quantity = quantity;
}
std::string Item::get_name(){
return name;
}
int Item::get_quantity(){
return quantity;
}
Jun 17, 2014 at 5:40am UTC
I have been messing around with the code a bit after doing some reading so the multiset files have changed as well but I still get same error. I think it is an include problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#ifndef MULTISET_H_
#define MULTISET_H_
#include "item.h"
class Multiset{
Multiset();
private :
std::vector<Item> items;
public :
void add_item(std::string name, int quantity);
void remove_item(std::string name);
};
#endif
#include <vector>
#include <string>
#include "multiset.h"
using namespace std;
Multiset::Multiset(){
items.resize(0);
}
void Multiset::add_item(string name, int quantity){
items.push_back(Item(name, quantity));
}
void Multiset::remove_item(string name){
for (int i = 0; i< items.size(); i++){
if ( name == items[i].get_name()){
items.erase(items.begin());
}
}
}
Jun 17, 2014 at 5:44am UTC
std::vector<T> needs T to have a default constructor. Your definition of Item::Item(std::string, int) causes the compiler to not generate one automatically, you'll have to define one yourself. Something like simply Item(){} will work.
Jun 17, 2014 at 5:44am UTC
You need to #include <string>
in Item.h , although I don't know if that's your only problem.
Topic archived. No new replies allowed.