So many errors

Pages: 12
#include <iostream>
#include <string>
using namespace std;

struct libro{
string nome;
int anno;
};

struct libreria{
libro L[100];
int n;
};

void Carica(libreria &o){
for (int i=0;i<o.n;i++){
cout<<"Inserisci nome libro: ";
cin>>o.L[i].nome;
cout<<"Inserisci anno libro: ";
cin>>o.L[i].anno;
}
}

void Mostra(libreria &o){
for (int i=0;i<o.n;i++){
cout<<"Nome del libro: "<<o.L[i].nome<<" ("<<o.L[i].anno<<")"<<endl;

}
}

void Elimina(libreria &o,int x){
for(int i=0;i<o.n;i++){
if(o.L[i]==x){
for(int j=i;j<o.n;j++){
libro b=o.L[j];
o.L[j]=o.L[j+1];
o.L[j+1]=b;
}
i--;
o.n--;
}
}

}

void Ordinamento(libreria &o){
bool scambio=true;
while((o.n>1)and(scambio=true)){
scambio=false;
int i=0;
while(i<o.n-1){
if(o.L[i].anno>o.L[i+1].anno){
libro y=o.L[i];
o.L[i]=o.L[i+1];
o.L[i+1]=y;
scambio=true;
}
else if((o.L[i].anno==o.L[i+1].anno)and(o.L[i].nome>o.L[i+1].nome)){
libro y=o.L[i];
o.L[i]=o.L[i+1];
o.L[i+1]=y;
scambio=true;
}
i--;
}
o.n--;
}
}

int Ricerca(libreria &o,int s,int d,int y){
if(s<d){
int m=(s+d)/2;
if(o.L[m]==y){
return m;
}
else if(o.L[m]<y){
return Ricerca(&o,m+1,d,y);
}
else{
return Ricerca(&o,s,m-1,y);
}
}
else{
return -1;
}
}

int main(){

libreria b;
cout<<"Numero di libri da inserire: ";
cin>>b.n;

Carica(b);

Ordinamento(b);

int x;
cout<<"Elemento da eliminare: ";
cin>>x;

Elimina(b,x);

int y;
cout<<"Elemento da ricercare: ";
cin>>y;

int n;
Ricerca(b,0,n,y);

Mostra(b);

return 0;
}
Last edited on
When someone gets a long list of error messages my standard response is to start with the first one, fix it, recompile and repeat. You probably don't have as many real errors that you need to fix as is listed because often errors cause other errors. That's why you should start with the first so that you know it's a real error.
Last edited on
I have these errors and I don't know how to fix

33 [Error] no match for 'operator==' (operand types are 'libro' and 'int')

73 [Error] no match for 'operator==' (operand types are 'libro' and 'int')

76 [Error] no match for 'operator<' (operand types are 'libro' and 'int')

77 [Error] invalid initialization of non-const reference of type 'libreria&' from an rvalue of type 'libreria*'

80 [Error] invalid initialization of non-const reference of type 'libreria&' from an rvalue of type 'libreria*'
Last edited on
Yes, but do you expect comparing an object of type libro with an object of type int to work?
it gives me the error even if I compare libro and libro
The compiler doesn't know what comparing two libro objects is supposed to do. For that to work you would have to overload the comparison operators for the libro type.
How can I overload?
1
2
3
bool operator==(const libro& l1, const libro& l2) {
    return l1.nome == l2.nome && l1.anno == l2.anno;
}


If you want to compare libro with an int then you need to specify what the comparison means. An example could be:
1
2
3
bool operator==(const libro& l1, int i) {
    return l1.anno == i;
}

And if you want to overload the other comparison operators !=, <, <=, > and >= (because you need them or for consistency) you do it in the same way (the expression need to be different of course).
Last edited on
Thanks, in this instead

83 [Error] invalid initialization of non-const reference of type 'libreria&' from an rvalue of type 'libreria*'
Remove the & in front of o when passing it to the Ricerca function.

1
2
return Ricerca(&o,m+1,d,y);
               ^


The & operator is used to get a pointer to the variable. The Ricerca function takes a reference (not a pointer) so then you should not use the & operator.
Last edited on
With C++20 you don't need to define != - as that is derived from ==

Also, if the equality operator for the same type is defined as a member function then it can just be defaulted:

1
2
3
4
5
6
struct libro{
    string nome;
    int anno;

    bool operator==(const libro&) const = default;
};

Also, if the equality operator for the same type is defined as a member function then it can just be defaulted

This assumes you want == to simply compare all the member variables using ==.
Last edited on
It gives me this errors

8 [Warning] defaulted and deleted functions only available with -std=c++11 or -std=gnu++11
8 [Error] 'bool libro::operator==(const libro&) const' cannot be defaulted
Last edited on
What version of C++ are you using? It looks like you're trying to compile as C++98??? What compiler are you using? Post your current code.
5.11
OK. v5.11 of which compiler? and post your current code as requested please.

Last edited on
I use Dev-C++ 5.11 and this is the code

#include <iostream>
#include <string>
using namespace std;

struct libro{
string nome;
int anno;
bool operator==(const libro&) const = default;
};

struct libreria{
libro L[100];
int n;
};

void Carica(libreria &o){
for (int i=0;i<o.n;i++){
cout<<"Inserisci nome libro: ";
cin>>o.L[i].nome;
cout<<"Inserisci anno libro: ";
cin>>o.L[i].anno;
}
}

void Mostra(libreria &o){
for (int i=0;i<o.n;i++){
cout<<"Nome del libro: "<<o.L[i].nome<<" ("<<o.L[i].anno<<")"<<endl;

}
}

void Elimina(libreria &o,int x){
for(int i=0;i<o.n;i++){
if(o.L[i]==x){
for(int j=i;j<o.n;j++){
libro b=o.L[j];
o.L[j]=o.L[j+1];
o.L[j+1]=b;
}
i--;
o.n--;
}
}

}

void Ordinamento(libreria &o){
bool scambio=true;
while((o.n>1)and(scambio=true)){
scambio=false;
int i=0;
while(i<o.n-1){
if(o.L[i].anno>o.L[i+1].anno){
libro y=o.L[i];
o.L[i]=o.L[i+1];
o.L[i+1]=y;
scambio=true;
}
else if((o.L[i].anno==o.L[i+1].anno)and(o.L[i].nome>o.L[i+1].nome)){
libro y=o.L[i];
o.L[i]=o.L[i+1];
o.L[i+1]=y;
scambio=true;
}
i--;
}
o.n--;
}
}

int Ricerca(libreria &o,int s,int d,int y){
if(s<d){
int m=(s+d)/2;
if(o.L[m]==y){
return m;
}
else if(o.L[m]<y){
return Ricerca(o,m+1,d,y);
}
else{
return Ricerca(o,s,m-1,y);
}
}
else{
return -1;
}
}

int main(){

libreria b;
cout<<"Numero di libri da inserire: ";
cin>>b.n;

Carica(b);

Ordinamento(b);

int x;
cout<<"Elemento da eliminare: ";
cin>>x;

Elimina(b,x);

int y;
cout<<"Elemento da ricercare: ";
cin>>y;

int n;
Ricerca(b,0,n,y);

Mostra(b);

return 0;
}
Change your compiler! That only uses gcc 4.9.2 from 2014 which is 'only' c++11 compliant when used with the right parameters. With one exception, this compiles OK with VS2022:

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
#include <iostream>
#include <string>
using namespace std;

struct libro {
	string nome;
	int anno;
	bool operator==(const libro&) const = default;
	bool operator==(int i) { return anno == i; }
	bool operator<(int i) { return anno < i; }
};

struct libreria {
	libro L[100];
	int n;
};

void Carica(libreria& o) {
	for (int i = 0; i < o.n; i++) {
		cout << "Inserisci nome libro: ";
		cin >> o.L[i].nome;
		cout << "Inserisci anno libro: ";
		cin >> o.L[i].anno;
	}
}

void Mostra(libreria& o) {
	for (int i = 0; i < o.n; i++) {
		cout << "Nome del libro: " << o.L[i].nome << " (" << o.L[i].anno << ")" << endl;
	}
}

void Elimina(libreria& o, int x) {
	for (int i = 0; i < o.n; i++) {
		if (o.L[i] == x) {
			for (int j = i; j < o.n; j++) {
				libro b = o.L[j];
				o.L[j] = o.L[j + 1];
				o.L[j + 1] = b;
			}
			i--;
			o.n--;
		}
	}

}

void Ordinamento(libreria& o) {
	bool scambio = true;
	while ((o.n > 1) and (scambio == true)) {
		scambio = false;
		int i = 0;
		while (i < o.n - 1) {
			if (o.L[i].anno > o.L[i + 1].anno) {
				libro y = o.L[i];
				o.L[i] = o.L[i + 1];
				o.L[i + 1] = y;
				scambio = true;
			} else if ((o.L[i].anno == o.L[i + 1].anno) and (o.L[i].nome > o.L[i + 1].nome)) {
				libro y = o.L[i];
				o.L[i] = o.L[i + 1];
				o.L[i + 1] = y;
				scambio = true;
			}
			i--;
		}
		o.n--;
	}
}

int Ricerca(libreria& o, int s, int d, int y) {
	if (s < d) {
		int m = (s + d) / 2;
		if (o.L[m] == y) {
			return m;
		} else if (o.L[m] < y) {
			return Ricerca(o, m + 1, d, y);
		} else {
			return Ricerca(o, s, m - 1, y);
		}
	} else {
		return -1;
	}
}

int main() {

	libreria b;
	cout << "Numero di libri da inserire: ";
	cin >> b.n;

	Carica(b);

	Ordinamento(b);

	int x;
	cout << "Elemento da eliminare: ";
	cin >> x;

	Elimina(b, x);

	int y;
	cout << "Elemento da ricercare: ";
	cin >> y;

	int n;
	Ricerca(b, 0, n, y);

	Mostra(b);

	return 0;
}


The remaining issue is that L107 uses n which is defined at L106 but not initialised before its' usage!
Last edited on
You mean Visual Studio 2022?
Pages: 12