ok.
you do not need 'new' here at all. That is for pointers and doing it yourself.
so your constructor is not necessary.
this works. I had to make several small changes, compare it to yours to see what I did.
As best as I remember it..
- you tried to use the class without objects in main (you need variables: a class is a TYPE and you can't just call methods, you need a variable of the TYPE then call methods on THAT)
- you had the new stuff, which was not correct
- you had print returning something, which it should not
- cleaned up the giant for loop
- your print was trying to print a class that did not overload << ... you can either overload << yourself or do as I did and print from a getter.
- your print is probably nothing like what you wanted now, but it should be an example of what you need to do.
- may be one or two other minor syntax issues
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include<iostream>
#include <list>
using namespace std;
class Item{
//Access specifer
public: //todo, private with get/set
string item;
int meshNum;
int mNum;
//constructor
public:
Item( string item,int mNum, int meshNum ){
this->item=item;
this-> mNum= mNum;
this-> meshNum= meshNum;
}
//Memeber functions
public:
string getItem(){
return item;
}
void setItem(string item){
this->item = item;
}
int getMeshNum(){
return this->meshNum;
}
void setMeshNum(int meshNum){
this->meshNum= meshNum;
}
int getMNum(){
return this->mNum;
}
void setMNum(int mNum){
this-> mNum= mNum;
}
};
//____________________________________________
class materialList{
// Access specifer
private:
list <Item> items;
//constructor
public:
/* materialList(){
this->items = new list<Item>;
} */
// Memeber fucntions
public:
void add(Item &item)
{
items.push_back(item);
}
//print my list
void Print()
{
cout<<"Working ";
for (auto &i : items)
cout << i.getItem() << " ";
cout << endl;
cout<<"Print done ";
}
};
int main(){
Item test= Item("door",34,50);
//itemList =
materialList ml; //(list<Item> test);
ml.add(test);
ml.Print();
}
| |