Solved

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

Posted on 1998-03-01
10
412 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
10 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:larockd
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:larockd
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Press Ctrl-Z, which produces End-of-File (EOF).
0
 

Expert Comment

by:ngcollin
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now