Multiple Errors when Following Vector Tutorial

Hi Cplusplus forum,

Thank you for hosting a place for beginners to write C++ questions! I'll try to not ask anything too absurd, but as I'm learning sometimes it's tough to know what is and is not absurd!

I would like to begin working with vectors and am following an online tutorial here:

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

In fact it appears the below code doesn't even address vectors yet but uses arrays, so the directive #include <vector> may be unnecessary here. Anyway, I get a number of errors with the code below, and I don't know what's causing it. I feel like it may be something very simple - like how it's arranged, but regardless of how I arrange it I get the following errors:

error: expected unqualified-id before 'for'
error: 'i' does not name a type
error: expected unqualified-id before '++' token
error: expected unqualified-id before 'delete'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstddef>
#include <vector>
#include <iostream>

int main(){
	std::size_t size = 10;
	int StaticArray[10];
	int *DynamicArray = new int[size];
	for(int i=0; i<10; ++i){
		StaticArray[i] = i;
		DynamicArray[i] = i;
	}
	Return 0;
}


Any help would be really great! Thank you!
You're correct, this example does not use vectors. Line 2 is unnecessary.

The only error I see in the code is line 13. return should not be capitalized.
Other than that, the code compiles cleanly for me.

It does not appear that the code you posted is the code you're attempting to compile.
error: expected unqualified-id before 'delete'

There is no delete keyword in the code you posted.


The program leaks memory (no delete[] called on DynamicArray though it's there on the website) and typo on line 13 – return with lower-case 'r' – otherwise it works fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstddef>
#include <vector>
#include <iostream>

int main(){
	std::size_t size = 10;
	int StaticArray[10];
	int *DynamicArray = new int[size];
	for(int i=0; i<10; ++i){
		StaticArray[i] = i;
		DynamicArray[i] = i;
		std::cout << "StaticArray: " << StaticArray[i]
            << " DynamicArray: " << DynamicArray[i] << "\n";
	}
	delete[] DynamicArray;
//	Return 0;

}

 the below code doesn't even address vectors yet

yet is indeed the key word, scroll further down
Sorry about the missing delete keyword, I removed it just before copying it to the post.

gunnerfunner: what you suggested seems to work, but I'm a bit mystified as to why it's working now and not before, as I'm *nearly certain* what I had was basically the same without the output.

Thanks again, I really appreciate the help on this!
Topic archived. No new replies allowed.