Re: What can't I make a call in global scope?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am Freitag, 5. Dezember 2008 schrieb 孙宗君:
> Why the following code is illegal?
>
>
> #include <iostream>
> #include <vector>
>
> vector<int > vi;
>
> vi.push_back(10); //why this line is illegal?
> int ii = vi.at(0); // this line is legal??
>
> int main()
> {
>   return 0;
> }
>

You want this:
</code>
#include <vector>

vector<int> vi;

int main()
{
  vi.push_back(10); 
  int ii = vi.at(0); 

  return 0;
}
</code>
or even better
<code>
#include <vector>

int main()
{
  vector<int> vi;
  vi.push_back(10); 
  int ii = vi.at(0); 

  return 0;
}
</code>
because one should avoid global variables.

As Ian mentioned this is a basic C++ language question.  The best place to ask 
these kind of questions is the c++ news group:
  http://groups.google.de/group/comp.lang.c++/topics
Please use the search service you'll see there before asking a question.  

To make you life much easier, the first things you should search for are a 
nice c++ tutorial at the web and a good C++ beginner book.

Best,
 -- Maik



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux