I
I
Islam Aliev2021-01-24 00:04:24
OOP
Islam Aliev, 2021-01-24 00:04:24

Calling a function from a class?

I want to call the getValues ​​function from the Properties class . Unfortunately, I can not understand the error that is happening ... Who knows, write any option. Plizz)
I will also correct the code.

#include <iostream>

class Properties
{
public:
  int a = 13;
  int b = 82;
  float c = 27.01f;
  void getValues(Properties* objectValues);
};

void Properties::getValues(Properties* objectValues) // именно эту...
{
  std::cout << objectValues->a << std::endl;
}

int main()
{
  setlocale(0, "");
  Properties* objectValues;
  objectValues->getValues(); // неужели так нельзя?
  std::cin.get();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Solomennikov, 2021-01-24
@Devrains

You have an error in calling the getValues ​​method because an argument is expected: void getValues(Properties* objectValues);.
Create an object and pass a reference to it.

Properties obj;
Properties* objectValues = new Properties;
objectValues->getValues(&obj);

UPD : If you don't need to indirectly access an object through a pointer, but want to access a member of an object through a pointer, then you can do this:
#include <iostream>

class Properties
{
public:
  void getValues();

  int a = 13;
  int b = 82;
  float c = 27.01f;
};

void Properties::getValues()
{
  std::cout << a << std::endl;
}

int main()
{
  setlocale(0, "");
  
  Properties* ptr = new Properties;
  ptr->getValues();
  
  std::cin.get();
  
  return 0;
}

You can also access a member of an object without a pointer, using dot notation:
#include <iostream>

class Properties
{
public:
  void getValues();

  int a = 13;
  int b = 82;
  float c = 27.01f;
};

void Properties::getValues()
{
  std::cout << a << std::endl;
}

int main()
{
  setlocale(0, "");
  
  Properties obj;
  obj.getValues();
  
  std::cin.get();
  
  return 0;
}

D
Denis Zagaevsky, 2021-01-24
@zagayevskiy

I think you have an error in the method signature. You do not need to pass a pointer there, you need to take data from this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question