Expected declaration before token '}'

HI all

Please help with this problem. I think its quite small and I have been looking at it for ages but cant work it out?? The error I get in the code is as per the title of this message and it appears in line 13:1, at the end of the first function. It is a compile time error. I think its to do with brackets but I have tried as many combinations as I can think of with no success. The code is below. If anyone can help me out I would be grateful.

#include <iostream>
#include <vector>
using namespace std;

template<typename T>
void Print(T array[], int array_size)
{
for(int i = 0; i < array_size; i++)

cout <<array[i] <<" ";

}
}
template<typename T>
void print2(T vector<int> & v)
{
for (int i=1; i<=v.size(); i++) v.push_back(i);
{
std::cout << "myvector contains:";
for (std::vector<int>::const iterator it = v.begin() ; it != v.end(); ++it){
std::cout << ' ' << *it;
std::cout << '\n';
}
}

}
int main (){

//double Array[10] = {1.3,2.44,0,3,4,2,5,6,0,7};
//Print(Array, 10);

string s[] = {"if", "you","just","think","about","it","it's","actually","quite", "easy"};
Print(s, 10);


//int vector[3] = {1,2,3};
//print2(vector);


}
Remove the bracket on line 13.
if you indent your code, you will quickly see where is the brace error :

( and remove std:: prefixes since, you have using namespace std; )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
using namespace std;

template<typename T>
void Print(T array[], int array_size)
{
    for(int i = 0; i < array_size; i++)
        cout <<array[i] <<" ";

}

template<typename T>
void print2(T vector<int> & v)
{ 
    for (int i=1; i<=v.size(); i++) v.push_back(i);
    {
        std::cout << "myvector contains:";
        for ( std::vector<int>::const iterator it = v.begin() ;
                it != v.end();
                ++it)
        {
            std::cout << ' ' << *it;
            std::cout << '\n';
        }
    }	
}

int main ()
{

    //double Array[10] = {1.3,2.44,0,3,4,2,5,6,0,7};
    //Print(Array, 10);

    string s[] = {"if", "you","just","think","about","it","it's","actually","quite", "easy"};
    Print(s, 10);


    //int vector[3] = {1,2,3};
    //print2(vector);


}
Hi Fran


Thanks for the prompt reply.

I have tried this previously and this then brings up three more errors relating to line 15:27.
expected unqualified id before & token
expected ) before & token
expected initializer before & token.

Any ideas?


Hi Shadow fiend,

I removed a bracket from the original code but it brought up the three errors I listed to you. Im very new to this C++ so I cant work it out? When I add the backet I get one error when I take it out I get three? Also, if I want to use the std:: prefix do I put using std::cout and std::vector at the top instead of using namespace std?
Hi bennyscammin1

There are a few problems with the code,

#include <iostream>
#include <vector>
using namespace std;

template<typename T>
void Print(T array[], int array_size)
{
for(int i = 0; i < array_size; i++)
cout <<array[i] <<" ";
}

template<typename T>
void print2(const vector<T>& v)
{
for (int i=1; i<=v.size(); i++) v.push_back(i);
{
cout << "myvector contains:";
for (const::vector<int>::iterator it = v.begin() ; it != v.end(); ++it)
{
cout << ' ' << *it;
cout << '\n';
}
}
}

int main ()
{

//double Array[10] = {1.3,2.44,0,3,4,2,5,6,0,7};
//Print(Array, 10);

string s[] = {"if", "you","just","think","about","it","it's","actually","quite", "easy"};
Print(s, 10);
system ("pause");


//int vector[3] = {1,2,3};
//print2(vector);


}

Its always good to use using namespace std because by using that only you will be free to use std:: at every required place in your program.

That will not only reduce size of the program but also helps you to make any mistake related to std::
No Joy with that. Its now throwing up a full page of errors? Did the code above compile for you?

Thanks
Yes, its working on my machine without any error.
Kindly check the following link.

https://plus.google.com/photos/114243324783447513765/albums/5947580950683186849?authkey=CKnDtJXKoK3uywE
Thanks. I will look into it further. There must be some issue with my compiler.
Topic archived. No new replies allowed.