PDA

View Full Version : vector help needed


surreal5335
07-02-2010, 09:42 PM
I am working on a vector based program for my class.

Here is my simple setup I have right now:


#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <vector>

using namespace std;

template <typename int>

void writeVector(const vector<int>& v);
void sortVector(vector<int>& v);



int main() {



vector<int> v; // int vector with 0 elements
~vector(); // destructor



system("PAUSE");


}

void writeVector(const vector<int>& v) {

template <typename int>
}

void sortVector(vector<int>& v) {


template <typename int>
}



Just trying to get the basic structure working first.

Here is the pile of errors I have so far:




C:\Documents and Settings\ben\Desktop\park\c++>g++ vector.cpp
vector.cpp:11: error: expected nested-name-specifier before "int"
vector.cpp:11: error: ISO C++ forbids declaration of `parameter' with no type
vector.cpp: In function `int main()':
vector.cpp:23: error: missing template arguments before '(' token
vector.cpp: In function `void writeVector(const std::vector<int, std::allocator<
int> >&)':
vector.cpp:54: error: expected primary-expression before "template"
vector.cpp:54: error: expected `;' before "template"
vector.cpp: In function `void sortVector(std::vector<int, std::allocator<int> >&
)':
vector.cpp:73: error: expected primary-expression before "template"
vector.cpp:73: error: expected `;' before "template"




I appreciate the help in any of these you can point out.

oracleguy
07-02-2010, 09:49 PM
This is bad, don't do this ever:
~vector(); // destructor

Objects on the stack will have their destructors called automatically when they go out of scope. When they are on the heap the destructor is called when you free the memory.

void sortVector(vector<int>& v) {


template <typename int>
}


Take the template line out, what are you trying to do? You don't need the template line (which goes above the function definition) unless you want a templated function.

And you don't need that template line near top of the program either.

surreal5335
07-03-2010, 03:23 AM
Thanks a lot for the reply, I was able to get it to compile. I've been wondering why I keep seeing the mention of the template in these vector tutorials, didnt make sense.

I have now added a lot of the functionality to my program and ran into this problem:

I am atempting to assign 15 random numbers to the vector in my main, then print out the numbers using a writeVector() method. I am not getting any compilation errors although I am getting a lot of 0's. Many more than 15 as well.

This is what I have so far:


#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <vector>
#include "d_random.h"
//#include "d_vector.h"
//#include "d_except.h"
using namespace std;



void writeVector(const vector<int>& v);
void sortVector(vector<int>& v);

int vArr[15];



int main() {



vector<int> v; // int vector with 0 elements


randomNumber rnd;

for (int i = 0; i <= 15; i++) {
v.push_back(rnd.random(100)); // push 15 entries from 0-99 onto the vector
}

writeVector(v); // output vector
sortVector(v); // sort the vector
writeVector(v); // output sorted vector

system("PAUSE");

// 1. Declare: miniVector<int> v;
// 2. Declare: randomNumber rnd;
// 3. call v.push_back(rnd.random(100)); to
// 4. call writeMiniVector to output vector
// 5. call sortMiniVector to sort the vector
// 6. call writeMiniVector to output the sorted vector.

}

void writeVector(const vector<int>& v) {

//1. Iterate through the vector v and output each element to the screen.

for (int i = 0; i <= v.size(); i++) {
cout << vArr[i] << " ";
}


}

void sortVector(vector<int>& v) {


}




I appreciate the help