Multiple errors in header, looking for insight

Hey!
I'm new to c++, and I'm working with a program to print a vector with random values from 0 to 100. When I try to run the program, I get the following errors:

- utilities.h(8): error C2065: 'vector': undeclared identifier

- utilities.h(8): error C2062: type 'int' unexpected

- tests.cpp(29): error C3861: 'randomizeVector': identifier not found

1
2
3
4
utilities.h
#pragma once

  void randomizeVector(vector<int>& vektor, int antall);


1
2
3
4
tests.h
#pragma once

void testVectorSorting();


1
2
3
4
5
6
7
8
9
tests.cpp
#include "tests.h"
#include "utilities.h"
#include "std_lib_facilities.h"

void testVectorSorting() {
	vector<int> percentages;
	randomizeVector(percentages, 20);
}


1
2
3
4
5
6
7
8
9
10
11
utilities.cpp
#include "utilities.h"
#include "std_lib_facilities.h"

void randomizeVector(vector<int>& vektor, int antall) {
	for (int i = 0; i < antall; i++) {
		int tall = rand() % 100 + 1;
		vektor.push_back(tall);
		cout <<  vektor[i]; 
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
main.cpp
#include "std_lib_facilities.h"
#include "utilities.h"
#include "tests.h"

int main(){
	srand(time(0));
	testVectorSorting();

	return 0;
}


The reason why I have so many different cpp and header files is because it's a part of a bigger task. There might of course be some logical errors as well..
Sorry if it's a messy post, but I appreciate all answers!
vector requires the header:
#include <vector>

the int error is a compiler spasm left over from the vector confusion. As a rule of thumb, fix the top error and try again unless its a very lengthy compile time program (millions of lines stuff).

ditto for the next error. It couldn't figure out the function from the vector error so it didnt register the function name ... error cascading ... you get used to it :)

consider using <random> instead of C's random tools.
Last edited on
Thanks for the tips!
However, I still get the same errors after including vector..
Am I supposed to use the #include <vector> in the headers as well?
I tried both including in the header and not, but it didn't seem to work
well, that is a loaded question :)
you can 'get away' with include chains... that is, if header B includes header A that includes Vector, B will be fine if it uses vector. This is bad practice, but it happens from time to time.

The right thing to do is: if a file uses something, you include it in that file.
Your own headers should have include guards (these let the compiler handle it if you include it more than once in the same program) and all the standard ones have this taken care of for you.

all that to say include vector anywhere you used a vector (in the header, and not in the related cpp file is ok, but each header that has a user should have it).

pragma once is, by the way, a microsoftism for an include guard.
Last edited on
Thanks again, you are really helping me understand how this works!:)
The code is running now btw, just had to adjust the including stuff a little (just as you said).
Topic archived. No new replies allowed.