473,546 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing argument in STL loop ?

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(astruc t&)'
test5c.cpp: In function `void dog_name(astruc t&)':
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<iostre am>
#include <string>
#include<vector >
#include<sstrea m>
#include<fstrea m>
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,lin e)){
istringstream anyname(line);
anyname>>astr.n ame>>astr.id>>a str.type;
v.push_back(ast r);
}
vector<astruct> ::iterator search;
for (search=v.begin ();search!=v.en d();++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

Jan 20 '06 #1
3 2365
sd2004 wrote:
could you also recommend a book that I can ref. to ?


To learn: Koenig & Moo, Accelerated C++
For reference: Stroustrup, The C++ Programming Language

Best regards,

Tom

Jan 21 '06 #2
sd2004 <tv****@hotmail .com> wrote:
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. ////////////////// 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(astruc t&)'
Here is your function prototype: void dog_name (astruct& astructref);
Here is your definition of astruct: struct astruct
{
string name;
int id;
string type;
};
This is your iterator: vector<astruct> ::iterator search;
Here is how you call dog_name: dog_name(search->name);
search->name is equivalent to (*search).name, and (*search) is of type
astruct, so you are passing the "name" member (which is of type string)
of the astruct associated with the current iterator. Therefore you are
passing a string into your dog_name() function, but dog_name() is
expecting a reference to an astruct.
test5c.cpp: In function `void dog_name(astruc t&)':
test5c.cpp:49: error: overloaded function with no contextual type
information
test5c.cpp:49: error: base operand of `->' is not a pointer void dog_name (astruct& s){
cout <<search->name <<" is GoldenRetriever "<<endl;
}
You use operator-> with pointers (and pointer-like types, like
iterators) to refer to members of structs or classes. You use operator.
to refer to members of structs or classes when they are actual objects,
or references to objects. Also, you are using the wrong variable in
your function, and the parameter should be const since you are not
changing it.
could you also recommend a book that I can ref. to ?


The C++ FAQ is a good source of information:
http://www.parashift.com/c++-faq-lite/

There is also the newsgroup:
alt.comp.lang.l earn.c-c++
which is more geared towards beginners.

For book recommendations , check out:
http://www.accu.org/

--
Marcus Kwok
Jan 21 '06 #3
Thanks for taking your time. I really appreciated.
After reading your thread. I did fixed the code and it worked.

Jan 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3949
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
5
34346
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would...
3
14914
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
3
22844
by: K. Deepa | last post by:
Hi all, I am using postgresql7.4. How to handle arrays in plpgsql. How can I pass an array. Is it possible to retrieve values from an array by indexing it like argument : '{1,2,3}' Return value : varchar array Variables : ---------
3
2847
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); ...
10
3131
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I...
5
5822
by: jlea | last post by:
I'm trying to pass a filename, obtained with using the fileName property from the OpenFileDialog, as a application parameter in Process.StartInfo.Arguments and run a MFC/C++ application using the Start method. When I hardcode the application parameter such as "/name=c:\\myFile.txt" all is well in the C++ application. When I use the...
4
2493
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
8
3488
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.