|
Having this;QList<QBluetoothHostInfo> information_main; inside the method creates a new Qlist type variable called 'information_main', this variable is totally different than the class variable called 'information_main' that has been initialized in a class constructor. This new QList variable (the one just declared inside the method) is empty because you just created it and have not added anything to it. When you make an indexed access to an empty QList variable it will fail. That's why you have to initialize (add stuff to) the new QList variable (the one just declared inside the method) to get the loop to work.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Yes, as I mentioned above the one in the innermost scope is the one that will be used; that is the one declared inside the method. But you really should not create variables with the same name like this, it just confuses people, particularly you. See my last sentence in the previous reply for a possible fix to the out of range error. But you should really capture the count first in a variable and test it to see if it returns a ositive value.
|
|
|
|
|
I am not sure who is confused. Just read the original post and subsequent comments.
So far it looks as the template code fails if there are NO items in it. That makes sense, but it is not the primary issue - just nice to know.
However, the list is not empty and I still do not have an answer to the original post.
Maybe I should rephrase the question.
The template list content was created by other process, what I want is to read it - I do not want to rebuild / reinitialize it.
I can read it fine first time in constructor, now I like to read it again in method.
I do not want to reinitialize it.
And I am doing it wrong , and the matching names is NOT the issue.
I do not want to reinitialize it.
|
|
|
|
|
Member 14980433 wrote: I am not sure who is confused. I am, because there is still some information missing. And I cannot understand why you need to read this information inside the constructor (which is definitely wrong), as well as in the method (which is correct).
But to go back to the main point, the only way to resolve the index out of range problem is to use the debugger and step through the code as it runs.
|
|
|
|
|
Is there a chance that the following call QBluetoothLocalDevice::allDevices(); may return no devices? If so, your do-while loop will have an out of range issue.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Thanks for the reply.
Good point, but does not address the question.
The do-while-loop compares the index to count of the items being processed.
No count , no processing,
Yes, I could check the count before entering the loop.
I am sure I will find more "elegant" way to process the items and stop the loop.
|
|
|
|
|
Member 14980433 wrote: No count , no processing, No, you're going to process it once before size is checked, and likely crash if the list is empty.
Member 14980433 wrote: I am sure I will find more "elegant" way to process the items and stop the loop. Indeed, most likely a for loop.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
No, you're going to process it once before size is checked, and likely crash if the list is empty.
My objective is to identify the reason for the "out of range "error.
I am getting same when accessing another template list.
Ok, let's make an assumption the do-while " list " is indeed empty. ( It is not )
Would the compiler produce the "index out of range" error then ?
|
|
|
|
|
It is not the compiler that produces the error, it is your code when it runs. You really need to make use of the debugger to find out exactly where the error occurs and what variable values are the cause.
|
|
|
|
|
Member 14980433 wrote: Ok, let's make an assumption the do-while " list " is indeed empty. ( It is not )
Why are you sure it is non-empty?
How did did you check it? Using debugger? Using TRACE with the item count? Using something else?
|
|
|
|
|
This thread is not very productive.
Please do not waste your time "taking side trips" , perhaps re-read the original EDITED post
to concentrate on the real issue.
I am using the templates wrong and have no idea why.
Thanks for understanding.
|
|
|
|
|
Firstly this is not a template, it is (one assumes, since we cannot see you code) a class. And a number of us have explained at length what is wrong and how to fix it. If you think that is taking side trips ...
|
|
|
|
|
Member 14980433 wrote: Template "list" = QBluetoothLocalDevice::allDevices();
It would be nice if contributions lead to resolve this SPECIFIC problem.
Naming variables, verifying validity of "loop index / count" , pointing out the code is
repeated in class method so far does not addresses my error usage of template.
Did you use the Debugger to check what this
QBluetoothLocalDevice::allDevices(); returns?
|
|
|
|
|
Why complicate things??
Is it not sufficient that I get run time error?
Running debug is still not addressing the issue.
Logically - if the "list count " is > 0 the code in constructor works.
I did verify the count > 0 before do-while loop , but that is superficial.
If same code is used in method it appears that the count changed - the run time error said so.
Hence using debugger will just confirm that - so what is the point? ( another useless side trip, in my opinion, proving nothing ).
I will repeat - did I used the template QList wrong is my concern? How did the contents of the list changed AKA index is no longer valid ?
I will delete the code in constructor and run only the method code. That is my next best guess, but it will NOT answer the problem.
|
|
|
|
|
Member 14980433 wrote: Running debug is still not addressing the issue. Of course it is. That is what the debugger provides: information about why your program crashes, raises an exception, produces the wrong output etc. But if you want to waste your time complaining because we cannot magically produce the answer that you think you deserve, despite having only half the picture, that is up to you.
|
|
|
|
|
It absolutely is the issue.
The debugger is there to debug YOU, not the code. It's there to show you what the code is really doing compared to what you THINK it does.
|
|
|
|
|
Member 14980433 wrote: If same code is used in method it appears that the count changed - the run time error said so.
Hence using debugger will just confirm that - so what is the point? ( another useless side trip, in my opinion, proving nothing ).
I will repeat - did I used the template QList wrong is my concern? How did the contents of the list changed AKA index is no longer valid ?
Just debug your code step-by-step, look in the debug/watch window, compare the variables values with the ones you expected to see,... And you will find the problem!
|
|
|
|
|
Could you post your class definition and the whole of your method?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Not sure if I want to do so.
So far the discussion is centered on "why are you doing this ?" instead of on resolution.
My code is written for me and I have had bad experience of "gurus" criticizing my style.
Do not want that again.
Let me try my last idea - running just the method and I'll try to sanitize / remove my comments from he code.
Fair?
|
|
|
|
|
Don't know what to tell you, showing code is far better than trying to describe it. No one has criticized your coding 'style', they've mentioned things that are logical improvements, not code style improvements. Make the smallest runnable example that exhibits the problem, then show the whole example.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Here is a working , sanitized / abbreviated TEST code used in method
information_main.count() = 2
{
QList<QBluetoothHostInfo> information_main = QBluetoothLocalDevice::allDevices();
int index = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() );
reset index and run it again - works as expected
fails run time if index is not reset - as expected
index = 0;
do
{
information_main.at(index).address().toString();
index++;
} while( index != information_main.count() );
|
|
|
|
|
What is this? Can't you post the whole class definition, initialization, and the method you mentioned?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
What does this have to do with the problem? You were asked to create a sample that demonstrates the problem you are having. This merely demonstrates that correct management of the index variable makes the code work.
|
|
|
|
|
Hi . Recently i've got to make a game called gomoku between two players . Now i need to edit it and change it from PVP to versus computer . I understand that in order of getting a random number from computer i can use <time.h> and rand() , and the letter by something like this :
char letters[] = "abcdefghijklmnopqrstuvwxyz";
char x = letters[rand() % 26];
Now my question is how i can add it in my program , i tried to put them in but it still is not generating a move from computer . Here's my code,maybe u have some ideas
#include <iostream>
#include <iomanip>
using namespace std;
void print_table(int x[][15]) {
system("cls");
for (int i = 0; i < 15; i++) {//the loop that use to print out the english character row
if (i == 0)
cout << setw(4) << "A";
else if (i == 1)
cout << " B";
else if (i == 2)
cout << " C";
else if (i == 3)
cout << " D";
else if (i == 4)
cout << " E";
else if (i == 5)
cout << " F";
else if (i == 6)
cout << " G";
else if (i == 7)
cout << " H";
else if (i == 8)
cout << " I";
else if (i == 9)
cout << " J";
else if (i == 10)
cout << " K";
else if (i == 11)
cout << " L";
else if (i == 12)
cout << " M";
else if (i == 13)
cout << " N";
else if (i == 14)
cout << " O";
else if (i == 15)
cout << " P";
}
cout << endl;
for (int i = 0; i < 15; i++) {
cout << setw(2) << i;//print out the row number
for (int j = 0; j < 15; j++) {//print out the board game.
if (x[i][j] == 0) {//the inital value is 0, so when the block is 0 then print out the '.'
cout << " .";
}
else if (x[i][j] == 1) {//when the player O input the block then the value will adding one then if check the block is one then output the 'o'
cout << " O";
}
else if (x[i][j] == 2) {//when the player X input the block then the value will adding two then if check the block is two then output the 'x'
cout << " X";
}
}
cout << endl;
}
}
int check_player(int p) {
if (p == 1) {//change the player everytime before the next loop compile
p++;
}
else {
p--;
}
return p;
}
void input_value(int &t, int &n, int p, int x[][15]) {
char eng;
int number;
do {//the loop that ask for the user input the location.
cout << "player ";
if (p == 1) {
cout << "O";
}
else {
cout << "X";
}
cout << ", make a move: ";
cin >> eng;//input the location
cin >> number;
if (eng == 'A')//change the character to different number
t = 0;
else if (eng == 'B')
t = 1;
else if (eng == 'C')
t = 2;
else if (eng == 'D')
t = 3;
else if (eng == 'E')
t = 4;
else if (eng == 'F')
t = 5;
else if (eng == 'G')
t = 6;
else if (eng == 'H')
t = 7;
else if (eng == 'I')
t = 8;
else if (eng == 'J')
t = 9;
else if (eng == 'K')
t = 10;
else if (eng == 'L')
t = 11;
else if (eng == 'M')
t = 12;
else if (eng == 'N')
t = 13;
else if (eng == 'O')
t = 14;
if (!(eng >= 'A'&&eng <= 'M') || !(number >= 0 && number <= 14) || x[number][t] != 0) {//when the input wrong, output the statement to ask anouther input and loop again.
cout << "Invaid input, Try again!" << endl;
continue;
}
else {//if no problem then this input loop is break and jump to the next statement
break;
}
} while (1);//Because it will break as well so the do-while loop is no any requirement
n = number;
}
int main() {
const int num = 15;//the number for constant the array row and column value
char check_e;//for the user input the column
int R[num][num] = { 0 }, check_n, player = 1, buger = 0, transfer, playerO_win = 0, playerX_win = 0, draw = 0, check_draw;//the variable that for user input or checking the game statment
do {//launch the loop for the user input again and again
check_draw = 0;//reset the checking of draw
print_table(R);
input_value(transfer, check_n, player, R);
R[check_n][transfer] += player;//change the value according the player's input and the player name.
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (i <= 8 && R[j][i] != 0 && (R[j][i] == R[j][i + 1] && R[j][i] == R[j][i + +2] && R[j][i] == R[j][i + 3] && R[j][i] == R[j][i + 4])) {//the checking for the row bingo
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
else if (j <= 8 && R[j][i] != 0 && (R[j][i] == R[j + 1][i] && R[j][i] == R[j + 2][i] && R[j][i] == R[j + 3][i] && R[j][i] == R[j + 4][i])) {//the checking for the column bingo
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
else if (j <= 8 && i <= 8 && R[j][i] != 0 && (R[j][i] == R[j + 1][i + 1] && R[j][i] == R[j + 2][i + 2] && R[j][i] == R[j + 3][i + 3] && R[j][i] == R[j + 4][i + 4])) {//the checking for the \ situation.
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
else if ((j >= 4 || i >= 4 || i <= 8) && R[j][i] != 0 && (R[j][i] == R[j - 1][i + 1] && R[j][i] == R[j - 2][i + 2] && R[j][i] == R[j - 3][i + 3] && R[j][i] == R[j - 4][i + 4])) {//the checking for the / situation
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
for (int i = 0; i < num; i++) {//the loop for checking the draw
for (int j = 0; j < num; j++) {//this loop will check for every time compilation.
if (R[j][i] == 0)//when there are any empty block then the check_draw will adding, the draw situation is the check_draw be 0
check_draw++;
}
}
if (check_draw == 0) {//when the check_draw equal to 0 which mean the situation is no empty block
draw++;
break;
}
}
if (playerO_win != 0 || playerX_win != 0 || draw == 1)//break the second loop
break;
}
if (playerO_win == 1 && playerX_win == 0) {// when the player win print the block game again and print out the win statement
print_table(R);
cout << "player O wins!" << endl;
break;
}
else if (playerX_win == 1 && playerO_win == 0) {//the other player win the game
print_table(R);
cout << "player X wins!" << endl;
break;
}
else if (draw == 1) {//the draw block game print
print_table(R);
cout << "Draw game!" << endl;
break;
}
player = check_player(player);
} while (1);//in fact it is no need for the loop statement, because most of the situation will have a break statement for out of the loop
return 0;
}
modified 22-Nov-20 9:41am.
|
|
|
|
|
You need to learn how to use a debugger to find your problem. It will allow you to step through your code, one line at a time, to find out where it's doing something unexpected.
I will point out, however, that you can map a char to a range that starts at 0 like this:
t = eng - 'A';
'A' through 'Z' have contiguous values, so this maps eng to 0 to 25 , assuming that it was an uppercase letter. You can check for invalid input with
if((t < 0) || (t > 14))... // or t > ('O' - 'A')
Similarly, you can write
cout << ' ' << i + 'A';
|
|
|
|