Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > trouble accessing into a referenced variable

Reply
Thread Tools

trouble accessing into a referenced variable

 
 
Alan
Guest
Posts: n/a
 
      11-13-2006
I`m having trouble figuring out the correct syntax for accessing an
element of a struct variable (t_list) passed by reference to a
function. The compiler does not like the code below when I added the
"&" in the call and the "*" in the body of the function. It provides
the error: "expected primary-expression before '.' token"

How do you properly use a variable passed by reference to access
into its structure?

Thanks in advance, Alan

struct report
{
int time;
double xcor; double ycor; double speed;
};

void add_report (report t, vector<report>& t_list)
{
bool found = false;
for (int i = 0; i < t_list*.size(); i++)
if (t.time == t_list[i]*.time)
. . .

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      11-13-2006
"Alan" <> wrote:

> I`m having trouble figuring out the correct syntax for accessing an
> element of a struct variable (t_list) passed by reference to a
> function.


See below:

> struct report
> {
> int time;
> double xcor; double ycor; double speed;
> };
>
> void add_report (report t, vector<report>& t_list)
> {
> bool found = false;

for (int i = 0; i < t_list.size(); i++)
if (t.time == t_list[i].time)
> . . .


--
To send me email, put "sheltie" in the subject.
 
Reply With Quote
 
 
 
 
Nindi
Guest
Posts: n/a
 
      11-13-2006

Alan wrote:
> I`m having trouble figuring out the correct syntax for accessing an
> element of a struct variable (t_list) passed by reference to a
> function. The compiler does not like the code below when I added the
> "&" in the call and the "*" in the body of the function. It provides
> the error: "expected primary-expression before '.' token"
>
> How do you properly use a variable passed by reference to access
> into its structure?
>
> Thanks in advance, Alan
>
> struct report
> {
> int time;
> double xcor; double ycor; double speed;
> };
>
> void add_report (report t, vector<report>& t_list)
> {
> bool found = false;
> for (int i = 0; i < t_list*.size(); i++)
> if (t.time == t_list[i]*.time)


This works for me

struct report
{
int time;
double xcor; double ycor; double speed;
};

void add_report (report t, vector<report>& t_list)
{
bool found = false;
int i =0;
for (int i = 0; i < t_list.size(); i++)
if (t.time == t_list[i].time) i = 0;
}


I am not sure if I misunderstand but I do not know why you are using *

N

 
Reply With Quote
 
Alan
Guest
Posts: n/a
 
      11-13-2006
Daniel,
Thanks! All the simpler examples of passing by reference
had examples like:

In function declaration: int& a

In function body: a* = 2

so I thought I needed it here. Duh! Alan

 
Reply With Quote
 
Nindi
Guest
Posts: n/a
 
      11-13-2006

Alan wrote:
> Daniel,
> Thanks! All the simpler examples of passing by reference
> had examples like:
>
> In function declaration: int& a
>
> In function body: a* = 2
>
> so I thought I needed it here. Duh! Alan


no .. a*=2 is actualy a=a*2

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
local variable referenced before assignment Pete Bartonly Python 10 10-25-2007 05:08 PM
Accessing namespaces referenced in codebehind of an ascx file newjazzharmony@hotmail.com ASP .Net 2 05-23-2006 06:13 PM
UnboundLocalError: local variable 'colorIndex' referenced silverburgh.meryl@gmail.com Python 2 02-26-2006 09:29 PM
non-static variable this cannot be referenced from a static context kookey Java 3 08-20-2005 07:43 PM
HELP:UnboundLocalError: local variable '_nntp' referenced before assignment Peter Moscatt Python 2 03-19-2005 08:37 AM



Advertisments
 



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