Advertisement

How to point on a class?

Started by November 12, 2000 04:20 PM
12 comments, last by PrinceofZ 24 years ago
i have a class (looks about the same like below) and how can i point on this class, so that i can change the variables in a function class classname{ public: x; y; z; } classname array[100] void functionname(int *?????????) { *x++; *y--; *z++; } so that i can call this function functioname(&????????) Thanks in advance btw: these are my first steps with functions so that i can
u wouldnt understand a german quote :)
i think what you want to do is this:

  // the class definitionclass classname {    public:        int x;        int y;        int z;};// the functionvoid func1(classname *classptr){    classptr->x = 1;    classptr->y = 2;    classptr->z = 3;}// this is how you''d call the functionclassname classInstance;func1(&classInstance);  


hope that helped...

------------------------
IUnknown *pUnkOuter

"Try the best you can
try the best you can
the best you can is good enough"
--Radiohead
------------------------IUnknown *pUnkOuter"Try the best you cantry the best you canthe best you can is good enough" --Radiohead
Advertisement
I typed this in, but there is a message unresolved external, or something (i have the german version)
It's important to me that i have this class 250 times (something[250])i could call this function with a loop 250 times, but i'm sure there is another way to make this in this function


#include
class classname{
public:
int x;
int y;
int z;
};
classname something[250];
// the function
void printing(classname *classptr)
{
classptr->x++;
classptr->y++;
classptr->z++;
cout << classptr->x;
}
// this is how you'd call the function
void winmain()
{
classname classInstance;
(&classInstance);
}

btw: how do you make this white "VC++6 - like" background

Edited by - PrinceofZ on November 12, 2000 6:27:21 PM
u wouldnt understand a german quote :)
Did you get ''unresolved external _main or WinMain?

While programming for DOS you would use main() with either an int or void return value, in Windows the main function is a bit more complicated.

the declaration of it is...

//WINAPI is just __stdcall.

int WINAPI WinMain(
HINSTANCE hInst,
HINSTANCE hPrevInst,
LPSTR lpCmdLine,
int nStyle){

//insert code here

return 0;
}

for my demonstrative apps in Windows, I typically use MessageBox to display values, and use wsprintf to convert numbers to ascii.

int MessageBox(HWND hWnd,LPSTR lpText,LPSTR lpTitle,int nStyle);

eg MessageBox(0,"something","title",0);

the first 0 means this HWND, and the second is the constant MB_OK.

So the total code as I would write it would be something like the following

*don''t forget your <''s and >''s and their ;''s*

I use Borland BTW, but it shouldn''t make a difference#include

#include <windows.h>

class classname{
public:
int x;
int y;
int z;
};

classname something[250];
char buffer[1024];

// the function
//actually I''d pass a pointer and a integer saying how many

void printing(classname *classptr){
for(int n=0;n<250;n++){
classptr[n].x++;
classptr[n].y++;
classptr[n].z++;
wsprintf(buffer,
"Class number %d\n\nX: %d\nY: %d\nZ: %d\n",
n,classptr[n].x,classptr[n].y,classptr[n].z);
MessageBox(0,buffer,"TITLE",0);
}}

// this is how you''d call the function
int WINAPI WinMain(
HINSTANCE hInst,
HINSTANCE hPrevInst,
LPSTR lpCmdLine,
int nStyle){
printing(something);
}


This probably needs to be tinkered with a little, I haven''t really tested it, though it should work, ignoring typos, that may or may not be there.



Now I get to see if my name''s still here, and if I got the password and what not right.
"It is one thing to use poorly coded programs. It is another to produce poorly coded programs" -- Me, as far as I know.
i typed this in, but there are some errors with unresolved externals and so on. I really would appriciate if you would test this and say me how to do this, i have no clue how to translate the message into english...
And i need this code

  #include <windows.h>class classname{ public:int x; int y; int z;};classname something[250];char buffer[1024];// the function//actually I''d pass a pointer and a integer saying how manyvoid printing(classname *classptr){ for(int n=0;n<250;n++){classptr[n].x++;classptr[n].y++;classptr[n].z++;wsprintf(buffer,"Class number %d\n\nX: %d\nY: %d\nZ: %d\n",n,classptr[n].x,classptr[n].y,classptr[n].z);MessageBox(0,buffer,"TITLE",0);}}// this is how you''d call the functionvoid WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nStyle){printing(something); }  



u wouldnt understand a german quote :)
It''s usually a good idea to keep all the variables as privates and have public functions that can access them... ie.

  class ClassName(){public:  ClassName();  void function();private:  int x, y, z;};//ClassName::ClassName(){  x = 0;  y = 0;  z = 0;}//void ClassName::function(){  x++;  y++;  z++;}////WinMain etc etc...//{  const int ClassSize = 256;  ClassName Class[ClassSize];  for (int count = 0; count < ClassSize; count++)  {    Class.function();  }}  


There, that should do it.. I haven''t done much programming lately tho, so I may be rusty.

S.
Advertisement
Ok..... the only problem is you forgot to make windows return a value. Changing it from void WINAPI Windows(blah, blah) to int WINAPI Windows(blah, blah) makes it work perfectly. The following code compiles and works under msvc 6.0 w/service pack 4


mheyman

#include


class classname
{
public:int x; int y; int z;
};

classname something[250];
char buffer[1024];
// the function
//actually I''d pass a pointer and a integer saying how many

void printing(classname *classptr)
{
for(int n=0;n<250;n++)
{
classptr[n].x++;
classptr[n].y++;
classptr[n].z++;
wsprintf(buffer,"Class number %d\n\nX: %d\nY: %d\nZ: %d\n",
n,classptr[n].x,classptr[n].y,classptr[n].z);MessageBox(0,buffer,"TITLE",0);
}
}

// this is how you''d call the function
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nStyle)
{
printing(something);
return true;

}


It works, thanks

(btw: can someone tell me how to create a headerfile and compile? i have to mo the .h file into the include directory, and i have no idea how to include the headerfile in MY directory)
u wouldnt understand a german quote :)
#include "includefile"

use quotation marks
i create under new a headerfile, write my stuff in it and in my cpp file include "headername.h", i know, but when i try to compile i get the error: header could not be founded, but when i move or copy the file into the include folder it works fine, but its unpracticle to move and copy ... before compiling (especially as a newbie i compile after nearly every line of code to test)
u wouldnt understand a german quote :)

This topic is closed to new replies.

Advertisement