By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,885 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,885 IT Pros & Developers. It's quick & easy.

vector<string> gives error

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

vector<stringsvec(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<stringsvec(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"

Oct 10 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
arnuld wrote:
in C++ Primer 4/3 Lippman says in chapter 3, section 3.3.1:

vector<stringsvec(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<stringsvec(10);
std::vector<std::stringsvec(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
Regards,
Sumit.
Oct 10 '06 #2

P: n/a
Sumit Rajan wrote:
std::vector<stringsvec(10);

std::vector<std::stringsvec(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<stringsvec;" &
"std::vector<std::stringsvec;" say to the compiler?

Oct 10 '06 #3

P: n/a
arnuld wrote:
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<stringsvec;" &
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::stringsvec;" 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,

--
Ney André de Mello Zunino
Oct 10 '06 #4

P: n/a
Ney André de Mello Zunino wrote:
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.
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"

Oct 10 '06 #5

P: n/a

"arnuld" <ar*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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
Oct 10 '06 #6

P: n/a
LR
arnuld wrote:
>

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<intis 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
Oct 10 '06 #7

P: n/a
arnuld wrote:
#include <iostream>
#include <vector>
#include <string>
// insert this line here:
using namespace std;
>
int main() {
std::vector<stringsvec(10);

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

return 0;
}
Best regards, Martin
Oct 10 '06 #8

P: n/a
Martin Steen wrote:
#include <iostream>
#include <vector>
#include <string>

// insert this line here:
using namespace std;
it seems like you do not read C++ FAQs:

http://www.parashift.com/c++-faq-lit....html#faq-27.5

i did check them before i posted my problem.

thanks

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

Oct 10 '06 #9

P: n/a

arnuld wrote:
Martin Steen wrote:
#include <iostream>
#include <vector>
#include <string>
// insert this line here:
using namespace std;

it seems like you do not read C++ FAQs:

http://www.parashift.com/c++-faq-lit....html#faq-27.5

i did check them before i posted my problem.
Nice, someone answers your question and you respond flippantly.

Oh well. Don't qualify your namespaces then. See how far it gets yah.

Oct 10 '06 #10

P: n/a
Noah Roberts wrote:
>
arnuld wrote:
Martin Steen wrote:
#include <iostream>
#include <vector>
#include <string>
>
// insert this line here:
using namespace std;
it seems like you do not read C++ FAQs:

http://www.parashift.com/c++-faq-lit....html#faq-27.5

i did check them before i posted my problem.

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
Oct 10 '06 #11

P: n/a
Noah Roberts wrote:
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

Oct 11 '06 #12

P: n/a
arnuld wrote:
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
"arnuld" -- www.arnuld.blogspot.com
Oct 11 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.