Solved

Errors On Code From Book (vector question), Question On Its Output

Posted on 1998-03-01
10
424 Views
Last Modified: 2013-12-14
I am reading Stroustrup's Edition 3 C++ book.  I am trying to get this code to compile, because I do not understand how the output can be what it is...  I was wondering if anyone can help me out getting this to compile.  I am sure I do not have the proper include files included, and I am working under Microsoft Visual C++ 5.0.

According to the book if the input was
aa bb bb aa aa bb aa aa

The output should be
aa: 5
bb: 3

I have no idea how this could be...I would figure it would display each element of vector's name and what the value of val would be..  Any help would be appreciated..

Below Is Code And Errors..

#include <iostream.h>
#include <string>
#include <vector>

using namespace std;

struct Pair {
 string name;
 double val;
};

vec<Pair>pairs;

double& value(const string& s);


int main() // count the occurence of each word on input

{
 string buf;

 while (cin>>buf) value(buf)++;
 for (vector<Pair>::const_iterator p = pairs.begin(),p!=pairs.end(), ++p)
    cout << p->name << " : " << p->val << '\n';

 return 0;

}


double& value(const string& s)
/*
 Maintain a set of pairs:
 search for s, return its value if found; otherwise
 make a new pair and return the default value 0
*/

{
 for (int i=0; i < pairs.size(); i++)
  if (s==pairs[i].name) return pairs[i].val;

  Pair p={s,0};
  pairs.push_back(p);  // add pair at end
  return pairs[pairs.size()-1].val;

}


Errors...

--------------------Configuration: vector - Win32 Debug--------------------
Compiling...
vector.cpp
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(16) : error C2501: 'vec' : missing decl-specifiers
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(16) : error C2239: unexpected token '<' following declaration of 'vec'
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(16) : error C2059: syntax error : '<'
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(26) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>>' (or there
is no acceptable conversion)
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2065: 'pairs' : undeclared identifier
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2228: left of '.begin' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2086: 'p' : redefinition
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ';' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ';' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ')' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(28) : error C2143: syntax error : missing ';' before '!='
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(44) : error C2228: left of '.size' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2109: subscript requires array or pointer type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2228: left of '.name' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2109: subscript requires array or pointer type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(45) : error C2228: left of '.val' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(47) : error C2552: 'p' : non-aggregates cannot be initialized with initializer list
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(48) : error C2228: left of '.push_back' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(50) : error C2228: left of '.size' must have class/struct/union type
F:\Program Files\DevStudio\MyProjects\vector\vector.cpp(50) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

vector.exe - 20 error(s), 0 warning(s)
0
Comment
Question by:larockd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 20 total points
ID: 1182910
The following is the correct one.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Pair {
 string name;
 double val;
};

vector<Pair>pairs;

double& value(const string& s);


int main() // count the occurence of each word on input

{
 string buf;

 while (cin>>buf) value(buf)++;
 for (vector<Pair>::const_iterator p = pairs.begin();p!=pairs.end(); ++p)
    cout << p->name << " : " << p->val << '\n';

 return 0;

}


double& value(const string& s)
/*
 Maintain a set of pairs:
 search for s, return its value if found; otherwise
 make a new pair and return the default value 0
*/

{
 for (int i=0; i < pairs.size(); i++)
  if (s==pairs[i].name) return pairs[i].val;

  Pair p;
  p.name = s;
  p.val = 0;
  pairs.push_back(p);  // add pair at end
  return pairs[pairs.size()-1].val;

}

0
 

Author Comment

by:larockd
ID: 1182911
Chensu,
Thanks For your help...I pasted your code into the compiler and below is the errors from the compile..   Is there something I have to change in Visual C++ to get this to compile.


--------------------Configuration: vector - Win32 Debug--------------------
Compiling...
vector.cpp
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const template-parameter-1 *)' : could not deduce t
emplate argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const template-parameter-1 *,const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &)' : could not deduce t
emplate argument for 'const template-parameter-1 *' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const class std::basic_string<`template-parameter-1
',`template-parameter-2',`template-parameter-3'> &)' : could not deduce template argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &,c
onst class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &)' : could not deduce template argument for 'const class std::reverse_iterator<`template-parameter-
1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2784: 'bool __cdecl std::operator <(const struct std::pair<`template-parameter-1',`template-parameter-2'> &,const struct std::pair<`template-parameter-1',`template-parameter-2'> &)' : could
 not deduce template argument for 'const struct std::pair<`template-parameter-1',`template-parameter-2'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(45) : error C2676: binary '<' : 'const struct Pair' does not define this operator or a conversion to a type acceptable to the predefined operator
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const template-parameter-1 *)' : could not deduce t
emplate argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const template-parameter-1 *,const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &)' : could not deduce t
emplate argument for 'const template-parameter-1 *' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &,const class std::basic_string<`template-parameter-1
',`template-parameter-2',`template-parameter-3'> &)' : could not deduce template argument for 'const class std::basic_string<`template-parameter-1',`template-parameter-2',`template-parameter-3'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &,c
onst class std::reverse_iterator<`template-parameter-1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &)' : could not deduce template argument for 'const class std::reverse_iterator<`template-parameter-
1',`template-parameter-2',`template-parameter-3',`template-parameter-4',`template-parameter-5'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2784: 'bool __cdecl std::operator <(const struct std::pair<`template-parameter-1',`template-parameter-2'> &,const struct std::pair<`template-parameter-1',`template-parameter-2'> &)' : could
 not deduce template argument for 'const struct std::pair<`template-parameter-1',`template-parameter-2'> &' from 'const struct Pair'
F:\Program Files\DevStudio\VC\INCLUDE\xutility(47) : error C2676: binary '<' : 'const struct Pair' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

vector.exe - 12 error(s), 0 warning(s)

0
 
LVL 3

Expert Comment

by:q2guo
ID: 1182912
try this larockd, I had the same problem just a few days ago.

#include <iostream.h>
#include <string>
#include <vector>

struct Pair {
     string name;
     double val;
};

std::vector<Pair>pairs;

.....
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:larockd
ID: 1182913
I gave it a try, but It did not help.  Same errors..  I beleive even though I had it declared as
vector<Pair>pairs; it was actually std::vector<Pair>pairs; because I had using namespace std; defined at the top.

Thanks Again
0
 
LVL 23

Expert Comment

by:chensu
ID: 1182914
Follow these steps,

1. Create a new Win32 Console Application.
2. Copy and paste the above code I gave you, name it test.cpp.
3. Add the test.cpp to the project.
4. Build it.

If the compiling errors still come out, the only question is whether you have installed Visual Studio Service Pack 1, 2 or 3?
0
 

Author Comment

by:larockd
ID: 1182915
Mr. Chensu,
I did as you directed and still have the same compiling errors.  As you have indicated I do not have any of the service packs.  I am on my way right now to start downloading them to install them..

Should this fix my problem?

0
 

Author Comment

by:larockd
ID: 1182916
I just checked out microsofts site, and like you indicated I found a KB article that referenced this error and the fix is in service pack one.  I am now downloading it.


0
 

Author Comment

by:larockd
ID: 1182917
The code compiles with the installation of the service pack..  I do have a question about the code..

This line here

 while (cin>>buf) value(buf)++;

is an infinite input loop to me, how do you break out of it for normal program operation?

Thanks For your help and good call on the problem with it being the service pack.


0
 
LVL 23

Expert Comment

by:chensu
ID: 1182918
Press Ctrl-Z, which produces End-of-File (EOF).
0
 

Expert Comment

by:ngcollin
ID: 1182919
cin>> will return a reference to the stream. So

while(cin>>buffer)

Checks if there is more input in the stream.

As you are using Visual C++ 5 like me, I assume you are not on a unix platform, therefore pressing Ctrl-Z won't give you an EOF and thus will not be able to stop the loop.

To test this program in dos, you could run this program by piping a text stream to the program, eg:

echo testing aa bb bb aa aa bb aa aa | yourprog.exe

This way, the test for the end of stream will work and your program can terminate.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question