Ovakav kod ali sa vektorom umjesto liste, radi perfektno. Ovaj izbacuje hrpu errora, u cemu je problem? (sortiranje listi opcenito nisam skuzio, pa ako netko moze neki primjer napisati)
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <list>
using namespace std;
bool ascending(int x, int y);
bool descending(int x, int y);
void output(list<int>my_list);
int main()
{
list<int> my_list;
int value;
while(1)
{
cout << "Enter a positive number or -1 to quit:" << endl;
cin >> value;
if(value==-1)
break;
else
my_list.push_back(value);
}
system("CLS");
cout << "You have entered the following numbers:" << endl;
output(my_list);
cout << "After sorting:" << endl;
sort(my_list.begin(), my_list.end(), ascending);
output(my_list);
system("pause");
return 0;
}
bool ascending(int x, int y)
{
return x < y;
}
bool descending(int x, int y)
{
return x > y;
}
void output(list<int>my_list)
{
for(list<int>::iterator it=my_list.begin(); it != my_list.end(); it++)
{
cout << *it <<"\t";
}
cout << endl;
}
Radio sam po ovome http://www.learncpp.com/cpp-tutorial/813-friend-functions-and-classes/ ali sam ocito zeznuo negdje. Na pocetku koda je napisan zadatak.
/* Product Inventory Project - Create an application which manages an inventory
of products. Create a product class which has a price, id, and quantity on
hand. Then create an inventory class which keeps track of various products
and can sum up the inventory value. */
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
class Product
{
private:
string name;
float price;
int ID;
int quantity;
public:
Product (string name, float price, int ID, int quantity)
{
this->name = name;
this->price = price;
this->ID = ID;
this->quantity = quantity;
}
string get_name() {return name;}
float get_price() {return price;}
int get_ID() {return ID;}
int get_quantity() {return quantity;}
friend class Inventory;
};
class Inventory
{
public:
Inventory() { cout << "Building an Inventory." << endl; }
~Inventory() { cout << "Destroying an Inventory." << endl; }
Product access;
void get_data(Product &access)
{
cout << access.get_ID() <<" " << access.get_name() << " @ " <<
access.get_price() << " " << access.get_quantity() << endl;
}
};
//---------------------------------------------------------------------------//
int main()
{
vector<Product> my_vector;
string name;
float price;
int ID;
int quantity;
char input;
while(1)
{
cout << "Enter the name of the product:" << endl;
getline(cin, name);
cout << "Enter " << name << " price:" << endl;
cin >> price;
cout << "Enter " << name << " ID number: " << endl;
cin >> ID;
cout << "Enter " << name << " quantity:" << endl;
cin >> quantity;
Product temp(name, price, ID, quantity);
my_vector.push_back(temp);
cout << "Enter another product <Y/N>?" << endl;
cin >> input;
if(input == 'n' || input == 'N')
break;
}
Inventory my_inventory;
for(vector<Product>::iterator it=my_vector.begin(); it != my_vector.end(); it++)
{
my_inventory.get_data(access);
}
system("pause");
return 0;
}
Ovo su greske koje javlja:
In constructor `Inventory::Inventory()' no matching function for call to `Product::Product()'
In function `int main()' `access' undeclared (first use this function)
Hvala.



HUGE RESPECT: woodgamesfx, captain_soap_McTawish, rustweaver, royalhero, tracerCPP,Indestructible