7 Replies - 734 Views - Last Post: 07 April 2016 - 03:53 AM Rate Topic: -----

#1 Morgan Le Fay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-April 16

Values not printing to console when using a member function

Posted 05 April 2016 - 03:21 PM

Hi,

when I use the following in the main, the values of the array prints to the console:
int main()
{
	SmallArr sArr[4];
	int i;
	for (i=0; i<4;i++)
		sArr[i].setCellValues(i);
	for (i=0; i<4;i++)
		cout << sArr[i].get()<<endl;
}




however, when using a member function to output the values of an array, nothing prints to the console:

#include <iostream>

using namespace std;

class SmallArr
{
	int m_CellNumber;		//unique identifier 
public:
	
	SmallArr() //constructor
	{
		SetToZero();
	}

	void SetToZero()		//a function to set member variables to 0
	{
		m_CellNumber = 0;
	}

	//Member function declaration
	int get()
	{
		return m_CellNumber;
	}

	//set
	void setMembers (int);
	void setCellValues (int cellNumber)
	{
		m_CellNumber=cellNumber;
	}

private:

};

//function to set values of array
void SmallArr::setMembers (int cellNumber)		
	{
		SmallArr sArr[4];
		int i;
		for (i=0; i<4;i++)
			sArr[i].setCellValues(i);
		for (i=0; i<4;i++)
			cout << sArr[i].get()<<endl;
}

int main()
{
	void setMembers();
	cin.get();
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Values not printing to console when using a member function

#2 jimblumberg   User is offline

  • member icon

Reputation: 5780
  • View blog
  • Posts: 17,684
  • Joined: 25-December 09

Re: Values not printing to console when using a member function

Posted 05 April 2016 - 03:26 PM

In your second snippet where are you even using your class?

Line 50 is a function prototype, not a function call. And by the way where is the setMembers() function implemented? Hint, it's not starting on line 38, that is the class function.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Values not printing to console when using a member function

Posted 05 April 2016 - 04:12 PM

In case you're still confused about this:

What you have on lines 38-46 is the implementation of this setMembers function
void SmallArr::setMembers (int cellNumber)

which you declared on line 27 in the class definition.

What you have on line 50
void setMembers();

is a function prototype, in other words a declaration (without any implementation) of a local function inside your main function. It has no relationship to the setMembers function you defined as part of the SmallArr class. (And local functions are illegal anyway.)

To use the function you defined in SmallArr, you first have to declare a SmallArr object, and then use it to call the function:

SmallArr myArray;
myArray.setMembers(1234);



But first you should take a closer look at your function implementation, because right now it doesn't do anything with the parameter that is sent to it.

This post has been edited by kaa: 05 April 2016 - 04:12 PM

Was This Post Helpful? 1
  • +
  • -

#4 Morgan Le Fay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-April 16

Re: Values not printing to console when using a member function

Posted 06 April 2016 - 06:43 AM

View Postkaa, on 05 April 2016 - 11:12 PM, said:

To use the function you defined in SmallArr, you first have to declare a SmallArr object, and then use it to call the function:

SmallArr myArray;
myArray.setMembers(1234);



Thanks for the reply and explanation. I declare a SmallArr object in line 40; should I declare another object of SmallArr in the main? E.g.
int main()
{
	SmallArr myArr;
	myArr.setMembers(1);
	cin.get();
	return 0;
}



Also, which function were you referring here:

Quote

But first you should take a closer look at your function implementation, because right now it doesn't do anything with the parameter that is sent to it.

Was This Post Helpful? 0
  • +
  • -

#5 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Values not printing to console when using a member function

Posted 06 April 2016 - 08:14 AM

View PostMorgan Le Fay, on 06 April 2016 - 09:43 AM, said:

I declare a SmallArr object in line 40

That part really doesn't make sense. If I understande your assignment correctly your SmallArr class should have a private data member that is an array of int. And then all of the class functions should be operating on that array. So setMembers shouldn't be declaring a SmallArr object. setMembers is already "inside" a SmallArr object, and setMembers should be placing elements in its private array of int.
Was This Post Helpful? 0
  • +
  • -

#6 Morgan Le Fay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-April 16

Re: Values not printing to console when using a member function

Posted 06 April 2016 - 09:57 AM

View Postkaa, on 06 April 2016 - 03:14 PM, said:

That part really doesn't make sense.


I am trying to create a class SmallArr with a member variable m_CellNumber. Then, using an array of size 4, assign a unique, sequential cell number (m_CellNumber?) to each element of the array (please see attached image). Currently, I can only get this to work when I have the following in the main (as opposed to a separate function):
int main()
{
	SmallArr sArr[4];
	int i;
	for (i=0; i<4;i++)
		sArr[i].setCellValues(i);
	for (i=0; i<4;i++)
		cout << sArr[i].get()<<endl;
	cin.get();
	return 0;
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#7 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Values not printing to console when using a member function

Posted 06 April 2016 - 12:55 PM

The picture doesn't help. It doesn't make sense to have a class whose only data member is one int variable, and then put functions inside the class object to manipulate arrays of this class object. The array of int should be inside the class (as its data member). Even your class name says that this is the way it should be. Why is it called SmallArr -- that name implies that a class object is some kind of array.

The alternative is to put those functions outside the class, except then there would be no reason for the class to exist at all.
Was This Post Helpful? 1
  • +
  • -

#8 Morgan Le Fay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-April 16

Re: Values not printing to console when using a member function

Posted 07 April 2016 - 03:53 AM

Ok, thanks Kaa and jimblumberg for the advice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1