error: expected initializer before ‘void’

Well every time I try to compile my code using make command I get this error... and help?

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// ============================================================================
// algos.cpp
// ~~~~~~~~~~~~~~~~
// author: JOHN DOE
// - this is the ONLY file you can modify
// - feel free to include more headers if you need to
// ============================================================================

#include <stdexcept> // to throw exceptions if you need to
#include <fstream>   // to open & read from input file
#include <cstdlib>   // for atoi() if you want to use it
#include <set>       // for sba algorithm
#include <vector>    // for vba algorithm
#include <algorithm> // for vba algorithm
#include "Lexer.h"
#include "algos.h"
using namespace std; // BAD PRACTICE

int vba(string filename)
void printVector(vector<pair<int, int> > & myVec)
    {
    vector<pair<int, int> >::iterator i;
    for (i = myVec.begin(); i != myVec.end(); ++i) {
        cout << "(" << i->first << ", " <<  i->second << ") ";
    }
    cout << endl;
}
    
    ifs.open(filename.c_str());
    ifstream ifs; 
	string line;
	while (getline(ifs, line)) 
		if (line.substr(0,4)=="exit") {
			exit(1);
		}
		if (line=="#") {
			cout << endl;
		} else {
			istringstream iss;
			int a;
            int b;
			iss >> a;
			iss >> b;
			pair<int, int> p;
			p = make_pair(a, b);			
			pairVector.push_back(p);                      
    cout << "# of inserted pairs = " << pairVector.size() << endl;
    printVector(pairVector);
    sort(pairVector.begin(), pairVector.end());
    printVector(pairVector);
    vector<pair<int, int> >::iterator i = pairVector.begin();
    while (i != pairVector.end())
	 {
        vector<pair<int, int> >::iterator j = i+1;
        if (j != pairVector.end() && *j == *i) 
        {
            i = pairVector.erase(i);
            break;
        }        
     }
    printVector(pairVector);
    
    return pairVector.size();
}  
     }
int sba(string filename)
{ 
    int x;
    int y;
    pair<int, int> p1;
    istringstream is;
    set<pair<int, int> > set;

    myfile.open(fn.c_str());
    if (myfile.fail()) {
        cerr << "ERROR: Failed to open file " << file_name << endl;
        myfile.clear();
    } else {
        if (myfile.is_open())
        {
            while (getline(myfile,line) )
            {
                if (line[0] != '#')
                {
                    istringstream is(line);
                    is >> x;
                    is >> y;
                    if ( y > x)
                    {
                        p1 = make_pair(x, y);
                    } else{
                        
                        p1 = make_pair(y,x);
                    }

                    edgeSet.insert(p1);
                }
            }
            cout << edgeSet.size() << '\n';
            myfile.close();
        }

    }

    return 1;
           }
}







Here's the pic as to what the error shows
https://www.dropbox.com/s/05ewmacqchkaps8/Screenshot%20%2839%29.png?dl=0
I guess the cause im missing the semicolon after
?
void printVector(vector<pair<int, int> > & myVec)
You're missing either a semicolon, or the full body of the function after this line:
 
int vba(string filename)


As a minimum, it should be
 
int vba(string filename);


but I expect the function body is required too
1
2
3
4
int vba(string filename)
{
    // fill in required code for function vba here
}
closed account (48T7M4Gy)
line 19 missing ';'
I added ; to line 19 still the same error....now I get this error

error: expected unqualified-id before ‘{’ token
{
The new code that I have
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// ============================================================================
// algos.cpp
// ~~~~~~~~~~~~~~~~
// author: JOHN DOE
// - this is the ONLY file you can modify
// - feel free to include more headers if you need to
// ============================================================================
#include <iostream>
#include <sstream>
#include <stdexcept> // to throw exceptions if you need to
#include <fstream>   // to open & read from input file
#include <cstdlib>   // for atoi() if you want to use it
#include <set>       // for sba algorithm
#include <vector>    // for vba algorithm
#include <algorithm> // for vba algorithm
#include "Lexer.h"
#include "algos.h"
using namespace std; // BAD PRACTICE

int vba(string filename);
{
void printVector(vector<pair<int, int> > & myVec);
    ifs.open(filename.c_str());
    ifstream ifs; 
	string line;
    vector<pair<int, int> > pairVector;
	while (getline(ifs, line)) 
		if (line.substr(0,4)=="exit") {
			exit(1);
		}
		if (line=="#") {
			cout << endl;
		}   else {
			istringstream iss;
			int a;
            int b;
			iss >> a;
			iss >> b;
			pair<int, int> p;
			p = make_pair(a, b);			
			pairVector.push_back(p);                      
            cout << "# of inserted pairs = " << pairVector.size() << endl;
            printVector(pairVector);
            sort(pairVector.begin(), pairVector.end());
            printVector(pairVector);
            vector<pair<int, int> >::iterator i = pairVector.begin();
            while (i != pairVector.end()) {
            vector<pair<int, int> >::iterator j = i+1;
            if (j != pairVector.end() && *j == *i)  {
            i = pairVector.erase(i);
        }        
     }
    printVector(pairVector);
    
    return pairVector.size();
}  
     }
     
     
int sba(string filename)
{ 
    int x;
    int y;
    pair<int, int> p1;
    istringstream is;
    set<pair<int, int> > set;
    myfile.open(fn.c_str());
    if (myfile.fail()) {
        cerr << "ERROR: Failed to open file " << file_name << endl;
        myfile.clear();
    }   else {
        if (myfile.is_open()) {
            while (getline(myfile,line) )
            {
                if (line[0] != '#') {
                    istringstream is(line);
                    is >> x;
                    is >> y;
                    if ( y > x) {
                        p1 = make_pair(x, y);
                    }   else {  
                        p1 = make_pair(y,x);
                    }
                    edgeSet.insert(p1);
                }
            }
            cout << edgeSet.size() << '\n';
            myfile.close();
        }
    }
    return 1;
           }
}




It looks as though there is the code for function vba(), which should look roughly like this:
1
2
3
4
5
6
7
int vba(string filename)
{
    // lots of code omitted for clarity
    //
    // 
    return something;
}


... and then somehow some code for a completely different function printVector() has become interspersed and muddled into it.

Although the code in this post is quite advanced, it might help to review some of the basics of functions so you can recognise what well-formed code should look like, before proceeding further.

http://www.cplusplus.com/doc/tutorial/functions/
I did have a print code after the void print statement...
I did have a print code after the void print statement...
I noticed that in the earlier version of the code. But the problem was as i described, the two functions should be separate but the code for one somehow ended up inside the other.
Hmmm okay I see where u getting att..
Tried everything cant seem to get the error fixed ;/
i chopped and changed the code a bit to get this which at least compiles, though I don't claim it is correct. Some lines commented out as i don't have those headers.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// ============================================================================
// algos.cpp
// ~~~~~~~~~~~~~~~~
// author: JOHN DOE
// - this is the ONLY file you can modify
// - feel free to include more headers if you need to
// ============================================================================
#include <iostream>
#include <sstream>
#include <stdexcept> // to throw exceptions if you need to
#include <fstream>   // to open & read from input file
#include <cstdlib>   // for atoi() if you want to use it
#include <set>       // for sba algorithm
#include <vector>    // for vba algorithm
#include <algorithm> // for vba algorithm
//#include "Lexer.h"
//#include "algos.h"
using namespace std; // BAD PRACTICE

void printVector(vector<pair<int, int> > & myVec)
{
    vector<pair<int, int> >::iterator i;
    for (i = myVec.begin(); i != myVec.end(); ++i) {
        cout << "(" << i->first << ", " <<  i->second << ") ";
    }
    cout << endl;
}

int vba(string filename)
{
    ifstream ifs;     
    ifs.open(filename.c_str());

	string line;
    vector<pair<int, int> > pairVector;
	while (getline(ifs, line)) 
		if (line.substr(0,4)=="exit") {
			exit(1);
		}
		if (line=="#") {
			cout << endl;
		}   
        else 
        {
			istringstream iss;
			int a;
            int b;
			iss >> a;
			iss >> b;
			pair<int, int> p;
			p = make_pair(a, b);			
			pairVector.push_back(p);                      
            cout << "# of inserted pairs = " << pairVector.size() << endl;
            printVector(pairVector);
            sort(pairVector.begin(), pairVector.end());
            printVector(pairVector);
            vector<pair<int, int> >::iterator i = pairVector.begin();
            while (i != pairVector.end()) 
            {
                vector<pair<int, int> >::iterator j = i+1;
                if (j != pairVector.end() && *j == *i)  
                {
                    i = pairVector.erase(i);
                }        
            }
    printVector(pairVector);
    
    return pairVector.size();
    }  
    return 0;
}
     
     
int sba(string filename)
{ 
    int x;
    int y;
    pair<int, int> p1;
    istringstream is;
    set<pair<int, int> > set;
    ifstream myfile;
    myfile.open(filename.c_str());
    if (myfile.fail()) {
        cerr << "ERROR: Failed to open file " << filename << endl;
        myfile.clear();
    }   else {
        if (myfile.is_open()) {
            string line;
            while (getline(myfile,line) )
            {
                if (line[0] != '#') {
                    istringstream is(line);
                    is >> x;
                    is >> y;
                    if ( y > x) {
                        p1 = make_pair(x, y);
                    }   else {  
                        p1 = make_pair(y,x);
                    }
 //                   edgeSet.insert(p1);
                }
            }
//            cout << edgeSet.size() << '\n';
            myfile.close();
        }
    }
    return 1;
}

int main()
{
    
}

No error now...but now it shows for my SBA function:P

algos.cpp: In function ‘int vba(std::string)’:
algos.cpp:70:1: error: a function-definition is not allowed here before ‘{’ token
{
^
algos.cpp:103:12: error: expected ‘}’ at end of input
}
^
algos.cpp:103:12: warning: control reaches end of non-void function [-Wreturn-type]
}
^
If you cant get it its all good...i will fix it later on

Help appreciated :)
Sorry, I only half understand what you mean. Is "algos.cpp" a different file? What is the code which gives those errors?
My bad... its actually the algos.cpp file in which im writing this code..so i complie using cygwin..hence the format like that
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// ============================================================================
// algos.cpp
// ~~~~~~~~~~~~~~~~
// author: JOHN DOE
// - this is the ONLY file you can modify
// - feel free to include more headers if you need to
// ============================================================================
#include <iostream>
#include <sstream>
#include <stdexcept> // to throw exceptions if you need to
#include <fstream>   // to open & read from input file
#include <cstdlib>   // for atoi() if you want to use it
#include <set>       // for sba algorithm
#include <vector>    // for vba algorithm
#include <algorithm> // for vba algorithm
#include "Lexer.h"
#include "algos.h"
using namespace std; // BAD PRACTICE      

void printVector(vector<pair<int, int> > & myVec)
{
         vector<pair<int, int> >::iterator i;
    for (i = myVec.begin(); i != myVec.end(); ++i) {
        cout << "(" << i->first << ", " <<  i->second << ") ";
    }
    cout << endl;
}

int vba(string filename)
{
    ifstream ifs; 
    ifs.open(filename.c_str());   
	string line;
	while (getline(ifs, line)) {	
		if (line=="#") {
			cout << endl;
		}   else {
			istringstream iss;
			iss.clear();
			iss.str(line);
			int a;
            int b;
			iss >> a;
			iss >> b;
			pair<int, int> p;
			p = make_pair(a, b);
            vector<pair<int, int> > pairVector;			
			pairVector.push_back(p); 
              
            cout << "Number of inserted pairs = " << pairVector.size() << endl;
            sort(pairVector.begin(), pairVector.end());    
            vector<pair<int, int> >::iterator i = pairVector.begin();
            while (i != pairVector.end()) {
            vector<pair<int, int> >::iterator j = i+1;
            if (j != pairVector.end() && *j == *i)  {
            i = pairVector.erase(i);
            } else { 
            i++;
        }        
     }                                            
    return pairVector.size();
           } 
     }
return 0;
}
        
int sba(string filename)
{   
    string line;
    ifstream myfile;
    istringstream is;
    int x;
    int y;
    pair<int, int> p1;
    set<pair<int, int> > set;
    myfile.open(filename.c_str());
    if (myfile.fail()) {
        cerr << "ERROR: Failed to open file " << filename << endl;
        myfile.clear();
    }   else {
        if (myfile.is_open()) {
            while (getline(myfile,line) )
            {
                if (line[0] != '#') {
                    istringstream is(line);
                    is >> x;
                    is >> y;
                    if ( y > x) {
                        p1 = make_pair(x, y);
                    }   else {  
                        p1 = make_pair(y,x);
                    }

                    edgeSet.insert(p1);
                }
            }
            cout << edgeSet.size() << '\n';
            myfile.close();
        }
    }
    return 0;
    }








algos.cpp: In function ‘int sba(std::string)’:
algos.cpp:94:21: error: ‘edgeSet’ was not declared in this scope
edgeSet.insert(p1);
^
algos.cpp:97:21: error: ‘edgeSet’ was not declared in this scope
cout << edgeSet.size() << '\n';
^
Makefile:13: recipe for target 'algos.o' failed


help??
Last edited on
Topic archived. No new replies allowed.