Re: Stroustrup 5.5 References
On Mar 28, 2:28 pm, Markus Moll <markus.m...@esat.kuleuven.be> wrote:
arnuld wrote:
Well, it's all in the error messages...
yes, it is always in the error messages but nearly all of them
reporting something unrelated.
for(std::vector<Pair>::const_iterator p=pairs_list.begin(); p !=
pairs_list.end(); ++p)
{
std::cout << p->name
Here, you try to access a Pair, but the definition of Pair is still
unknown, thus:
OK, from now on every function definition will come above "main()" and
i will always define "main()" in the end. BTW, i got confused because
of "invalid use....". compiler could have said "undefind struct"
simply
now i got a new compiler, its name is "Markus" ;-)
<< std::endl;
}
return 0;
}
5.5_references.cpp: At global scope:
i did not get it.
double& get_value(const std::string& s)
5.5_references.cpp:54: error: new types may not be defined in a return type
5.5_references.cpp:54: note: (perhaps a semicolon is missing after the
definition of 'Pair')
Indeed, your definition of Pair is missing a semicolon. (Notice that you
could e.g. define a Pair-object p by "struct Pair { [...] } p;". The
same is _not_ allowed for functions, but to the compiler, it looks like
that is what you want to do: struct Pair { [...] } [...] get_value(const
std::string& s) { [...] })
this mistake is a new thing for me.
And furthermore, it looks like you give two return types, Pair and
double&, so...
5.5_references.cpp:54: error: two or more data types in declaration of
'get_value'
NO, it is always "double&", in both "declaration" and "definition".
even the "return expression" in the definition of "get_value" is a
"double". i never do such mistakes.
Of course, this differs from the previous declaration only in the return
type and you cannot overload functions on return types:
it is not.
/* maintains aset of Pairs, if "s" is found
returns its corresponding value */
const int my_val = 0;
for(int i=0; i < pair_list.size(); i++)
^^^^^^^^^
Wait, it was pair!s!_list before! What is pair_list?
aaaargh... IDIOT me.
Remember: the compiler is confused about the return type of get_value!
i am not using any different return types. i dont have any idea on why
it is reporting this error. i have checked twice. i did not make any
correction in return types.
hth
Yep :-)
i corrected the programe with "Markus compiler" ;-) and it runs now,
flawlessly. though i did not understand one correction:
---------- PROGRAMME ------------
/* Stroustrup, 5.5 references
STATEMENT:
the basic idea is that a "string" has an associated floating-point
value
associated with it, wrapped in a "Struct" named "Pair"."pairs_list"
is
a "Vector" of different "Pair(s)"
then we define a function named "get_value()" which keeps a "Pair"
in "pair_list" for each different string that has been given to it
as input. If the "input" matches with some "string" already present,
then the value corresponding to that "string" is returned, otherwise
that string is added with corresponding value 0 (zero) and 0 is
returned.
in "main()", we aks of ro input and just print the whole structure
out
*/
#include<iostream>
#include<string>
#include<vector>
struct Pair;
double& get_value(const std::string&);
std::vector<Pair> pairs_list;
struct Pair
{
std::string name;
double val;
};
int main()
{
std::string get_name;
while(std::cin >> get_name)
get_value(get_name)++;
for(std::vector<Pair>::const_iterator p=pairs_list.begin(); p !=
pairs_list.end(); ++p)
{
std::cout << p->name
<< " : "
<< p->val
<< std::endl;
}
return 0;
}
double& get_value(const std::string& s)
{
/* maintains aset of Pairs, if "s" is found
returns its corresponding value */
const int my_val = 0;
for(unsigned int i=0; i < pairs_list.size(); i++)
if(s == pairs_list[i].name)
return pairs_list[i].val;
Pair p = { s, my_val };
pairs_list.push_back(p); // adds pair at the end
return pairs_list[pairs_list.size() - 1].val; // actually return 0
(zero)
}
------ OUTPUT ------------
[arch@voodo tc++pl]$ ./a.out
like
and this
and
oh
jk
oj
like : 1
and : 2
this : 1
oh : 1
jk : 1
oj : 1
[arch@voodo tc++pl]$
----------------------------
notice this line:
for(unsigned int i=0; i < pairs_list.size(); i++)
i *had* to use "unsigned int i=0" rather than my usual "int i=0",
because the "g++" was giving out this error:
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra
5.5_references.cpp
5.5_references.cpp: In function 'double& get_value(const
std::string&)':
5.5_references.cpp:63: warning: comparison between signed and unsigned
integer expressions
[arch@voodo tc++pl]$ clear
it means most of the time, when i will use "for" i have to use
"unsigned int"
??