vector<string> gives error

A

arnuld

in C++ Primer 4/3 Lippman says in chapter 3, section 3.3.1:

vector<string> svec(10);
// 10 elements, each an empty string

here is the the code output & output from my Debian box running "gcc
3.3.5":

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

int main() {
std::vector<string> svec(10);

// std::cout << v_str << std::endl;

return 0;
}


/* OUTPUT

unix@debian:~/programming/cpp$ g++ -ansi test-programme.cpp

test-programme.cpp: In function `int main()':
test-programme.cpp:6: error: `string' undeclared (first use this
function)
test-programme.cpp:6: error: (Each undeclared identifier is reported
only once
for each function it appears in.)
test-programme.cpp:6: error: template argument 1 is invalid
test-programme.cpp:6: error: template argument 2 is invalid
test-programme.cpp:6: error: ISO C++ forbids declaration of `svec' with
no type

unix@debian:~/programming/cpp$

*/


where is the trouble?

"arnuld"
 
S

Sumit Rajan

arnuld said:
in C++ Primer 4/3 Lippman says in chapter 3, section 3.3.1:

vector<string> svec(10);
// 10 elements, each an empty string

here is the the code output & output from my Debian box running "gcc
3.3.5":

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

int main() {
std::vector<string> svec(10);

std::vector said:
// std::cout << v_str << std::endl;

return 0;
}


/* OUTPUT

unix@debian:~/programming/cpp$ g++ -ansi test-programme.cpp

test-programme.cpp: In function `int main()':
test-programme.cpp:6: error: `string' undeclared (first use this
function)
test-programme.cpp:6: error: (Each undeclared identifier is reported
only once
for each function it appears in.)
test-programme.cpp:6: error: template argument 1 is invalid
test-programme.cpp:6: error: template argument 2 is invalid
test-programme.cpp:6: error: ISO C++ forbids declaration of `svec' with
no type

Regards,
Sumit.
 
A

arnuld

Sumit said:
std::vector<std::string> svec(10);

OK, i even tried "using std::vector" but still same error was reported.
it means, C++ Primer is wrong here.

anyway, what exactly "std::vector<string> svec;" &
"std::vector<std::string> svec;" say to the compiler?
 
M

Martin Steen

arnuld said:
#include <iostream>
#include <vector>
#include <string>

// insert this line here:
using namespace std;
int main() {
std::vector<string> svec(10);

// std::cout << v_str << std::endl;

return 0;
}

Best regards, Martin
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

arnuld said:
OK, i even tried "using std::vector" but still same error was reported.
it means, C++ Primer is wrong here.

That would bring only the /vector/ symbol into scope.
anyway, what exactly "std::vector<string> svec;" &

The above is an attempt to instantiate the /std::vector/ template using
a type /string/ as its template parameter. If the compiler can see no
type named /string/ in scope at the point of instantiation, the
instantiation will fail.
"std::vector<std::string> svec;" say to the compiler?

This time you are using /std::string/ as the template parameter. The
difference is that you are letting the compiler know that the /string/
type actually lives inside the /std/ namespace and not in the global
namespace.

FWIW, your first attempt would have worked out if you had included a
/using std::string;/ statement before the instantiation.

Regards,
 
A

arnuld

Ney said:
That would bring only the /vector/ symbol into scope.

ok, i got it.
This time you are using /std::string/ as the template parameter. The
difference is that you are letting the compiler know that the /string/
type actually lives inside the /std/ namespace and not in the global
namespace.

FWIW, your first attempt would have worked out if you had included a
/using std::string;/ statement before the instantiation.

Hmmm.. that is why /std::vector<int>/ works as /int/ is already in
global namespace

now i understood.


also why this works: /std::vector<int>::iterator/ & why using
/std::vector<int>::std::iterator/ gives error?

please do not get angry, i am a newbie :-(

"arnuld"
 
H

Howard

Ney Andri de Mello Zunino wrote:

also why this works: /std::vector<int>::iterator/ & why using
/std::vector<int>::std::iterator/ gives error?

Because :: indicates membership (either of a class or of a namespace), and
std is not a member of std::vector<>. So, using ::std::iterator is an
error. However, iterator is a member, so using ::iterator is correct.

-Howard
 
L

LR

arnuld said:
also why this works: /std::vector<int>::iterator/ & why using
/std::vector<int>::std::iterator/ gives error?

Think of having a struct like:

namespace non_std {
struct S {
typedef int my_type;
};
}

and somewhere else in your code you have:
const non_std::S::my_type i = 43;

where (informally)
non_std is a namespace
S is a struct in the namespace non_std
my_type is a typedef in the struct S

And how that might differ from:

const non_std::S::non_std::my_type i = -1;
^^^^^^^
What is this?

Same thing for std::vector<int>::iterator

(informally)
std is a namespace.
vector<int> is the name of a class.
iterator is a type that is a member of the class vector<int>

OTOH, You could do something like this:

namespace non_std {
struct S {
struct non_std {
typedef int my_type;
};
};
}

and then
const non_std::S::non_std::my_type i = 99;

Confusing? Yes. Don't do that.



please do not get angry, i am a newbie :-(

Ok.

LR
 
D

Default User

Noah said:
Nice, someone answers your question and you respond flippantly.

Martin didn't answer the question, he gave a method to cover it up. The
contributors in the other branch pointed out the other error, and kept
with it until the OP understood the issue.
Oh well. Don't qualify your namespaces then. See how far it gets
yah.


What are you talking about? He was trying to follow the advice from the
FAQ, which is pretty good advice. He happened to screw up and miss one.
Martin's advice was to shortcut it with a using, against the advice of
the FAQ.

If you think the FAQ is wrong, better take it up with Marshall.





Brian
 
A

arnuld

Noah said:
Nice, someone answers your question and you respond flippantly.

hey Noah, i did *not* ..mean ..that...., if you felt hurt then i
apologize. i wanted to say that i always check FAQs before posting my
problem as i must not waste other's preciuos time.

2nd, i posted that senetence without even looking at your name, i did
not know my friend "Noah" is here, i will change this habit now.
> Oh well. Don't qualify your namespaces then. See how far it gets
yah.

i admire "Modern C++ style", that is why i said check the FAQs & FAQs
say "do not use /using namespace std/ & i dont, even C++ Primer 4/e
does not use this.


thanks

"arnuld" -- www.arnuld.blogspot.com
 
A

arnuld

arnuld said:
2nd, i posted that senetence without even looking at your name, i did
not know my friend "Noah" is here, i will change this habit now.

OOPS, it was Martin, "sorry to Martin" :)

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top