473,721 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector<string> gives error

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

vector<stringsv ec(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<str ingsvec(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 5711
arnuld wrote:
in C++ Primer 4/3 Lippman says in chapter 3, section 3.3.1:

vector<stringsv ec(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<str ingsvec(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<str ingsvec(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<st ringsvec;" &
"std::vector<st d::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<st ringsvec;" &
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<st d::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::iterato r/ gives error?

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

"arnuld"

Oct 10 '06 #5

"arnuld" <ar*****@gmail. comwrote in message
news:11******** *************@i 42g2000cwa.goog legroups.com...
Ney Andri de Mello Zunino wrote:

also why this works: /std::vector<int >::iterator/ & why using
/std::vector<int >::std::iterato r/ 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::iterato r/ 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<str ingsvec(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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
17993
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 application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
2
2633
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 ? // test.cpp #include <iostream> #include <fstream> #include <vector> #include <string>
5
3550
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
2559
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( vector<string>::const_iterator it = possible_data_types.begin();
10
2491
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! --Shafik
6
5715
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
6
7377
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", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
42
4531
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 of string, in one line. I could
9
3812
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 representation of vector<string>? Will the vector contain the string objects or will it contain pointers/references to the string objects? The reason I ask is that it is not clear to me how the v.reserve() and &v operations would work for a vector<string>.
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.