Can't figure out error

This is my code
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <stdlib.h>
#include <math.h>

#define n 10 //pososto apo to arxiko emvado
#define m 10 //xrimata gia kathe t.m. se periptwsh pou meta thn meiwsh tou emvadou menoun ligotera apo 100 t.m.
#define y 10 //xrimata gia kathe metro pou vlepei sth thalassa

using namespace std;

int i;

class site
{
    char name[30];
    int code, vertices, **coordinates;
    int plus; //posa t.m. gia na sumplirwthoun 100 wste na vroume to poso pou tha katavallei o idioktitis
    site();
public:
    float area(); //emvado
    float decrease(); //meiwsh arxikou emvadou
    int money(); //poso pou prepei na katavallei o idioktitis
    void print(); //ektupwsh stoixeiwn
};

site::site()
{
    cout<<"Enter name, code and vertices:",'\n';
    cin>>name, code, vertices;
    coordinates=(int**)malloc(2*sizeof(int)); //mnhmh gia 2 grammes
    for (i=0; i<2;i++)
    {
        coordinates[i]=(int*)malloc(vertices*sizeof(int)); //mnhmh gia 10 kelia se kathe grammh
    }
    for (i=0; i<vertices; i++)
    {
        cout<<"Enter coordinates x, y of "<<i<<" vertice",'\n';
        cin>>coordinates[0][i]>>coordinates[1][i]; //prwth grammh x, defterh grammh y
    }
}

float site::area()
{
    float emvado=0;
    for (i=0;i<vertices;i++)
    {
        emvado=emvado+((coordinates[0][i+1]-coordinates[0][i])*(coordinates[1][i+1]+coordinates[1][i]))/2;
    }
    return emvado;
}

float site::decrease()
{
    float decrease;
    plus=0;
    decrease=area()-n*area();
    if (decrease<100)
    {
        do
        {
            decrease++;
            plus++;
        }
        while (decrease<100);
    }
    return decrease;
}

int site::money()
{
    int money;
    money=m*plus;
    return money;
}

void site::print()
{
    cout<<"Site stats: \n"<<name<<"\n"<<code<<"\n"<<vertices<<"\n"<<area<<"\n";
    cout<<"Area remaining: \n"<<area()-decrease()<<"\n";
    cout<<"To be paid: "<<money<<"\n";
}

class sea_site
{
    char name[30];
    int code, vertices, s_vertices, **coordinates, **coordinates_s;
    int plus;
    sea_site();
public:
    float area(); //emvado
    float decrease(); //meiwsh arxikou emvadou
    int money(); //poso pou prepei na katavallei o idioktitis
    void print(); //ektupwsh stoixeiwn
    float mhkos();
};

sea_site::sea_site()
{
    cout<<"Enter name, code vertices and sea vertices:",'\n';
    cin>>name>>code>>vertices>>s_vertices;
    coordinates=(int**)malloc(2*sizeof(int)); //mnhmh gia 2 grammes
    coordinates_s=(int**)malloc(2*sizeof(int)); //mnhmh gia suntetagmenes twn korufwn pou vlepoun sth thalassa wste na vroume to mhkos
    for (i=0; i<2;i++)
    {
        coordinates[i]=(int*)malloc(vertices*sizeof(int)); //mnhmh gia "vertices" kelia se kathe grammh
        coordinates_s[i]=(int*)malloc(s_vertices*sizeof(int));
    }
    for (i=0; i<vertices; i++)
    {
        cout<<"Enter coordinates x, y of "<<i<<" vertice",'\n';
        cin>>coordinates[0][i]>>coordinates[1][i]; //prwth grammh x, defterh grammh y
    }
    for (i=0; i<s_vertices; i++)
    {
        cout<<"Enter coordinates x, y of"<<i<<" sea vertice \n";
        cin>>coordinates_s[0][i]>>coordinates_s[1][i];
    }
}

float sea_site::area()
{
    float emvado=0;
    for (i=0;i<vertices;i++)
    {
        emvado=emvado+((coordinates[0][i+1]-coordinates[0][i])*(coordinates[1][i+1]+coordinates[1][i]))/2;
    }
    return emvado;
}

float sea_site::decrease()
{
    float decrease;
    plus=0;
    decrease=area()-n*area();
    if (decrease<100)
    {
        do
        {
            decrease++;
            plus++;
        }
        while (decrease<100);
    }
    return decrease;
}

int sea_site::money()
{
    int money;
    money=m*plus+y*mhkos();
    return money;
}

void sea_site::print()
{
    cout<<"Site stats: \n"<<name<<"\n"<<code<<"\n"<<vertices<<"\n"<<area<<"\n";
    cout<<"Area remaining: \n"<<area()-decrease()<<"\n";
    cout<<"To be paid: "<<money<<"\n";
}

float sea_site::mhkos()
{
    float mhkos=0;
    for (i=0; i<s_vertices; i++)
    {
        mhkos=mhkos+sqrt((coordinates_s[0][i+1]-coordinates_s[0][i])*(coordinates_s[0][i+1]-coordinates_s[0][i])+((coordinates_s[1][i+1]-coordinates_s[1][i])*(coordinates_s[1][i+1]-coordinates_s[1][i])));
    }
    return mhkos;
}

void calk(site_n, site_t, sea_site_n, sea_site_t, n, m, y, *total_m, *total_l) //arithmos oikopedwn, pinakas site, arithmos parathalassiwn oikopedwn, pinakas parathalassiwn, pososto x3, sunoliko xrimata, sunolikh gh
{
    &total_m=0;
    &total_l=0;
    for (i=0; i<site_n; i++)
    {
        &total_m=&total_m+site_t[i].decrease();
        &total_l=&total_l+site_t[i].money();
    }
    for (i=0; i<sea_site_n; i++)
    {
        &total_m=&total_m+sea_site_t[i].decrease();
        &total_l=&total_l+sea_site_l[i].money();
    }

}

int main()
{
    int site_n, site_sea_n, *total_m, *total_l;
    site *site_t;
    sea_site *sea_site_t;
    cout<<"Enter number of sites and sites by the sea: \n";
    cin>>site_n>>site_sea_n
    site_t=(site*)malloc(site_n*sizeof(site));
    sea_site_t=(sea_site*)malloc(sea_site*sizeof(sea_site));
    calk(site_n, site_t, site_sea_n, site_sea_t, n, m, y, *total_m, *total_l);
    for (i=0; i<site_n; i++)
    {
        site_t[i].print();
    }
    for(i=0; i<sea_site_n; i++)
    {
        sea_site_t[i].print();
    }
}





And these are the errors/notes I am getting:

Many of these:
"c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|108|note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|"

An error saying that the variables in "calk(...)" are not declared in that scope.

Pay no attention to the comments and some of the variables' names as they are in "greeklish".
This is unrelated to your problem, but don't use malloc/free. new[] and delete[] are better.

Also you're mallocing wrong:

coordinates=(int**)malloc(2*sizeof(int)); //mnhmh gia 2 grammes that should probably be sizeof(int*) -- but this is somewhat moot because again you shouldn't even be using malloc at all.

Also you're not freeing stuff you malloc (memory leaks!)

Anyway this error you posted:

"c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\ostream|108|note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]|"


This is not the actual error, it's a followup note about the error. The actual error is probably "XXX call is ambiguous". Please repost the actual error.

An error saying that the variables in "calk(...)" are not declared in that scope


Don't paraphrase. Paste the actual error. You're leaving out very critical details when you paraphrase (line number, the actual name of the variable that isn't declared, etc)
Thank you for reply. I am using malloc because that's what I am asked to do.
As for the rest the build log is too big to post here so I am just going to link it.
http://hotfile.com/dl/81105052/5e05aaa/build_log.txt.html
Hope you can make something out of it.
I am using malloc because that's what I am asked to do


Whoever is asking you to use it is wrong. malloc has no place in C++.

If this is for a class or something and the teacher requires you use it, then I guess you have to do it to get through the class -- but BLECH!

Anyway... this is the actual error:

Others\Programmatistikes Texnikes\ergasiab.cpp|78|error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_tr|]


"no match for operator <<" is the key part. Looking at the line it's talking about:

cout<<"Site stats: \n"<<name<<"\n"<<code<<"\n"<<vertices<<"\n"<<area<<"\n";

You're trying to print area but area is a function name.

You probably meant to output area().


Line 80 has the same problem: cout<<"To be paid: "<<money<<"\n"; Probably should be printing money(), not money.

Same thing on line 156


Line 171 is missing types for all the variables:

1
2
3
void calk(site_n, site_t, sea_site_n, ...etc...) // bad

void calk(int site_n,int site_t, int sea_site_n, ...etc...)  // good 


Also note n, m, and y are macros so you can't have them as parameter names or it will screw up (the macros are polluting your namespace). Change this:

1
2
3
4
5
6
7
// #define n 10  // EVIL, BAD
// #define m 10 // bad
// #define y 10 // bad

static const int n = 10;  // good
static const int m = 10;  // good
static const int y = 10;  // good 
Thank you so much for your time. It's the first time I am asked to write a code with more than 60-70 lines and I guess it's not as easy finding out errors.
The code is for a class indeed and the teacher clearly states that we have to use malloc, I have no idea why. I should probably ask him why he insists so much.
Tell him that you have to use new/delete for complex types because they call ctors and dtors and malloc/free don't. Then tell him that mixing malloc with new in the same program is inconsistent, adds confusion, and makes it possible that the user will accidentally mix and match incorrectly (ie: free something that was allocated with new, which would be very bad).

So the best/easiest way to avoid those problem is just to use new/delete all the time and forget about malloc/free entirely. malloc doesn't offer anything special.


Also note you have tons of memory leaks. It doesn't look like you're calling free at all, so every call to malloc leaks memory.
Last edited on
Topic archived. No new replies allowed.