# Need help with Vectors

Page 1 of 1

## 12 Replies - 1162 Views - Last Post: 18 June 2009 - 07:30 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=110167&amp;s=f7b3b1b0f8fdb789f66839e29ba7bde8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 IdealistTCO

Reputation: 0
• Posts: 23
• Joined: 18-September 08

# Need help with Vectors

Posted 15 June 2009 - 04:45 PM

Hi, I've been trying to learn vectors to better prepare me for a college course that I'll be taking this fall. I'm trying to initialize a vector in a header file so it can be used in every file that I am creating. I need to know what I'm doing wrong because I'm getting errors even though I initialized it per the tutorial found on this site at Vector Tutorial I

Here is the code for the header file. Also note, when I was coding in the other source files they do recognize my vector as a vector but for some reason it still won't build.

```#ifndef VECTORDECLARE_H
#define VECTORDECLARE_H
#include <vector>

class VectorDecl{

int x,y;

public:
vector<int> vectorA;

}vectorOne;

#endif
```

Here is my main code.

```#include <iostream>
#include <vector>
#include "VectorDeclare.h"

using namespace std;
void pushVector(int);
void displayVector();
int main(){

/*Declare integer variable x and initialize it to the number 10*/
int x;
x = 10;

/*Push the integer onto the back of the vector*/
pushVector(x);

//Display the vector contents
displayVector();

return 0;
}
```

My Push back file.

```#include <vector>
#include "VectorDeclare.h"

using namespace std;

void pushVector(int x){

vectorOne.vectorA.push_back(x);

```

My display vector file
```#include <iostream>
#include <vector>
#include "VectorDeclare.h"

using namespace std;

void displayVector(){
for(long index = 0; index<(long)vectorOne.vectorA.size();++index)
cout << vectorOne.vectorA.at(index) << " " << endl << endl;

}
```

I know it's all nothing fancy but I'm really trying to learn to be a better programmer, because I want to make a career of this. I wasn't sure what use I was gonna make of my code so it has more of a generic direction. Which is just simple code making use of the vectors functions in a shortcut type manner.

*EDIT*
Almost forget. Here is the error message I'm getting

```1>------ Build started: Project: VectorTutorial, Configuration: Debug Win32 ------
1>Compiling...
1>pushVect.cpp
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C2143: syntax error : missing ';' before '<'
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C2238: unexpected token(s) preceding ';'
1>.\pushVect.cpp(9) : error C2039: 'vectorA' : is not a member of 'VectorDecl'
1>		c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(5) : see declaration of 'VectorDecl'
1>.\pushVect.cpp(9) : error C2228: left of '.push_back' must have class/struct/union
1>.\pushVect.cpp(10) : fatal error C1075: end of file found before the left brace '{' at '.\pushVect.cpp(7)' was matched
1>Main.cpp
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C2143: syntax error : missing ';' before '<'
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C2238: unexpected token(s) preceding ';'
1>DisplayVect.cpp
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C2143: syntax error : missing ';' before '<'
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(10) : error C2238: unexpected token(s) preceding ';'
1>.\DisplayVect.cpp(9) : error C2039: 'vectorA' : is not a member of 'VectorDecl'
1>		c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(5) : see declaration of 'VectorDecl'
1>.\DisplayVect.cpp(9) : error C2228: left of '.size' must have class/struct/union
1>.\DisplayVect.cpp(10) : error C2039: 'vectorA' : is not a member of 'VectorDecl'
1>		c:\users\willie\documents\visual studio 2008\projects\vectortutorial\vectortutorial\VectorDeclare.h(5) : see declaration of 'VectorDecl'
1>.\DisplayVect.cpp(10) : error C2228: left of '.at' must have class/struct/union
1>Generating Code...
1>Build log was saved at "file://c:\Users\Willie\Documents\Visual Studio 2008\Projects\VectorTutorial\VectorTutorial\Debug\BuildLog.htm"
1>VectorTutorial - 16 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
```

This post has been edited by IdealistTCO: 15 June 2009 - 05:08 PM

Is This A Good Question/Topic? 0

## Replies To: Need help with Vectors

### #2 crazyjugglerdrummer

• GAME OVER. NERD WINS.

Reputation: 121
• Posts: 690
• Joined: 07-January 09

## Re: Need help with Vectors

Posted 15 June 2009 - 06:23 PM

What exactly are you trying to do? You declare a vector in a class wrapper, then make function that manipulate a specific vector. Make the functions member functions of your class.

You're class needs a constructor. make one, then declare an instance with
Vector1Dec foo( arguments );

### #3 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Need help with Vectors

Posted 15 June 2009 - 06:39 PM

In short, it's not clear why you are trying to wrap a vector in another class, but if you insist, your problem is not with vector, it's with your class, so you need to read a tutorial (at least) on classes.

### #4 ccubed

• It's That Guy

Reputation: 164
• Posts: 1,416
• Joined: 13-June 08

## Re: Need help with Vectors

Posted 16 June 2009 - 12:31 AM

Constructors are the least of his problems. To begin with, You're confusing classes and structures. classes do not have identifiers after the end brace like vectorOne. It doesn't work that way. Classes are instantiated. That is, if you want a VectorDecl called VectorOne you do.

```VectorDecl vectorOne;

```

As for a constructor, it's not really needed here. He's not doing anything special so let the default constructor for vector deal with it.

Secondly, your functions need to be passed a VectorDecl object. That is

```void some_Function(int x, VectorDecl vectorOne);

```

Then vectorOne.VectorA.push_back(x) becomes valid. Note though that it should be named something other than what you instantiated it as.

Display vector has the same problem. Either you do that outside of a function or pass a VectorDecl to it.

Also, why is your index a long? It should just be an int. The indexes go from 0 to MAX_VECTOR_SIZE, whatever that happens to be defined as. So long is unnecessary.

Lastly, At is not needed at all. Vectors have [] overloaded, so vector.at(0) is the same as vector[0].

### #5 Lillefix

Reputation: 37
• Posts: 204
• Joined: 19-September 08

## Re: Need help with Vectors

Posted 16 June 2009 - 05:06 AM

vector<int> vectorA;

should be

std::vector<int> vectorA;

### #6 IdealistTCO

Reputation: 0
• Posts: 23
• Joined: 18-September 08

## Re: Need help with Vectors

Posted 16 June 2009 - 07:03 AM

Much appreciated. For every advice I've been given it helps knock the rust of off me. I've been like "Oooo yeaa..." with every reply I get. You guys are helping me greatly. Another thing that I JUST found is my old text book from my Intermediate C++ class and my old class notes. I'm trying to combine everything I'm getting from you guys to correct my mistakes and remember how to properly deal with classes. To those that asked, all I'm aiming to do is take what I learned about a standard vector and see if I can find other ways to make use of it. Passing the integer into those functions is pretty generic but what I'm doing there is trying to insure the passes will work. I'm intending to make more functions for the different things vectors can do and then try to make a customized program that takes advantage of that, like maybe an simple list of names. I am still going through the vector tutorial, what I'm hoping is to find out if it's possible to make a multi-directional vector than can hold data for columns and rows like seats in a stadium. I want to learn how to do that because I really want to learn advanced uses of vectors but I'm in the "crawl" phase right now. Hopefully you guys can watch me grow into a better programmer.

*Edit*
Per all advice given and information found. My programmed built. Here is what I've used and changed.

"Main"
```#include <iostream>
#include <vector>
#include "VectorDeclare.h"

using namespace std;
VectorDecl vectorOne;
int main(){

/*Declare integer variable x and initialize it to the number 10*/
int x;
x = 10;

/*Push the integer onto the back of the vector*/
vectorOne.pushVector(x);

//Display the vector contents
vectorOne.displayVector();

return 0;
}
```

My .h

```#ifndef VECTORDECLARE_H
#define VECTORDECLARE_H
#include <vector>

class VectorDecl{

int x;

public:
VectorDecl(){ x = 0;};
std::vector<int> vectorA;
void pushVector(int);
void displayVector();

};

#endif
```

pushVector

```#include <vector>
#include "VectorDeclare.h"

using namespace std;

void VectorDecl::pushVector(int x){

vectorA.push_back(x);
}
```

Display Vector

```#include <iostream>
#include <vector>
#include "VectorDeclare.h"

using namespace std;

void VectorDecl::displayVector(){
for(int index = 0; index<vectorA.size();++index)
cout << vectorA[index] << " " << endl << endl;

}
```

This post has been edited by IdealistTCO: 16 June 2009 - 07:21 AM

### #7 ccubed

• It's That Guy

Reputation: 164
• Posts: 1,416
• Joined: 13-June 08

## Re: Need help with Vectors

Posted 16 June 2009 - 11:41 AM

for multidimensional arrays, it's something like.

```std::vector<std::vector<int>> a;

```

That creates a vector of integer vectors. Thereby making you a simple 2 dimensional array.

### #8 IdealistTCO

Reputation: 0
• Posts: 23
• Joined: 18-September 08

## Re: Need help with Vectors

Posted 17 June 2009 - 08:44 PM

ccubed, on 16 Jun, 2009 - 10:41 AM, said:

for multidimensional arrays, it's something like.

```std::vector<std::vector<int>> a;

```

That creates a vector of integer vectors. Thereby making you a simple 2 dimensional array.

Really now? This has really caught my eye quicker than greased lightning. Is there anywhere I can see how this is put to use so I can practice with it. It's a bit difficult to picture it in use as it's my first time laying eyes on it. But already I want to master that piece of code.

### #9 ccubed

• It's That Guy

Reputation: 164
• Posts: 1,416
• Joined: 13-June 08

## Re: Need help with Vectors

Posted 18 June 2009 - 12:13 AM

I'll try to find a tutorial, but it's not really something they make tutorials for AFAIK. If I had to explain it though, imagine a normal vector as a list, then imagine a list of lists.

In that way, index 0 of

std::vector<int> a is a[0]
std::vector<std::vector<int>> a is a[0][0]
std::vector<std::vector<int>> a[5] is a[0][0][0]

The first one is your standard vector

The second one is your standard vector, but it holes vectors. Therefore a[x] is actually a vector and has many elements.

The third one is an array of 5 such vectors that are shown in 2. therefore, a[x] is which 2d array you want, a[x][y] is which vector inside the 2d array you want, and a[x][y][z] refers to element z, of vector y, in the 2d array of vectors at a[x].

Make any sense?

### #10 IdealistTCO

Reputation: 0
• Posts: 23
• Joined: 18-September 08

## Re: Need help with Vectors

Posted 18 June 2009 - 04:28 AM

Heck yah! That was the best crash course ever. I'm gonna test it out. I'm interested to see an example of the third to make sure I got it understood. Could u post one up?

This post has been edited by IdealistTCO: 18 June 2009 - 04:31 AM

### #11 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Need help with Vectors

Posted 18 June 2009 - 04:46 AM

Some background reading on vectors that may help a little
http://www.cplusplus...nce/stl/vector/

### #12 IdealistTCO

Reputation: 0
• Posts: 23
• Joined: 18-September 08

## Re: Need help with Vectors

Posted 18 June 2009 - 06:19 AM

Thanks bro, I'll read through that page as well. I'm going to try and make a template out of some code I will write that will store any type of data in a multidimensional vector. That's my next vector mastery step.

This post has been edited by IdealistTCO: 18 June 2009 - 06:24 AM

### #13 Milton Neal

Reputation: 1
• Posts: 3
• Joined: 28-May 09

## Re: Need help with Vectors

Posted 18 June 2009 - 07:30 AM

Hello
I found these very informative and gave me a good understanding of vectors.

http://www.codeproje...ctionclass.aspx
http://www.codeguru....ticle.php/c4027

Good luck and happy coding

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }