C++/hello, about passing functions again..
Expert: Ralph McArdell - 2/18/2005
QuestionHello, first of all i would like to start off with a thank you note for the previous question answered.
And I have applied the code
(I have slightly modified)
#include <iostream.h>
int function1( string, int);
int function2( string, int);
int function1( string str[256][2], int index )
{
// Note quoted string literals
str[index][0] = "ABC";
str[index][1] = "abc";
++index;
str[index][0] = "DEF";
str[index][1] = "def";
return ++index;
}
// Note revised declaration and definition
// Note correct dimensions and std qualification on C++ name string
int function2( string str[256][2], int index )
{
// Note quoted string literals
str[index][0] = "GHI";
str[index][1] = "ghi";
++index;
str[index][0] = "JKL";
str[index][1] = "jkl";
return ++index;
}
int main()
{
// Note possibly more meaningful name and correct dimensions
// Note std namespace qualification of C++ library name string
string str[256][2];
int end=0;
// Initialise end index value from return value of function2:
// Remember function1 and function2 now return the next available
// index which is one past end of the number of used indices.
// Again we pass the return value from function1 straight into
// function2 as its starting index, and pass 0 as the starting index
// for function 1.
//int end( function2( str , function1(str, 0) ) );
//int end( function2( str , function1(str, 0) ) );
function1(str, end);
function2(str, end);
// Loop from 0 until index reaches (one past the) end. Note
// preference for pre-increment when either ++index or index++
// can be used ? this not important here but can have performance
// ramifications in other code, so is a good habit to adopt.
// Note use of != instead of <. Again not important here but is
// important with C++ library container iterators so is also a good
// habit to adopt.
for ( int index(0); index != end; ++index )
{
// Note std namespace qualification of C++ library name cout
// Note outputting more than one value to cout in one statement
cout << str[index][0] << str[index][1];
}
cout << '\n';
cout << str[1][0];
return 0;
}//End of code
to the original program that I am programming.
The above program compiles fine.
(I remember asking you about how to write interpreters. I haven't exactly followed the way you told me to do so, but I am on my way to making one, actually your way was too sophisticated to my limited experience in C++)
But I keep getting this error:
test.cpp 36: Could not find a match for 'system(string,string,string ( *)[2], int)' in function main()
whenver I apply it to my interpreter..
This is a glimpse of what I have programmed
#include <fstream.h>
#include <iostream.h>
using namespace std;
enum Function {Failure, Success};
//name prototypes
int shenu(string, string, string, int);
..
..
..
int main()
{
//set for System counts
string String_System[256][2];
int Count_System=0;
//cout << "Content-type: text/html\n\n";
..
..
..
system(file_Name,file_Name, String_System, Count_System);
..
..
} //End of Main
int system(string sub_Name, string file_Name, string String_System[256][2], int Count_System){
..
..
..
}
I would thank you if you can help me out one more time, btw, my name is James. Nice to meet you.
AnswerFOLLOWUP
---------------
I read your rating comments. Please note that I am not angry that you changed the code I sent you. I was just pointing out that I thought your changes would not produce the expected results as posted originally and why I thought that!
Another option by the way would be to pass the index by reference:
void function1( string str[256][2], int & index );
Then the modifications to index in function1 modify what ever int object was passed to function1 - end in this case.
-------------------------------------------------------------------------------------------------
Hello again James,
First I would check that your modifications to the previous program actually produce the results you wish.
If you have a function such as function1 declared as so:
int function1( string str[256][2], int index )
The value of the index parameter is passed by _value_ which as I pointed out last time is no good if you wish to pass the value out. You then modified my example like so:
function1(str, end);
function2(str, end);
So what is the value of end when passed to function2? Remember that pass by values copies the value passed in to function1 and, as I just repeated, does not pass it out. This is why I returned the final next index value as the return values for function1 and function2.
As that is not the issue here I shall let you think on that one yourself.
So, what is wrong with your system call? Well you might have noticed in my previous reply that I mentioned the compiler only knows what it has seen during the processing of your source file. You call system in main but at this point the compiler has not seen either a declaration (a.k.a. prototype) for such a function nor its definition (which also serves as a declaration). Hence it is confused and issues an error. Either move the definition of system to a position before main or place a function prototype for it before main:
int system(string sub_Name, string file_Name, string
String_System[256][2], int Count_System);
// ...
int main()
{
// ...
}
I see you have a function prototype for a similar function named shenu, so you are not totally unaware of such issues.
Oh, there is one other slight problem with a function named system – the standard C library (and therefore also the standard C++ library) already has a function named system – albeit with a different set of parameters. This is not a problem for the compiler as C++ allows you overload function names, and you probably have not included a header file that declared the library system in the global namespace anyway. However it may cause confusion to those reading your code – I know my first thoughts were around a misuse of the library function system until I had finished reading your question.