Public Group

# Getting a variable inside a loop

This topic is 1016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know that once the loop is finished the variable is destroyed meaning you can't get the value once the loop has finished, however is there a way to do this? I want to keep the variable and change it.

	//                      x coord
x = character_location / r;
// y coord
y = character_location % c;
//this creates 1 character
clist[0] = new Character(x, y);

I want to be able to call x and y inside another function which will allow me to move the character say +- 1 units.

Any help would be appreciated . Edited by Mr N1nja

##### Share on other sites

I recognize some of that code from a previous question by Mr N1inja. It doesn't make sense to divide by r and take modulo c to compute the x and y coordinates. You should divide and take the modulo by the same number, which is something like the column length (the details depend on your exact conventions). I seem to remember r and c were uninitialized in your other code.

You don't want to access x and y somewhere else. You passed them to the constructor of Character, and now you should modify them there, preferably from a member function of the class Character.

##### Share on other sites

I recognize some of that code from a previous question by Mr N1inja. It doesn't make sense to divide by r and take modulo c to compute the x and y coordinates. You should divide and take the modulo by the same number, which is something like the column length (the details depend on your exact conventions). I seem to remember r and c were uninitialized in your other code.

You don't want to access x and y somewhere else. You passed them to the constructor of Character, and now you should modify them there, preferably from a member function of the class Character.

Yeah i have passed them to the character class, but i'm struggling on how i would call that character's x and y coords

EDIT: also r and c both = 10

EDIT: well no, what i mean is i have the x and y in character class, by doing this
Character::Character(int start_x, int start_y){
x = start_x;
y = start_y;
}

void Character::print()
{
cout << "\n" << "Character Position: " << "xPos " << x << "  yPos " << y << endl;
}
But the problem i'm having now is when i'm trying to call my moveBy() function which is also in the character class, it gives me this error

'Character' : no appropriate default constructor available.
Character character;
I've called other functions from classes before so i'm not sure why this is. Edited by Mr N1nja

##### Share on other sites

But the problem i'm having now is when i'm trying to call my moveBy() function which is also in the character class, it gives me this error

'Character' : no appropriate default constructor available.

Character character;
I've called other functions from classes before so i'm not sure why this is.

You are trying to create a new instance of Character. That is not possible because the only available constructor requires two parameters (for initializing the starting position).

However, the core problem appears to be that you are unintentionally creating a new instance when that is not your intention and not useful to your cause. You really need to post more code for anyone to really help you though.

##### Share on other sites

But the problem i'm having now is when i'm trying to call my moveBy() function which is also in the character class, it gives me this error

'Character' : no appropriate default constructor available.

Character character;
I've called other functions from classes before so i'm not sure why this is.

You are trying to create a new instance of Character. That is not possible because the only available constructor requires two parameters (for initializing the starting position).

However, the core problem appears to be that you are unintentionally creating a new instance when that is not your intention and not useful to your cause. You really need to post more code for anyone to really help you though.

Didn't even realize that sorry! Yeah i've changed it no errors at the moment its just not calling the function, I'll try to figure it out first before asking for help.

##### Share on other sites
I'm trying to call my moveBy function from my character cpp file.

This is my .h

class Character {
protected:
int x, y; // co-ordinate positions

public:
Character(int start_x, int start_y);

virtual void print();

void moveBy();
};
my c++ file function

void Character::moveBy()
{
cout << "called function" << endl;
}
my main c++ file trying to call the function in the main
int _tmain(int argc, _TCHAR* argv[]){

Character moveBy();
exit();
return 0;
}
That receives no errors however it doesn't call my moveBy function, so just wondering how i would call this Edited by Mr N1nja

##### Share on other sites
It does not call your function because you never try to call it.

When you say
Character moveBy();
that means you declare a function local to _tmain called moveBy which returns a Character. That, however, is mostly an artifact rooted in C++'s ancient history (see also Most vexing parse). Had you said
Character moveBy(2, 3);
that would have caused you to create a new instance of character called moveBy. None of these make any attempt to call Character::moveBy. What you want for a first test is:
Character character(2, 3);
character.moveBy();

##### Share on other sites

It does not call your function because you never try to call it.

When you say

Character moveBy();
that means you declare a function local to _tmain called moveBy which returns a Character. That, however, is mostly an artifact rooted in C++'s ancient history (see also Most vexing parse). Had you said
Character moveBy(2, 3);
that would have caused you to create a new instance of character called moveBy. None of these make any attempt to call Character::moveBy. What you want for a first test is:
Character character(2, 3);
character.moveBy();

ah i get you now, so if i had my character stored away in an array with already stated x and y coords,
array[i]->moveby();
is above what i would be after in order to move the values already given? Edited by Mr N1nja

##### Share on other sites

You seem slightly confused on how objects work. You need to create an instance of the Character class(Declared in your header) before you call the function moveby().

Character myCharacter;//Create the myCharacter object of type Character
myCharacter.moveby(1,1) //Call the moveby() function and pass in 1,1 representing your desired x and y


Also I would suggest you try not to start so ambitiously. A basic level of understanding of simple aspects of the c++ language will help you a lot more by starting with a good book or a follow along tutorial online.

Edited by ExErvus

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
14
5. 5

• 12
• 9
• 17
• 16
• 9
• ### Forum Statistics

• Total Topics
632596
• Total Posts
3007323

×