help-gplusplus
[Top][All Lists]
Advanced

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

Re: vector not linked?


From: Guy Harrison
Subject: Re: vector not linked?
Date: Mon, 14 Jun 2004 19:43:10 +0100
User-agent: KNode/0.7.2

Samik Raychaudhuri wrote:

> Hello,
> I am using cygwin version of gcc/g++ 3.3.1 [current build] and trying to
> compile the following simple program:
> 
> -------------------------------
> #include <iostream>
> #include <vector>
> using namespace std;
> 
> int main()
> {       vector v;
>        v.push_back(12);
>        v.push_back(20);
>        for(int i=0;i<v.size();i++)
>        {       cout<<v[i]<<endl;       }
> }
> ------------------------------------------
> 
> This is the error msg I am getting:
> v.cpp: In function `int main()':
> v.cpp:6: error: `vector' undeclared (first use this function)
> v.cpp:6: error: (Each undeclared identifier is reported only once for each
>   function it appears in.)
> v.cpp:6: error: syntax error before `;' token
> v.cpp:7: error: `v' undeclared (first use this function)
> 
> Am I missing something or is g++ broken?

A tutorial. ;-)

Lurk/Google in comp.lang.c++ and alt.comp.lang.learn.c-c++ for their FAQ's.

> Thanks for any pointers.

vector is a container class. In theory it can hold anything. You've omitted
to tell it what "anything" is. EG:

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

template <typename T> void
Dump(T & t)
{
 for (std::size_t i = 0; i < t.size(); ++i)
   std::cout << t[i] << std::endl
 ;
}

int main()
{std::vector<int>         vi;
 std::vector<std::string> vs;

 vi.push_back(12);
 vi.push_back(20);

 vs.push_back("12");
 vs.push_back("20");

 Dump(vi);
 Dump(vs);

 return EXIT_SUCCESS;
}

<hint on some options>
$ g++ -o c.exe -ansi -pedantic -W -Wall -Werror c.cpp
</hint on some options>

The two aforementioned newsgroups can help with (non compiler specific)
detail - like why I've avoided "using namespace std" and more subtle items
why "++i" rather than "i++" and so forth. Compiler specific stuff (ie most
things found in "man/info gcc/g++") usually want to come here.


-- 
Guy Harrison


reply via email to

[Prev in Thread] Current Thread [Next in Thread]