Solved

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

Posted on 1998-03-01
10
419 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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 NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

863 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

23 Experts available now in Live!

Get 1:1 Help Now