Hello, i am very interested in the c++ language, i decided to make my own small program and test how arrays work with classes with private variables and private operations and then calling them any way i'd like from the main function.

a very big reason about this is probably because i'm trying to pass by reference, which i thought was necesary if i'm keeping the changes everywhere within the program, and using a void function. If there is any advice as to coding better, i'd like those too.
there are syntax errors, line 20 only, please help me :)

////.header file for class arrays.
#include <string>
using namespace std;

class arrays {
private:
    string hold;
    int keep;
    int raekwon[];
public:
void Talk(string&, int&);

};
///this is my arrays header function
#include "arrays.h"


void arrays::Talk(hold&, keep&) {
///this creates the array based from the size parameter
    raekwon[keep];
    
    //this displays the information
    cout << "okay, you are feeling " << hold << " today.\n";
    cout << "also, the size of the array you asked for is " << keep << "\n";
    cout << "the elements in the array are: " << raekwon << "\n";


}
///this is the main function

#include "arrays.h"
#include <iostream>
#include <string>

using namespace std;

main()
{
    arrays a;
    string feelings;
    int array_size;
    
    cout << "how are you feeling today?\n";
    cin >> feelings;
    cout << "\n" << "what size array you want?\n";
    cin >> array_size;

    a.Talk(feelings&, array_size&);
    return 0;
}

//////////////////////////////////////////////////

Edited 6 Years Ago by rcmango: n/a

>>void arrays::Talk(hold&, keep&) {
from line 5. You need to add the data type of each of those parameters, such as void arrays::Talk(string &hold,int &keep) { >>a.Talk(feelings&, array_size&);

Remove the two & symbols.

Edited 6 Years Ago by Ancient Dragon: n/a

First welcome to the board and using code tags on your first post!

void arrays::Talk(hold&, keep&)

The correct syntax would be:

void arrays::Talk(string& hold, int& keep)

However, since both hold and keep are member variables they are accessable by all member functions irrespective of whether hold and keep were declared with public or private, or protected for that matter, access. The issue of access only has to do with accessing the material outside of the class, like in main() or some other class, etc. Further, even if Talk wasn't a class method, unless you wanted to change the value of hold or keep within Talk() there is no reason to pass them by reference, unless hold was very large.

cout << "the elements in the array are: " << raekwon << "\n";

To display the elements in raekwon you should use a loop to display each individual element you want displayed. If anything, the above will print the first element of the array, only, but it is more likely to print the address of the first element of the array. In any event, it won't display the contents of the array.

a.Talk(feelings&, array_size&);

If you want to pass something by reference, using a reference argument, just pass the variable, like this:

a.Talk(feelings, array_size);

However, as indicated above, you don't need to pass either to Talk() since Talk() is member method.

Note, if you use a pointer to pass arguments by reference to a function, then you would need to pass the address of the variable, which would have the & operator in front of the variable name, if it's needed at all (for example, the & operator wouldn't be needed to pass a one dimensional array since the name of the array is the address of the first element of the array).

Comments
thanks alot, very helpful input.

thanks alot, great input, ya i was trying to pass variables by reference, while not intentionally using pointers. i see i just forgot the types where my amperands should have been.

....oh, some more questions, because i am asking the user for how they feel and the size of the array. I must pass this information to my function because those member variables are private and can't be accessed from the main. Am i correct on this?
I could write this differently, but i did this intentionally to understand what was going on better.

also, i think i've seen that if i just print an array like:
cout << array //this will show the first memory block
cout << array[a number] //it will print out 5 random numbers that randomly happen to be in the memory of the array.
cout << array[5] //with a loop then if i would assign numbers to it using your method with a while loop or the like, that would be what i want.
....i think there is a way to assign it new memory though right? right off the bat? dynamic or something.

thanks alot.

Edited 6 Years Ago by rcmango: n/a

This article has been dead for over six months. Start a new discussion instead.