Plz help

closed account (N8MNAqkS)
so here's my problem. despite making the class "PointerC" public, I cant use it anywhere else. basicly all this program does is print pointer values to the screen, but I cant do that unless the function "void Pointer()"(the function containing the code) is inside of the main function because for whatever reason printable code needs to be in there, anything I wright with "cout <<" has to be in the main function. but I cant put "Pointer()" in "int main()" or else it gives me an error that says it cant be redefined outside of its class. so what am I doing wrong.

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
 #include "pch.h"
#include<iostream>
#include "Header.h"
using namespace std;

void PointerC::Pointer()
{

	int * pointer;
	int v = 25;
	pointer = &v;
	int x = *pointer;

	cout << x << endl;
	cout << *pointer << endl;
	cout << pointer << endl;
};

int main()
{

	
	
	
	return 0;
}


{Header file}

#include <iostream>
#include "pch.h"

using namespace std;



class PointerC
{
public:

void Pointer();

};
Well if you've got a class, you need to create an instance of that class in order to be able to use it.

1
2
3
4
int main ( ) {
    PointerC  myThingamajig;  // make a thing
    myThingamajig.Pointer();  // call a method to make use of that thing.
}
closed account (N8MNAqkS)
thank you this worked. Although if u could, please explain what this instance does. why does putting any random word in front of a class make its function usable.
Well I can describe a kettle to you, and tell you that you can boil water with it.
But if you actually want to boil some water, then you need an actual kettle to do it.

1
2
3
4
5
6
7
class Kettle {
  double capacity;  // max capacity
  double amount;  // amount of water already present
public:
  void boil(double volumeInLitres);
  double pour(double amountToPour);
};

OK, so now you know you can boil water with a kettle.


I need a cup of tea.
1
2
3
4
5
int main ( ) {
    Kettle myKettle;
    myKettle.boil(1.2);  // Boil 1.2L of water for me.
    myCup.fill(myKettle.pour(0.3L));
}
or, another way of looking at it:

class int
{
...
};

int x = 3; //look familiar?
int = 3; //this won't work.

a class is (at one level of understanding) a user defined TYPE. Int is a pre-defined type. But thinking about it at this level, it may be easier to tell the difference between the type (int) and the object of the type (x). You need the actual object to do anything; int by itself in code is not very useful. (No, you can't make class int because int is a keyword, but I was making a point).
c++ has useful built in things you can use, like variables. Take an int for example. It can hold a number for you, but before you can use it you will have to declare an int and give it a name.

Something like... int myint;

What you did was create your own new thing, not already built into c++. You called it a PointerC. And just like ints, before you can use your PointerC, you will need to declare one and give it a name. Salem named one myThingamajig, with this line... PointerC myThingamajig;

Does this help?
closed account (N8MNAqkS)
(i named mythingamajig pointerInstance in my program) Ok so if I am understanding correctly::

To compare to "int"

If Int is calling a variable, then PointerC is like int.

If x is the variable, then pointerInstance is like x.

And if 3 is us initializing x, or us giving x a value, then Pointer() is us initializing pointerInstance. Right?

Or:

Int x = 3;

=

PointerC pointerInstance;
pointerInstance.Pointer();



Last edited on
> If Int is calling a variable, then PointerC is like int.
We say 'int' and 'PointerC' are types.

> And if 3 is us initializing x, or us giving x a value,
> then Pointer() is us initializing pointerInstance. Right?
You're not initializing pointerInstance, you're calling a method.
Topic archived. No new replies allowed.