CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Feb 2013
    Posts
    30

    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;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Error: Identifer "result" is undefined

    Giving your variable the same name as the class isn't a really good idea.

  3. #3
    Join Date
    Feb 2013
    Posts
    30

    Re: Error: Identifer "result" is undefined

    okay. but that would have nothing to do with the errors..

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Error: Identifer "result" is undefined

    Quote Originally Posted by tmcfadden View Post
    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];

  5. #5
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Error: Identifer "result" is undefined

    Quote Originally Posted by GCDEF View Post
    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
    Code:
    	MSTbl MSTbl;
    Victor Nijegorodov

  6. #6
    Join Date
    Feb 2013
    Posts
    30

    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.
    HTML Code:
    MSTbl 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.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error: Identifer "result" is undefined

    Quote Originally Posted by tmcfadden View Post
    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.
    HTML Code:
    MSTbl 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

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error: Identifer "result" is undefined

    Quote Originally Posted by tmcfadden View Post
    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.
    HTML Code:
    MSTbl 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

  9. #9
    Join Date
    Feb 2013
    Posts
    30

    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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Error: Identifer "result" is undefined

    Quote Originally Posted by tmcfadden View Post
    II'd like to know if the code that I changed looks correct
    We won't know till you post it.

  11. #11
    Join Date
    Feb 2013
    Posts
    30

    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];
    		}
    }

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error: Identifer "result" is undefined

    Quote Originally Posted by tmcfadden View Post
    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

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error: Identifer "result" is undefined

    Quote Originally Posted by tmcfadden View Post
    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
    Last edited by Paul McKenzie; March 6th, 2013 at 10:25 PM.

  15. #15
    Join Date
    Feb 2013
    Posts
    30

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured