Hallo iedereen,
Ik moet een programma schrijven voor school en dit vereist een klasse product. Het product heeft een barcode, prijs, btw-stelsel, beschrijving, minimum aantal staat voor het aantal dat minimum in stock mag zijn, en aanbod verwacht een boolean false of true (deze wordt gevraagd om te controleren of het product nog mag geleverd worden). In de product.cc moet de zo een product als output stream op 1 lijn teruggegeven worden, of al input ingelezen.
Ik test voorlopig al het product met een kleine main functie.
Om vanuit product.cc de functies uit de klasse in product.h te gebruiken, moet ik friend gebruiken, ik krijg echter veel foutmeldingen:
product.h:23: error: ISO C++ forbids declaration of `ostream' with no type
product.h:23: error: `ostream' is neither function nor member function; cannot be declared friend
product.h:23: error: expected `;' before "operator"
product.h:24: error: ISO C++ forbids declaration of `istream' with no type
product.h:24: error: `istream' is neither function nor member function; cannot be declared friend
product.h:24: error: expected `;' before "operator"
product.cc: In function `std:
stream operator<<(std:
stream&, Product)':
product.cc:9: error: `getBeschrijving' was not declared in this scope
product.cc: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/i486-slackware-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private
product.cc:10: error: within this context
product.cc: In function `std::istream operator>>(std::istream&, Product&)':
product.cc:19: error: expected initializer before "if"
product.cc:47: error: expected primary-expression before "else"
product.cc:47: error: expected `;' before "else"
Nu geef ik even mijn code mee:
product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include <string>
using std::string;
class Product{
public: Product(unsigned int barcode, unsigned int prijs, unsigned int btw, string beschrijving, unsigned int minimum, bool aanbod):
barcodeProduct(barcode),
prijsProduct(prijs),
btwProduct(btw),
beschrijvingProduct(beschrijving),
minimumAantal(minimum),
inAanbod(aanbod){
};
unsigned int getBarcode(){return barcodeProduct;}
unsigned int getPrijs(){return prijsProduct;}
unsigned int getBtw(){return btwProduct;}
string getBeschrijving(){return beschrijvingProduct;}
void setPrijs(unsigned int nieuwePrijs){
prijsProduct = nieuwePrijs;}
void setInaanbod(bool x){
inAanbod = x;}
friend ostream operator<<(ostream &os, Product p);
friend istream operator>>(istream &is, Product &p);
private: unsigned int barcodeProduct;
unsigned int prijsProduct, btwProduct;
string beschrijvingProduct;
unsigned int minimumAantal;
bool inAanbod;
};
#endif
product.cc
#include "product.h"
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
using std::string;
ostream operator<<(ostream &os, Product p){
os << p.getBarcode() << " " << p.getPrijs() << " " << p.getBtw() << " " << getBeschrijving();
return os;
}
istream operator>>(istream &is, Product &p){
unsigned int nieuweBarcode;
unsigned int nieuwePrijs, nieuweBtw;
string nieuwBeschrijving;
unsigned int nieuwMinimum;
bool nieuwAanbod
if(is){is >> nieuweBarcode;
if(is){is >> nieuwePrijs;
if(is){ is >> nieuweBtw;
if(is) {is >> nieuweBeschrijving;
if(is) {is >> nieuwMinimum;
if(is) {is >> nieuwAanbod;
p.barcodeProduct = nieuweBarcode;
p.prijsProduct = nieuwePrijs;
p.btwProduct = nieuweBtw;
p.beschrijvingProduct = nieuweBeschrijving;
p.minimumAantal = nieuwMinimum;
p.inAanbod = nieuwAanbod;
} else {cerr << "geen nieuwaanbod";
exit (1);
}
} else {cerr << "geen minimum";
exit (2);
}
}else {cerr<< "geen beschrijving";
exit(3);
}
} else {cerr<< "geen btw";
exit(4);
}
} else {cerr<< "geen prijs";
exit(5);
}
} else {cerr<< "geen barcode";
exit(6);
}
}
Ik moet een programma schrijven voor school en dit vereist een klasse product. Het product heeft een barcode, prijs, btw-stelsel, beschrijving, minimum aantal staat voor het aantal dat minimum in stock mag zijn, en aanbod verwacht een boolean false of true (deze wordt gevraagd om te controleren of het product nog mag geleverd worden). In de product.cc moet de zo een product als output stream op 1 lijn teruggegeven worden, of al input ingelezen.
Ik test voorlopig al het product met een kleine main functie.
Om vanuit product.cc de functies uit de klasse in product.h te gebruiken, moet ik friend gebruiken, ik krijg echter veel foutmeldingen:
product.h:23: error: ISO C++ forbids declaration of `ostream' with no type
product.h:23: error: `ostream' is neither function nor member function; cannot be declared friend
product.h:23: error: expected `;' before "operator"
product.h:24: error: ISO C++ forbids declaration of `istream' with no type
product.h:24: error: `istream' is neither function nor member function; cannot be declared friend
product.h:24: error: expected `;' before "operator"
product.cc: In function `std:


product.cc:9: error: `getBeschrijving' was not declared in this scope
product.cc: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/i486-slackware-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private
product.cc:10: error: within this context
product.cc: In function `std::istream operator>>(std::istream&, Product&)':
product.cc:19: error: expected initializer before "if"
product.cc:47: error: expected primary-expression before "else"
product.cc:47: error: expected `;' before "else"
Nu geef ik even mijn code mee:
product.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include <string>
using std::string;
class Product{
public: Product(unsigned int barcode, unsigned int prijs, unsigned int btw, string beschrijving, unsigned int minimum, bool aanbod):
barcodeProduct(barcode),
prijsProduct(prijs),
btwProduct(btw),
beschrijvingProduct(beschrijving),
minimumAantal(minimum),
inAanbod(aanbod){
};
unsigned int getBarcode(){return barcodeProduct;}
unsigned int getPrijs(){return prijsProduct;}
unsigned int getBtw(){return btwProduct;}
string getBeschrijving(){return beschrijvingProduct;}
void setPrijs(unsigned int nieuwePrijs){
prijsProduct = nieuwePrijs;}
void setInaanbod(bool x){
inAanbod = x;}
friend ostream operator<<(ostream &os, Product p);
friend istream operator>>(istream &is, Product &p);
private: unsigned int barcodeProduct;
unsigned int prijsProduct, btwProduct;
string beschrijvingProduct;
unsigned int minimumAantal;
bool inAanbod;
};
#endif
product.cc
#include "product.h"
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
using std::string;
ostream operator<<(ostream &os, Product p){
os << p.getBarcode() << " " << p.getPrijs() << " " << p.getBtw() << " " << getBeschrijving();
return os;
}
istream operator>>(istream &is, Product &p){
unsigned int nieuweBarcode;
unsigned int nieuwePrijs, nieuweBtw;
string nieuwBeschrijving;
unsigned int nieuwMinimum;
bool nieuwAanbod
if(is){is >> nieuweBarcode;
if(is){is >> nieuwePrijs;
if(is){ is >> nieuweBtw;
if(is) {is >> nieuweBeschrijving;
if(is) {is >> nieuwMinimum;
if(is) {is >> nieuwAanbod;
p.barcodeProduct = nieuweBarcode;
p.prijsProduct = nieuwePrijs;
p.btwProduct = nieuweBtw;
p.beschrijvingProduct = nieuweBeschrijving;
p.minimumAantal = nieuwMinimum;
p.inAanbod = nieuwAanbod;
} else {cerr << "geen nieuwaanbod";
exit (1);
}
} else {cerr << "geen minimum";
exit (2);
}
}else {cerr<< "geen beschrijving";
exit(3);
}
} else {cerr<< "geen btw";
exit(4);
}
} else {cerr<< "geen prijs";
exit(5);
}
} else {cerr<< "geen barcode";
exit(6);
}
}