Article View: att.lang.c++
Article #79Re: Pointers To A Class
From: js
Date: Wed, 05 Apr 2000 00:00
Date: Wed, 05 Apr 2000 00:00
209 lines
5198 bytes
5198 bytes
I just took a quick look at the code, but I think tha the problem you are
having is caused by the fact that you are not properly dereferencing your
pointer.
for example:
class Foo
{
...
public:
void DoSomething();
};
void TestFunction(Foo* F)
{
// to call a method on the object pointed to by F, you normally do it
like this:
F->DoSomething();
// NOT F.DoSomething();
// Alternatively you could ...
(*F).DoSomething(); // but that's somewhat convoluted.
}
// You may also want to use a reference to the created Foo object.
void TestFunction(Foo& F);
This approach has many of the advantages of a pointer.
I hope that helps,
New C Programmer <s_joanis@siebe-ene.com> wrote in message
news:8br6t6$3dc$1@bob.news.rcn.net...
>
> I am having a problem with passing pointers to a class.
>
> I have a class called stack that works fine but when I try to pass a
pointer
> to an instance
>
> of the class it gives me problems.
>
> I need to know how to get to the methods of the object when I get it to
the
> function and
>
> perhaps how to pass it.
>
>
> this is the sending function,
>
> below it is a snipit of the recieving function.
>
> #include "scj_arr.h"
> #include "stack.h"
> #include "ass_out.h"
> #include<iostream.h>
> #include<stdlib.h>
> #include<ctype.h>
> #include<fstream.h>
> #define MAX 50
> // Function Prototypes
> int file_to_stack(stack *my_stack1, stack *my_stack2);
> int order_stack(stack *my_stack1, stack *my_stack2);
> void init_array(const char init_val, char array[], int arr_size);
> int process_command();
> int check_op(char c);
> file://int out_to_file(stack my_stack1, stack my_stack2);
>
>
>
> void main()
> {
> int test=0;
> int quit=0;
> static stack my_stack1;
> static stack my_stack2;
> stack *ptr_to_stack1;
> stack *ptr_to_stack2;
>
> ptr_to_stack1 = &my_stack1;
> ptr_to_stack2 = &my_stack2;
>
>
> int finished = 0;
> cout << "\nAssembly Compiler Program\n";
> while (!quit)
> {
> test = file_to_stack(ptr_to_stack1, ptr_to_stack2);//stack 2 is
corrected
> test = order_stack(ptr_to_stack1, ptr_to_stack2); file://stack 1 is
> corrected
> test = out_to_file(ptr_to_stack1, ptr_to_stack2); file://use stack 1
>
>
> cout << endl;
> cout << endl;
> my_stack1.st_clear();
> cout << "All Done? 1=YES 0=NO \n";
> cin >> quit;
> }
> }
>
>
> file://here is one of the recieving functions
> int file_to_stack(stack *my_stack1, stack *my_stack2)
> {
> char c;
> char c1;
> int count2=0;
> int count3=0;
> int pass_fail = 0;
> FILE *stream;
> int numread;
> char item;
> char element[MAX];
> char temp_element[MAX];
> char temp_element2[MAX];
> char filename_in[80];
> int count = 0;
> int count1 = 0;
>
> init_array(NULL, element, MAX);
> init_array(NULL, temp_element, MAX);
> init_array(NULL, temp_element2, MAX);
>
> cout << "What is the name of the file you want to open?";
> cout << endl;
> cin >> filename_in;
> stream=fopen(filename_in, "rb");// open for read binary
>
>
>
> do /*create a stack of arrays of equations*/
> {
> numread = fread(&item, sizeof(char),1,stream);
> if ((isalnum(item)) || (ispunct(item)) && (isprint(item)))
> {
> element[count] = item;
> ++count;
> if ((item==';') && (isprint(item)) && (item>0))
> {
> strcpy(temp_element,element);
>
> for (count2=0; count2<count; count2++)
> {
> c = element[count2];
> c1 = element[count2+1];
> temp_element[count3]=c;
> temp_element2[0]=c1;
> ++count3;
>
> if (check_op(c1))
> {
> my_stack1.st_put( temp_element );
> my_stack1.st_put( temp_element2 );
> init_array(NULL, temp_element, MAX);
> init_array(NULL, temp_element2, MAX);
> count3 = 0;
> count2++;
> }
> }
> cout << element;
> cout << "\n";
> init_array(NULL, element, MAX);
> count = 0;
> item = NULL;
> }
> }
> } while (numread>0);
>
> cout << endl;
> my_stack1.st_dump();
> fclose(stream);
>
>
>
>
>
> return pass_fail;
> }
>
> I get this sort of error "left of .st_put requires class/struct/union
type"
> I understand
>
> that I have only the pointer to the stack in the function but I cannot
seem
> to dereferance
>
> the pointer. I would appreciate any help.
>
> Regards,
>
>
> New C++ Programmer
>
>
Technical Details
Message-ID:
<bLLG4.863$P12.96347@nntp0.chicago.il.ameritech.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8br6t6$3dc$1@bob.news.rcn.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d4ba,62524d9860e4dcd5 X-Google-Attributes: gid10d4ba,public From: "js" <jeeves_97@yahoo.com> Subject: Re: Pointers To A Class Date: 2000/04/05 Message-ID: <bLLG4.863$P12.96347@nntp0.chicago.il.ameritech.net> X-Deja-AN: 607184649 References: <8br6t6$3dc$1@bob.news.rcn.net> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Trace: nntp0.chicago.il.ameritech.net 954959943 199.179.162.32 (Wed, 05 Apr 2000 13:39:03 CDT) Organization: Ameritech.Net www.ameritech.net Complaints: abuse@ameritech.net X-MSMail-Priority: Normal NNTP-Posting-Date: Wed, 05 Apr 2000 13:39:03 CDT Newsgroups: att.lang.c++