S
sd2004
I am still learning, could someone
show/explain to me how to fix the 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
show/explain to me how to fix the 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