[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 1998-03-01
10
Medium Priority
?
467 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 80 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Suggested Courses

834 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