Stroustrup 5.5 References

A

arnuld

this i the example programme. it has many errors, i am not able to
make anything out it:

---------- 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;



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->value
<< std::endl;
}

return 0;
}

struct Pair
{
std::string name;
double val;
}

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(int i=0; i < pair_list.size(); i++)
if(s == pairs_list.name)
return pairs_list.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]$ g++ -ansi -pedantic -Wall -Wextra
5.5_references.cpp
5.5_references.cpp: In function 'int main()':
5.5_references.cpp:39: error: invalid use of undefined type 'const
struct Pair'
5.5_references.cpp:24: error: forward declaration of 'const struct
Pair'
5.5_references.cpp:41: error: invalid use of undefined type 'const
struct Pair'
5.5_references.cpp:24: error: forward declaration of 'const struct
Pair'
5.5_references.cpp: At global scope:
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')
5.5_references.cpp:54: error: two or more data types in declaration of
'get_value'
5.5_references.cpp: In function 'Pair& get_value(const std::string&)':
5.5_references.cpp:54: error: new declaration 'Pair& get_value(const
std::string&)'
5.5_references.cpp:25: error: ambiguates old declaration 'double&
get_value(const std::string&)'
5.5_references.cpp: In function 'Pair& get_value(const std::string&)':
5.5_references.cpp:61: error: 'pair_list' was not declared in this
scope
5.5_references.cpp:63: error: invalid initialization of reference of
type 'Pair&' from expression of type 'double'
5.5_references.cpp:68: error: invalid initialization of reference of
type 'Pair&' from expression of type 'double'
[arch@voodo tc++pl]$
 
M

Markus Moll

Hi
this i the example programme. it has many errors, i am not able to
make anything out it:

Well, it's all in the error messages...
#include<iostream>
#include<string>
#include<vector>

struct Pair;
double& get_value(const std::string&);
std::vector<Pair> pairs_list;


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

Here, you try to access a Pair, but the definition of Pair is still
unknown, thus:

5.5_references.cpp:39: error: invalid use of undefined type 'const
struct Pair'
5.5_references.cpp:24: error: forward declaration of 'const struct Pair'
<< " : "
<< p->value

The same again:
5.5_references.cpp:41: error: invalid use of undefined type 'const
struct Pair'
5.5_references.cpp:24: error: forward declaration of 'const struct Pair'
<< std::endl;
}

return 0;
}

5.5_references.cpp: At global scope:
struct Pair
{
std::string name;
double val;
}

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) { [...] })

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'

Of course, this differs from the previous declaration only in the return
type and you cannot overload functions on return types:

5.5_references.cpp:54: error: new declaration 'Pair& get_value(const
std::string&)'
5.5_references.cpp:25: error: ambiguates old declaration 'double&
get_value(const std::string&)'
{
/* 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?

5.5_references.cpp:61: error: 'pair_list' was not declared in this scope
if(s == pairs_list.name)
return pairs_list.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)
}



Finally, there are some errors due to previous errors:
5.5_references.cpp:63: error: invalid initialization of reference of
type 'Pair&' from expression of type 'double'
5.5_references.cpp:68: error: invalid initialization of reference of
type 'Pair&' from expression of type 'double'

Remember: the compiler is confused about the return type of get_value!

hth
Markus
 
A

arnuld

Well, it's all in the error messages...


yes, it is always in the error messages but nearly all of them
reporting something unrelated.
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" ;-)


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.

^^^^^^^^^
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.


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.name)
return pairs_list.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"

??
 
G

Gavin Deane

std::vector<Pair> pairs_list;

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"

??

No. It means that you should declare variables to be the most
appropriate type for their use. In this case, you are using i to index
a std::vector<Pair> and to compare to the result of calling size() on
a std::vector<Pair>. size() returns std::vector<Pair>::size_type so
that is the type you should use for i.

std::vector<Pair>::size_type will be a typedef for one of the built in
integral types, but you don't need to worry about which. That is an
implementation detail private to the vector. The size_type typedef is
provided specifically for this sort of thing.

Gavin Deane
 
D

Daniel T.

arnuld said:
yes, it is always in the error messages but nearly all of them
reporting something unrelated.

Arnuld, when you get a whole bunch of error messages like that, ignore
all of them but the first two or three. Often one error will cause a
whole slew of error reports by the compiler, so fix the first error,
then try to recompile before trying to fix the next error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top