error compiling this code

G

Gary Wessle

Hi

I have been trying to find out the reason for this error for an 1hr,
please look at it for me.

thanks

**************** error ****************
$ make; ./proj
g++ -gdwarf-2 -c -o gen_data.o gen_data.cpp
gen_data.cpp: In member function 'void gen_data::gen_mngr()':
gen_data.cpp:31: error: expected primary-expression before ')' token
gen_data.cpp:32: error: expected primary-expression before '->' token
gen_data.cpp:32: error: expected primary-expression before '->' token
gen_data.cpp: At global scope:
gen_data.cpp:40: error: default argument given for parameter 2 of 'void gen_data::eek:nce_gen(std::string, double)'
gen_data.h:17: error: after previous specification in 'void gen_data::eek:nce_gen(std::string, double)'
make: *** [gen_data.o] Error 1



**************** gen_data.h ****************
#ifndef GEN_DATA_H
#define GEN_DATA_H
#include <string>
using std::string;

#include <map>
using std::map;

class gen_data
{
const map<string,double> cur_pairs;
string parent_dir;
void gen_mngr();
void once_gen(string, double val=99);

public:
gen_data(const map<string,double>&);
~gen_data();
};

#endif


**************** gen_data.cpp ****************
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::endl;

#include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
#include "boost/filesystem/fstream.hpp" // ditto
#include "gen_data.h"
namespace fs = boost::filesystem;

gen_data::~gen_data(){}

gen_data::gen_data(const map<string,double> & m)
:parent_dir("../data"), cur_pairs(m)
{
gen_mngr();
}

void gen_data::gen_mngr(){
fs::path data_dir(parent_dir);
if (!exists(data_dir))//needs an excption handler
cout << parent_dir << " does not exist" << endl;

if (cur_pairs.size()==0){
fs::directory_iterator end_iter;
for(fs::directory_iterator dir_itr(parent_dir); dir_itr != end_iter; ++dir_itr )
once_gen(dir_itr->leaf());
} else {
typedef map<string,double>::const_iterator mi;
for (mi p = cur_pairs.begin(); p != cur_pairs.end(); ++mi)
once_gen(mi->first, mi->second);
}
}

void gen_data::eek:nce_gen(string cur_p, double val=99){
cout << cur_p << endl;
}


**************** main.cpp ****************
#include <string>
using std::string;

#include <map>
using std::map;

#include "gen_data.h"

int main(){
map <string,double> m;
m["mothers"]=3;

gen_data gen_job(m);

}
 
B

Bo Persson

Gary Wessle said:
Hi

I have been trying to find out the reason for this error for an 1hr,
please look at it for me.

thanks

**************** error ****************
$ make; ./proj
g++ -gdwarf-2 -c -o gen_data.o gen_data.cpp
gen_data.cpp: In member function 'void gen_data::gen_mngr()':
gen_data.cpp:31: error: expected primary-expression before ')' token
gen_data.cpp:32: error: expected primary-expression before '->'
token
gen_data.cpp:32: error: expected primary-expression before '->'
token
gen_data.cpp: At global scope:
gen_data.cpp:40: error: default argument given for parameter 2 of
'void gen_data::eek:nce_gen(std::string, double)'
gen_data.h:17: error: after previous specification in 'void
gen_data::eek:nce_gen(std::string, double)'
make: *** [gen_data.o] Error 1



**************** gen_data.h ****************
#ifndef GEN_DATA_H
#define GEN_DATA_H
#include <string>
using std::string;

#include <map>
using std::map;

class gen_data
{
const map<string,double> cur_pairs;
string parent_dir;
void gen_mngr();
void once_gen(string, double val=99);

public:
gen_data(const map<string,double>&);
~gen_data();
};

#endif


**************** gen_data.cpp ****************
[]

void gen_data::eek:nce_gen(string cur_p, double val=99){
cout << cur_p << endl;
}

For some reason, we are not allowed to repeat the default argument in
the definition of the function. I have never understood exactly why,
but that is the rule.

Just make it "double val /*=99*/" here, and it will work. The default
value will be taken from the function declaration.

Bo Persson
 
R

Ron Natalie

Gary said:
if (cur_pairs.size()==0){

This by the way isn't very efficient if the map has entries.
Try cur_pairs.empty()
fs::directory_iterator end_iter;
for(fs::directory_iterator dir_itr(parent_dir); dir_itr != end_iter; ++dir_itr )

Try using
dir_itr(data_dir).
The implicit conversion from string to path here may be messing
up...this is the root of the next three errorrs.
void gen_data::eek:nce_gen(string cur_p, double val=99){

Default args can only be declared once (you've got it in the header).
 
G

Gary Wessle

Ron Natalie said:
This by the way isn't very efficient if the map has entries.
Try cur_pairs.empty()


Try using
dir_itr(data_dir).
The implicit conversion from string to path here may be messing
up...this is the root of the next three errorrs.


Default args can only be declared once (you've got it in the header).

thanks for the suggestions, I have implemented them but the errors are
still there, the suggestions were related to lines in the code
different from the lines whose numbers are in the errors, lines 31,32
are those here

for (mi p = cur_pairs.begin(); p != cur_pairs.end(); ++mi)
once_gen(mi->first, mi->second);

which look fairly clean to me.

1 #include <string>
2 using std::string;
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
8 #include "boost/filesystem/fstream.hpp" // ditto
9 #include "gen_data.h"
10 namespace fs = boost::filesystem;
11
12 gen_data::~gen_data(){}
13
14 gen_data::gen_data(const map<string,double> & m)
15 :parent_dir("../data"), cur_pairs(m)
16 {
17 gen_mngr();
18 }
19
20 void gen_data::gen_mngr(){
21 fs::path data_dir(parent_dir);
22 if (!exists(data_dir))//needs an excption handler
23 cout << parent_dir << " does not exist" << endl;
24
25 if (cur_pairs.empty()){
26 fs::directory_iterator end_iter;
27 for(fs::directory_iterator dir_itr(data_dir); dir_itr != end_iter; ++dir_itr )
28 once_gen(dir_itr->leaf());
29 } else {
30 typedef map<string,double>::const_iterator mi;
31 for (mi p = cur_pairs.begin(); p != cur_pairs.end(); ++mi)
32 once_gen(mi->first, mi->second);
33 }
34
35 }
 
G

Gary Wessle

Gary Wessle said:
thanks for the suggestions, I have implemented them but the errors are
still there, the suggestions were related to lines in the code
different from the lines whose numbers are in the errors, lines 31,32
are those here

for (mi p = cur_pairs.begin(); p != cur_pairs.end(); ++mi)
once_gen(mi->first, mi->second);

which look fairly clean to me.

1 #include <string>
2 using std::string;
3 #include <iostream>
4 using std::cout;
5 using std::endl;
6
7 #include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
8 #include "boost/filesystem/fstream.hpp" // ditto
9 #include "gen_data.h"
10 namespace fs = boost::filesystem;
11
12 gen_data::~gen_data(){}
13
14 gen_data::gen_data(const map<string,double> & m)
15 :parent_dir("../data"), cur_pairs(m)
16 {
17 gen_mngr();
18 }
19
20 void gen_data::gen_mngr(){
21 fs::path data_dir(parent_dir);
22 if (!exists(data_dir))//needs an excption handler
23 cout << parent_dir << " does not exist" << endl;
24
25 if (cur_pairs.empty()){
26 fs::directory_iterator end_iter;
27 for(fs::directory_iterator dir_itr(data_dir); dir_itr != end_iter; ++dir_itr )
28 once_gen(dir_itr->leaf());
29 } else {
30 typedef map<string,double>::const_iterator mi;
31 for (mi p = cur_pairs.begin(); p != cur_pairs.end(); ++mi)
32 once_gen(mi->first, mi->second);
33 }
34
35 }

the fix, "I feel stupid"

31 for (mi p = cur_pairs.begin(); p != cur_pairs.end(); ++p)
32 once_gen(p->first, p->second);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top