Error: Identifer "result" is undefined
I have some code does not compile. I think it's missing an included library, but not sure.
In the int main() block of code, the following three items give errors:
1. Mtrx (the one following "new") - Error: expected a type specifier
2. result - Error: expected a ";"
3. &result - identifier "result" is undefined
Below is the code with the head to show you what has been included:
HTML Code:
#include <iostream>
#include <iomanip>
using namespace std;
#include <limits.h>
// create the structure of the matrix
struct Mtrx
{
int numRows;
int numCols;
float array[101][101];
};
// create the tables of the matrix
struct MSTbl
{
int mTable[100][100];
int sTable[100][100];
};
void Input(int &, Mtrx *);
void Output(int, Mtrx &);
void DetEff(int, Mtrx *, MSTbl &);
void RMM(int, int, Mtrx *, Mtrx *, int, MSTbl);
void MM(Mtrx &, Mtrx &, Mtrx &);
int main()
{
int numsMtrx;
Mtrx *Mtrx = new Mtrx[500];
MSTbl MSTbl;
Mtrx result;
Input(numsMtrx, Mtrx);
DetEff(numsMtrx, Mtrx, MSTbl);
RMM(1, numsMtrx, &result, Mtrx, MSTbl.sTable[1][numsMtrx], MSTbl);
Output(MSTbl.mTable[1][numsMtrx], result);
return 0;
}
Re: Error: Identifer "result" is undefined
Giving your variable the same name as the class isn't a really good idea.
Re: Error: Identifer "result" is undefined
okay. but that would have nothing to do with the errors..
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
tmcfadden
okay. but that would have nothing to do with the errors..
Try changing the name of your pointer variable here to something else and get back to me.
Mtrx *Mtrx = new Mtrx[500];
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
GCDEF
Try changing the name of your pointer variable here to something else and get back to me.
Mtrx *Mtrx = new Mtrx[500];
... and please, the same with this line:
Quote:
Originally Posted by PMtmcfadden
Re: Error: Identifer "result" is undefined
Okay.
I understand what was done.
Both the variable and the pointer variable are the same so I renamed them:
HTML Code:
Mtrx *mtrx_ptr
I also renamed the MSTbl to table.
After making the change, I have to go further down in the code and change the other areas where the variable was called. Upon saving the whole thing, it compiles. Now, when I step through the program it hangs up on this block of code
HTML Code:
Input(numsMtrx, mtrx_ptr);
DetEff(numsMtrx, mtrx_ptr, table);
RMM(1, numsMtrx, &result, mtrx_ptr, table.sTable[1][numsMtrx], table);
Output(table.mTable[1][numsMtrx], result);
I think it's some form of matrix program that adds does a math procedure on numbers in rows and columns.
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
tmcfadden
Okay.
I understand what was done.
Both the variable and the pointer variable are the same so I renamed them:
HTML Code:
Mtrx *mtrx_ptr
I also renamed the MSTbl to table.
After making the change, I have to go further down in the code and change the other areas where the variable was called. Upon saving the whole thing, it compiles. Now, when I step through the program it hangs up on this block of code
HTML Code:
Input(numsMtrx, mtrx_ptr);
DetEff(numsMtrx, mtrx_ptr, table);
RMM(1, numsMtrx, &result, mtrx_ptr, table.sTable[1][numsMtrx], table);
Output(table.mTable[1][numsMtrx], result);
I think it's some form of matrix program that adds does a math procedure on numbers in rows and columns.
So what do you want us to do?
If you don't know C++, but just got code from somewhere and got it to compile, then I always ask "ok, so if something goes wrong when you run the program, who is going to debug the program?".
Can you debug the code? That is exactly what you're going to need to do to go further. If you don't know C++, well that's the pickle you get yourself into when by chance, some semi-complex C++ code just happens to compile and link, but doesn't run.
Regards,
Paul McKenzie
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
tmcfadden
Okay.
I understand what was done.
Both the variable and the pointer variable are the same so I renamed them:
HTML Code:
Mtrx *mtrx_ptr
I also renamed the MSTbl to table.
After making the change, I have to go further down in the code and change the other areas where the variable was called. Upon saving the whole thing, it compiles. Now, when I step through the program it hangs up on this block of code
HTML Code:
Input(numsMtrx, mtrx_ptr);
DetEff(numsMtrx, mtrx_ptr, table);
RMM(1, numsMtrx, &result, mtrx_ptr, table.sTable[1][numsMtrx], table);
Output(table.mTable[1][numsMtrx], result);
I think it's some form of matrix program that adds does a math procedure on numbers in rows and columns.
So what do you want us to do?
If you don't know C++, but just got code from somewhere and got it to compile, then I always ask "ok, so if something goes wrong when you run the program, who is going to debug the program?".
Can you debug the code? That is exactly what you're going to need to do to go further.
Regards,
Paul McKenzie
Re: Error: Identifer "result" is undefined
I am a student of C++ currently enrolled in a Sockets Programming class, so I have only an entry level knowledge of C++. I'm trying to decipher what my teacher has proposed.
He gave us the code, which purposely has no comments included, but is supposed to be a fully functioning program. However, it crashed the first time I ran it.
.
I "believe" from my novice study of it that he's left something very simple out, such as a semi-colon. Or, it's possible that he purposely gave the same name a set of classes and a variables. I've changed the variable names and now everything seems to compile okay. However, the program doesn't yield anything and I think it's because I haven't changed all of the callouts to the new variable name. I'd like to know if the code that I changed looks correct
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
tmcfadden
II'd like to know if the code that I changed looks correct
We won't know till you post it.
Re: Error: Identifer "result" is undefined
Here you go.
What I did was changed the following two variables:
Mtrx *mtrx_ptr = new Mtrx[500]; //the pointer variable in red was formerly *Mtrx
MSTbl table; //the variable in red was formerly MSTbl
Entire program - Sorry to include for the big post, but you have to see the where the new variable names are called out to know if I missed any.
------------------------------------------------------
HTML Code:
#include <iostream>
#include <iomanip>
using namespace std;
#include <limits.h>
struct Mtrx
{
int numRows;
int numCols;
float array[101][101];
};
struct MSTbl
{
int mTable[100][100];
int sTable[100][100];
};
void Input(int &, Mtrx *);
void Output(int, Mtrx &);
void DetEff(int, Mtrx *, MSTbl &);
void RMM(int, int, Mtrx *, Mtrx *, int, MSTbl);
void MM(Mtrx &, Mtrx &, Mtrx &);
int main()
{
int numsMtrx;
Mtrx *mtrx_ptr = new Mtrx[500];
MSTbl table;
Mtrx result;
Input(numsMtrx, mtrx_ptr);
DetEff(numsMtrx, mtrx_ptr, table);
RMM(1, numsMtrx, &result, mtrx_ptr, table.sTable[1][numsMtrx], table);
Output(table.mTable[1][numsMtrx], result);
return 0;
}
void Input(int &numsMtrx, Mtrx *Mtrx)
{
int i, j, k;
cin >> numsMtrx;
for(i = 1; i <= numsMtrx; i++)
{
cin >> Mtrx[i].numRows;
cin >> Mtrx[i].numCols;
for(j = 1; j <= Mtrx[i].numRows; j++)
for(k = 1; k <= Mtrx[i].numCols; k++)
cin >> Mtrx[i].array[j][k];
}
}
void Output(int numOfMults, Mtrx &result)
{
int rows, cols;
cout << numOfMults << endl;
cout << result.numRows << " " << result.numCols << endl;
for (rows = 1; rows <= result.numRows; rows++){
for (cols = 1; cols <= result.numCols; cols++)
cout << setw(7) << result.array[rows][cols] << " ";
cout << endl;
}
}
void DetEff(int numsMtrx, Mtrx *mtrx_ptr, MSTbl &MSTbl)
{
int n, i, j, k, l, q;
n = numsMtrx;
for (i = 1; i <= n; i++){
MSTbl.mTable[i][i] = 0;
}
for (l = 2; l <= n; l++){
for(i = 1; i <= n - l + 1; i++){
j = i + l - 1;
MSTbl.mTable[i][j] = INT_MAX;
for(k = i; k <= j - 1; k++){
q = MSTbl.mTable[i][k] + MSTbl.mTable[k + 1][j] + mtrx_ptr[i].numRows * mtrx_ptr[k].numCols * mtrx_ptr[j].numCols;
if(q < MSTbl.mTable[i][j]){
MSTbl.mTable[i][j] = q;
MSTbl.sTable[i][j] = k;
}
}
}
}
}
void RMM(int i, int j, Mtrx *res, Mtrx *m,
int s, MSTbl MSTbl)
{
Mtrx a, b;
if (i == j){
*res = m[i];
return;
}
if ((i + 1) == j){
MM(m[i], m[j], *res);
return;
}
RMM(i, s, &a, m, MSTbl.sTable[i][s], MSTbl);
RMM(s + 1, j, &b, m, MSTbl.sTable[s + 1][j], MSTbl);
MM(a, b, *res);
}
void MM(Mtrx &a, Mtrx &b, Mtrx &result)
{
int i, j, k;
result.numRows = a.numRows;
result.numCols = b.numCols;
for (i = 1; i <= a.numRows; i++)
for (j = 1; j <= b.numCols; j++){
result.array[i][j] = 0;
for (k = 1; k <= a.numCols; k++)
result.array[i][j] += a.array[i][k] * b.array[k][j];
}
}
Re: Error: Identifer "result" is undefined
Not really sure what it's supposed to do, but keep in mind, arrays are 0 based in C and C++, so this line and others like it
for(i = 1; i <= numsMtrx; i++)
skip the first element and goes one past where you want them to.
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
tmcfadden
Code:
for(i = 1; i <= numsMtrx; i++)
{
cin >> Mtrx[i].numRows;
cin >> Mtrx[i].numCols;
for(j = 1; j <= Mtrx[i].numRows; j++)
for(k = 1; k <= Mtrx[i].numCols; k++)
cin >> Mtrx[i].array[j][k];
}
}
As GCDEF pointed out, you are using 1-based coding in a 0-based C++ world. Doing so opens you up for buffer overruns and memory corruption.
In C++, array indices start at 0 and go up to n-1, where n is the total number of elements. Trying to fake out 1-based arrays in C++ using this type of coding is almost always faulty. There is a large probability that somewhere, something is "off-by-1", whether it is an array index, a for loop count, adding too many items to a sum, something. That error either leads to faulty values being used and/or programs behaving badly.
Either change the code to do true 0-based processing, or create a class that wraps up the 1-based processing so that all of that 1-based access becomes transparent (thereby safe).
Regards,
Paul McKenzie
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
tmcfadden
I'd like to know if the code that I changed looks correct
That's not the way you diagnose a programming problem.
It is one thing to get code to compile, and another thing to get the code to run properly. Just because the code compiles doesn't mean it will run correctly. Unless the program is a toy program, then anyone, including all that are helping you here, would run the code under the debugger to see what is wrong. No one, unless they have a strain of savant syndrome, can "run the computer in their head", keeping track of variables, program flow, etc..
So are you prepared to learn how to debug a program? Now is the time to learn, as you can't write any program, let alone a socket program, if you don't know how to debug a C++ program.
Regards,
Paul McKenzie
Re: Error: Identifer "result" is undefined
Here's what this program does.
When I run it it hits the INPUT function and waits for input. Even though the program does not prompt the user to enter something, I tested it by entering integers.
First I entered 1, which allowed me to enter it four times before performing a calculation and returning some obscure values.
I then entered all 2's, which allowed me to enter 13 times before performing a calculation and returning some obscure values.
Finally, I entered 3's which allowed me to enter 34 times before performing a calculation and returning some obscure values.
What I can tell is that it creates and array and does "something" with the numbers, after which is places them in a grid.
But, I can't tell for the life of me what. There's no rhyme or reason to the output.
Re: Error: Identifer "result" is undefined
Quote:
Originally Posted by
tmcfadden
Here's what this program does.
When I run it it hits the INPUT function and waits for input. Even though the program does not prompt the user to enter something, I tested it by entering integers.
First I entered 1, which allowed me to enter it four times before performing a calculation and returning some obscure values.
I then entered all 2's, which allowed me to enter 13 times before performing a calculation and returning some obscure values.
Finally, I entered 3's which allowed me to enter 34 times before performing a calculation and returning some obscure values.
What I can tell is that it creates and array and does "something" with the numbers, after which is places them in a grid.
But, I can't tell for the life of me what. There's no rhyme or reason to the output.
Time to learn to use your debugger. It will show you exactly what your program is doing and why. That's what any of us would do and it's absolutely indispensable. You cannot write working programs without it. It's not optional.
Re: Error: Identifer "result" is undefined
There's always 'rhyme and reason' to what a program does based upon the code it executes (unless you have a faulty CPU or compiler, which has a probability so close to 0 it can be discounted). The computer, when executing the program, does exactly what it has been told to do - no more and no less. If the program produces output which is at variance with what is expected then either the expectation is wrong or the program is at fault. In your case, you seem to have no knowledge of what the program is supposed to do! So your first step is to look at the program code and walk through it with the debugger as needed and deduce what is its function. You are correct that it performs operations on matrices - but what's that got to do with network socket programming?? If you look at the Input function, you can see that the first number requested is for the number of arrays to be used. Then for each array, it requests the number of rows and the number of columns and then inputs a value for each for these rows and columns. Then repeats for the next array etc.
This should help to make it more clear for you as to the input it requires.
Code:
void Input(int &numsMtrx, Mtrx *Mtrx)
{
int i,
j,
k;
cout << "How many matrices :";
cin >> numsMtrx;
cin.ignore(1000, '\n');
for(i = 0; i < numsMtrx; i++) {
cout << "How many rows for matrix " << i << ": ";
cin >> Mtrx[i].numRows;
cin.ignore(1000, '\n');
cout << "How many columns for matrix " << i << ": ";
cin >> Mtrx[i].numCols;
cin.ignore(1000, '\n');
for(j = 0; j < Mtrx[i].numRows; j++) {
for(k = 0; k < Mtrx[i].numCols; k++) {
cout << "Value for matrix " << i << " column " << k << " row " << j << ": ";
cin >> Mtrx[i].array[j][k];
cin.ignore(1000, '\n');
}
}
}
}
Now you just have to determine what the other functions do (by inspecting the code, walking through it and using the debugger) and then you'll know what the program is supposed to do and what the output means! None of us knows by simply looking at the code what it is supposed to do. We would have to do the same as you and walk through it and use the debugger. A knowledge of matrix mathematics would also be useful!