🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by New C Programmer Tue, 28 Mar 2000 00:00
Pointers To A Class
#76
Author: New C Programmer
Date: Tue, 28 Mar 2000 00:00
158 lines
4012 bytes
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
Re: Pointers To A Class
#79
Author: js
Date: Wed, 05 Apr 2000 00:00
209 lines
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
>
>
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads