Program Eror!

I have this error in visual studio, and i dont know what does mean
Link for picture for the error https://ibb.co/FswnRmj

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <ostream>
#include <string>
#include <istream>
class Stock {
	
	int code;
	std::string name;
	double valuePerUnit;
	int noUnits;
public:
	Stock();
		
	Stock(int c, std::string n, double v, int nu);
		
	void setCode(int c);
		
	void setName(std::string n);
		
	void setValuePerUnit(double v);
		
	void setNoUnits(int n);
		
	int getCode();
		
	std::string getName();
		
	double getValuePerUnit();
		
	double getNoUnits();
		
	double calcAmount();

	friend std::ostream & operator<<(std::ostream &out, const Stock &s);
		
	friend std::istream &operator>>(std::istream &in, Stock& s);


};


#include "Stock.h"
#include <iostream>

Stock::Stock() {
	code = 0;
	name = "";
	valuePerUnit = 1;
	noUnits = 1;
}
Stock::Stock(int c, std::string n, double v, int nu)
{
	code = c;
	name = n;
	valuePerUnit = v;
	noUnits = nu;
}

void Stock::setCode(int c)
{
	code = c;
}

void Stock::setName(std::string n)
{
	name = n;
}

void Stock::setValuePerUnit(double v)
{
	valuePerUnit = v;
}

void Stock::setNoUnits(int n)
{
	noUnits = n;
}

int Stock::getCode()
{
	return code;
}

std::string Stock::getName()
{
	return name;
}

double Stock::getValuePerUnit()
{
	return valuePerUnit;;
}

double Stock::getNoUnits()
{
	return noUnits;;
}

double Stock::calcAmount()
{
	return getNoUnits() * getValuePerUnit();
}

std::ostream& operator << (std::ostream &out,  Stock &s)
{
	out << "\nCode : " << s.getCode();
	out << "\nName : " << s.getName();
	out << "\nNumber Of Units : " << s.getNoUnits();
	out << "\nCost of Unit : " << s.getValuePerUnit();
	out << "\nTotal : " << s.calcAmount() << std::endl;
	
	return out;
}

std::istream& operator >> (std::istream& in, Stock& s)
{
	std::cout<<"Enter Stock Code ";
	in>>s.code;
	
	std::cout<<"Enter Stock Name ";
	in>>s.name;
	
	std::cout<<"Enter value per Unit : ";
	in>>s.valuePerUnit;

	std::cout<<"Enter Number of Units : ";
	in>>s.noUnits;
	
	return in;
}





Last edited on
This is your friend declaration:
friend std::ostream & operator<<(std::ostream &out, const Stock &s);

This is your implementation of said function:
1
2
3
4
std::ostream& operator << (std::ostream &out,  Stock &s)
{
	...
}

Notice, your implementation's function signature is not const-correct, but your declaration is (const Stock &s vs. Stock &s).

Every member function that does not modify data should have a const qualifier on it.

So, for example, it should be
int getCode() const;

1
2
3
4
int Stock::getCode() const
{
    return code;
}


Do this for all your get* functions, and calcAmount.
Last edited on
Topic archived. No new replies allowed.