STL ?

S

sd2004

Hi,
I am getting compiling error on the following line.
I could not figure it out.

Could someone please kindly help me fixing the error ?
//////////////////////////// Error message ////////////////////
bash-2.05b$ g++ test5c.cpp
test5c.cpp:20: error: parse error before numeric constant
test5c.cpp: In function `int main()':
test5c.cpp:36: error: `11' cannot be used as a function
test5c.cpp: At global scope:
test5c.cpp:48: error: parse error before numeric constant
////////////////// Below is the code ////////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

struct astruct
{
string name;
int id;
string type;
};

#define goldenretriever 11
#define Lab 33
#define Boxer 44
#define Terrier 55

void goldenretriever (astruct& astructref); //line 20
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (getline(in,line)){
istringstream anyname(line);
anyname>>astr.name>>astr.id>>astr.type;
v.push_back(astr);
}
vector<astruct>::iterator search;
for (search=v.begin();search!=v.end();++search){
switch (search->id){
case goldenretriever:
goldenretriever(search->name); //line 36
break;
case Lab:
cout <<search->name <<" is Lab. "<<endl;
default:
break;
break;
}
}
return 0;

}
void goldenretriever (search->name& s){ //line 48
cout <<s->name <<" is GoldenRetriever "<<endl;
//cout <<search->name <<" is GoldenRetriever "<<endl;
}

//////////////////////// input "test5c.txt"
//////////////////////////////////

cricket 11 GoldenRetriever
nitro 11 GoldenRetriever
Maxtor 33 Lab
Aaxtor 44 Boxer
Dora 55 Terrier
 
S

Shark

sd2004 said:
Hi,
I am getting compiling error on the following line.
I could not figure it out.

Could someone please kindly help me fixing the error ?
//////////////////////////// Error message ////////////////////
bash-2.05b$ g++ test5c.cpp
test5c.cpp:20: error: parse error before numeric constant
test5c.cpp: In function `int main()':
test5c.cpp:36: error: `11' cannot be used as a function
test5c.cpp: At global scope:
test5c.cpp:48: error: parse error before numeric constant
////////////////// Below is the code ////////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

struct astruct
{
string name;
int id;
string type;
};

#define goldenretriever 11

***************** Ok, you defined "goldenretriever" here
*****************
#define Lab 33
#define Boxer 44
#define Terrier 55

void goldenretriever (astruct& astructref); //line 20

***************** and you are declaring one again! *****************
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (getline(in,line)){
istringstream anyname(line);
anyname>>astr.name>>astr.id>>astr.type;
v.push_back(astr);
}
vector<astruct>::iterator search;
for (search=v.begin();search!=v.end();++search){
switch (search->id){
case goldenretriever:
goldenretriever(search->name); //line 36

***************** fix the above first *****************
 
M

Mark P

sd2004 said:
Hi,
I am getting compiling error on the following line.
I could not figure it out.

Could someone please kindly help me fixing the error ?
//////////////////////////// Error message ////////////////////
bash-2.05b$ g++ test5c.cpp
test5c.cpp:20: error: parse error before numeric constant
test5c.cpp: In function `int main()':
test5c.cpp:36: error: `11' cannot be used as a function
test5c.cpp: At global scope:
test5c.cpp:48: error: parse error before numeric constant

The error message pretty much tells you what the problem is...
////////////////// Below is the code ////////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

struct astruct
{
string name;
int id;
string type;
};

#define goldenretriever 11

The preprocessor will replace all occurences of goldenretriever with 11.
#define Lab 33
#define Boxer 44
#define Terrier 55

void goldenretriever (astruct& astructref); //line 20

becomes:
void 11 (astruct& astructref); // not a valid function declaration

You can't begin a function name with a digit.
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (getline(in,line)){
istringstream anyname(line);
anyname>>astr.name>>astr.id>>astr.type;
v.push_back(astr);
}
vector<astruct>::iterator search;
for (search=v.begin();search!=v.end();++search){
switch (search->id){
case goldenretriever:
goldenretriever(search->name); //line 36

becomes:
11(search->name); // same problem, illegal function name
break;
case Lab:
cout <<search->name <<" is Lab. "<<endl;
default:
break;
break;
}
}
return 0;

}
void goldenretriever (search->name& s){ //line 48

becomes:
void 11 (search->name& s) {

You can't use 11 as a function name (and you can't use search->name as
an argument type either, but the compiler gave up before it even noticed
that error)
 
S

sd2004

I fixed the first error. I am still learning, could someone
show/explain to me how to fix the next error.
I can see it is being wrong but do not know how to fix.
could you also recommend a book that I can ref. to ?
////////////////// ERROR MESSAGE /////////////////////

bash-2.05b$ g++ test5c.cpp
test5c.cpp: In function `int main()':
test5c.cpp:36: error: invalid initialization of reference of type
'astruct&'
from expression of type 'std::string'
test5c.cpp:20: error: in passing argument 1 of `void
dog_name(astruct&)'
test5c.cpp: In function `void dog_name(astruct&)':
test5c.cpp:49: error: overloaded function with no contextual type
information
test5c.cpp:49: error: base operand of `->' is not a pointer
bash-2.05b$
////////////////////// SOURCE CODE //////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

struct astruct
{
string name;
int id;
string type;
};

#define goldenretriever 11
#define Lab 33
#define Boxer 44
#define Terrier 55

void dog_name (astruct& astructref);
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (getline(in,line)){
istringstream anyname(line);
anyname>>astr.name>>astr.id>>astr.type;
v.push_back(astr);
}
vector<astruct>::iterator search;
for (search=v.begin();search!=v.end();++search){
switch (search->id){
case goldenretriever:
dog_name(search->name);
break;
case Lab:
cout <<search->name <<" is Lab. "<<endl;
default:
break;
break;
}
}
return 0;

}
void dog_name (astruct& s){
cout <<search->name <<" is GoldenRetriever "<<endl;
}

////////////////////// INPUT FILE "test5c.txt" ///////////////////

cricket 11 GoldenRetriever
nitro 11 GoldenRetriever
Maxtor 33 Lab
Aaxtor 44 Boxer
Dora 55 Terrier
 
J

Jim Langston

sd2004 said:
I fixed the first error. I am still learning, could someone
show/explain to me how to fix the next error.
I can see it is being wrong but do not know how to fix.
could you also recommend a book that I can ref. to ?
////////////////// ERROR MESSAGE /////////////////////

bash-2.05b$ g++ test5c.cpp
test5c.cpp: In function `int main()':
test5c.cpp:36: error: invalid initialization of reference of type
'astruct&'
from expression of type 'std::string'
test5c.cpp:20: error: in passing argument 1 of `void
dog_name(astruct&)'
test5c.cpp: In function `void dog_name(astruct&)':
test5c.cpp:49: error: overloaded function with no contextual type
information
test5c.cpp:49: error: base operand of `->' is not a pointer
bash-2.05b$
////////////////////// SOURCE CODE //////////////////////////////
#include<iostream>
#include <string>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;

struct astruct
{
string name;
int id;
string type;
};

#define goldenretriever 11
#define Lab 33
#define Boxer 44
#define Terrier 55

void dog_name (astruct& astructref);
int main()
{
vector<astruct> v;
astruct astr;
ifstream in ("test5c.txt");
string line;
while (getline(in,line)){
istringstream anyname(line);
anyname>>astr.name>>astr.id>>astr.type;
v.push_back(astr);
}
vector<astruct>::iterator search;
for (search=v.begin();search!=v.end();++search){
switch (search->id){
case goldenretriever:
dog_name(search->name);

dog_name is a function accepting a reference to astruct. But you are
calling it passing name, which is a std::string. The compiler tries to make
a reference to a astruct out of your std::string and cant, and that's your
error:
" test5c.cpp:36: error: invalid initialization of reference of type
'astruct&'
from expression of type 'std::string'"

You need to pass to the function a reference to your class, not a method of
your class. I believe you want here:

dog_name(search);
or
dog_name(*search);

not positive which. Either one will most likely work, I'm not sure how
happy the compiler will be converting an iterator to a reference, but it
should convert the contents of the iterator fine.
break;
case Lab:
cout <<search->name <<" is Lab. "<<endl;
default:
break;
break;
}
}
return 0;

}
void dog_name (astruct& s){
cout <<search->name <<" is GoldenRetriever "<<endl;
}

////////////////////// INPUT FILE "test5c.txt" ///////////////////

cricket 11 GoldenRetriever
nitro 11 GoldenRetriever
Maxtor 33 Lab
Aaxtor 44 Boxer
Dora 55 Terrier

Incidently, I am just pointing out what was wrong with your compiling, not
your program's logic errors. I actually didn't look at it that close.
 

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

Similar Threads

passing argument in STL loop ? 3
STL for loop ? 4
STL: copy vector of class ? 18
Another STL ? 3
STL ?? 2
Minimum Total Difficulty 0
Filter sober in c++ don't pass test 0
vector of struct ? 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top