Ach jo jsem tu zas, tohle je fakt krize. Přetížit operátor << umim, a pro jistotu sem to kontroloval i podle knížky. Ale kompilátor prostě hlásí chybu. Když v main() nic není, je to v pohodě, jakmile tam založím objekt, třeba Stock gastro; hlásí to tu chybu. Pro jistotu to sem házím celý, ale fakt nevim co se mu nelíbí ani cot o píše za blbosti.
stock2.h:
#ifndef _STOCK2_H_
#define _STOCK2_H_
class Stock
{
private:
char * company;
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock();
Stock(const char * co, int n, double pr);
~Stock() { delete [] company; }
void buy(int num, double price);
void sell(int num, double price);
void update(double price);
void show() const;
const Stock & topval(const Stock & s) const;
friend ostream & operator<<(ostream & os, const Stock & st);
};
#endif
stock2.cpp
#include<iostream>
using namespace std;
#include<cstdlib>
#include<cstring>
#include"stock2.h"
Stock::Stock()
{
company = new char[11];
company = "spolecnost";
shares = 0;
share_val=0.0;
total_val=0.0;
}
Stock::Stock(const char * co, int n, double pr)
{
int len = strlen(co);
company = new char[len+1];
strcpy(company, co);
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(int num, double price)
{
shares+=num;
share_val = price;
set_tot();
}
void Stock::sell(int num, double price)
{
if(num>shares)
{
cerr<<"Nemuzete prodat vic nez mate!\n";
exit(1);
}
shares -=num;
share_val = price;
set_tot();
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
/* void Stock::show() const
{
cout << "Spolecnost: " << company
<< "Pocet akcii: " << shares <<"\n";
cout << "Cena akcie: $" << share_val
<< "Celkova cena: $"<< total_val << "\n";
} */
const Stock & Stock::topval(const Stock & s) const
{
if(s.total_val > total_val)
return s;
else
return *this;
}
ostream & operator<<(ostream & os, const Stock & st)
{
os << st.company;
return os;
}
main - funguje
#include <iostream>
#include "stock2.h"
using namespace std;
int main()
{
system("PAUSE");
return 0;
}
main - nefunguje
#include <iostream>
#include "stock2.h"
using namespace std;
int main()
{
Stock gastro;
system("PAUSE");
return 0;
}
Hlásí to tohle:
2 C:\DATA\C++\mistrovstvi-37.cpp In file included from mistrovstvi-37.cpp
21 C:\DATA\C++\stock2.h ISO C++ forbids declaration of `ostream' with no type
21 C:\DATA\C++\stock2.h `ostream' is neither function nor member function; cannot be declared friend
21 C:\DATA\C++\stock2.h expected `;' before '&' token
C:\DATA\C++\Makefile.win [Build Error] [mistrovstvi-37.o] Error 1
Díky moc za rady, ...ale přečtu si to nejspíš až v pondělí. Jdů domůůů ... víííííkend ... jupí ;)