ThE_aLpHaBeAt's blog

By ThE_aLpHaBeAt, 12 years ago, In English

This is my code... they view error on vector book(5), Why?

#include<iostream>
using namespace std;
struct letter
{
  string name;
  int age;       
};
vector<letter> book (5);

int main()
{
 int n,i;
 cin>>n;
 while (book.size()<n-1) 
 {
  book.resize(book.size()++);      
 }  
 for (i=0;i<n;i++)
 {
  cin>>book[i].name;
  cin>>book[i].age;    
 }
 cout<<"Information Added!!!\n";
}

  • Vote: I like it
  • -14
  • Vote: I do not like it

»
12 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

book.resize(book.size()++); you can't call ++, book.size() is not "L-value", it is not a reference. should be book.resize(book.size()+ 1); also insdead of while ... you can call book.resize(n);

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    but he view problem on vector book(5);

  • »
    »
    12 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    vector book(5); he view: expected constructor, destructor, or type conversion before '<' token

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

may be problem on struct? :(

»
12 years ago, # |
Rev. 5   Vote: I like it +11 Vote: I do not like it

I think you forgot to include vector.

»
12 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Ooo, I'm Understand! That need (#include).....