473,224 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

vector<string> gives error

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
12 5660
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
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
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
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

"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
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
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
5
by: Gary Wessle | last post by:
whats an efficient way to copy a string to a vector<string>? how about this? #include <iostream> #include <string> #include <vector> Using namespace std;
5
by: Martin Jřrgensen | last post by:
Hi, The piece of code I'm struggling with is so simple, that I hope nobody wants a complete example for answering the question: -------- string color_line; int data_type = 0; for(...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.