473,658 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Vector Problem

Nepomuk
3,112 Recognized Expert Specialist
Hi everyone!
Until now, I've almost only programmed in Java and am now learning C++ (using Bjarne Stroustrup's book). Now, I wanted to test one of the examples from the book, but somehow it doesn't work as it should.
Here's my code, reduced to the most simple elements:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. vector<int> myVec(100);
  7.  
  8. int main()
  9. {
  10.         for(int i=0; i<26; i++)
  11.         {
  12.                 myVec[i]=i;
  13.         }
  14. }
  15.  
When trying to compile it with g++, I get: (translated from german)
Expand|Select|Wrap|Line Numbers
  1. vectorTest.cpp:12: Error: expected constructor, destructor, or type conversion before »<« token
  2. vectorTest.cpp: In function »int main()«:
  3. vectorTest.cpp:23: Error: »myVec« was not defined in this scope
  4.  
Here, line 12 is the line "vector<int > myVec(100);" and line 23 is "myVec[i]=i;".
In Java, I would guess, that a line like
Expand|Select|Wrap|Line Numbers
  1. myVec = new Vec<int>();
  2.  
was missing, but as it's not in the book, there must be some other error.

I'm sure, the error is really simple, but I just can't find it. Who will solve the mystery for me?

Greetings,
Nepomuk
Apr 18 '08 #1
6 1677
Laharl
849 Recognized Expert Contributor
You need to #include <vector>.
Apr 18 '08 #2
Nepomuk
3,112 Recognized Expert Specialist
You need to #include <vector>.
I KNEW it would be simple! ^^
Thanks a lot, it works fine now. Thing is, in this book, he just left out all "#include"s and I just assumed, vector was in one of those, he had mentioned.
Do you have to include a lot of classes like "vector" separately or are most in libraries like "iostream"?

Greetings,
Nepomuk
Apr 18 '08 #3
Ganon11
3,652 Recognized Expert Specialist
You have to #include any STL classes you use, such as set, map, vector, list, etc. Otherwise, most of what you need can be found in iostream or string.
Apr 18 '08 #4
Nepomuk
3,112 Recognized Expert Specialist
You have to #include any STL classes you use, such as set, map, vector, list, etc. Otherwise, most of what you need can be found in iostream or string.
Thanks a lot, I'll keep that in mind.

Greetings,
Nepomuk
Apr 18 '08 #5
JosAH
11,448 Recognized Expert MVP
Keep your Bjarne Stroustrup open at page 431 etc. (at least in my old copy); it
lists the most important header files from the standard library.

kind regards,

Jos
Apr 18 '08 #6
Nepomuk
3,112 Recognized Expert Specialist
Keep your Bjarne Stroustrup open at page 431 etc. (at least in my old copy); it
lists the most important header files from the standard library.

kind regards,

Jos
Thanks, it's the same page in my quite new "Special Edition". Those lists will come in handy next time! ^^

Greetings,
Nepomuk
Apr 21 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

31
14328
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
9
2726
by: Rafi Kfir | last post by:
Hi, This may look as a smiple task to most of you, but to me (a beginner with C), it drives me crazy. All I want is that one function passes a two dimensional array of strings to another function. example: NOTE: the strings can be of different length and the array will be
15
3198
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more crippling problems! I never used C++ much, so, I don't remember commands or fuctions well. My questions are simple. I posted the DOS window output so you can see the Borland C++ version I am using: E:\TZ>bcc32 file2
8
5105
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
4
3970
by: NT | last post by:
Hi there! I am puzzled by what I think must be a subtlety of C++ when list-initializing member variables in derived classes... Here's what I mean... class A { // warning l337proggy alert! ;-) public: A(int v=9812); // default value provided since this base constructor
4
4193
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { } double &operator()(int i) { return myX; }
3
2230
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using namespace std; ...... vector <Vector> vectorlist;
4
1920
by: Travis | last post by:
I'm creating a real simple tree. No sorting and every node can have infinite children. // TreeNode.h #ifndef TREENODE_H #define TREENODE_H #include <iostream> #include <iomanip>
1
6012
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main function which i think has problem // this a simple program about phonebook,it can add items,del items,find items and #include "member.h" #include <fstream> #include <vector>
0
8427
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
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
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
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
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
1737
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.