I am probably making a trivial mistake that I cannot see, but the g++ compiler is ignoring some function declarations in the .h file, and whether these function declarations are present or not, the executable program is created without any reported errors, and its output seems to be correct. But if I remove some other functions from the .h file, then there is an error reported by the g++ compiler.
I can imagine two possible reasons for this situation: One possibility is that perhaps the fact the std::map that I am using to create a map whose elements are in fact functions, is causing some hidden problems. Or is it possible that I have altered the settings of my GVIM editor (Ubuntu Linux) in such a way that some invisible characters were introduced?
Anyway, here is the entire code that is working.
1) First of all, here is the text file "FileForNamesOfFunctions.TXT" that the main program reads the names of the functions that will be executed:
function2
function3
function5
function6
2) Here is the .h file "h_file.h "of the main program:
Code:
// If I remove any of the following three function declarations
// below, the g++ compiler reports an error:
void TestChosenFunctions();
void SetUpMapOfFunctionsToActualFunctions();
void GetControlFunctionNamesFromDiskFile();
// But it's OK to remove the following function declarations below
//void function0(double);
//void function1(double);
//void function2(double);
//void function3(double);
//void function4(double);
//void function5(double);
//void function6(double);
3) Here is the main program file "MapsAndFunctions.cpp" that contains everything except the function declarations:
Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include "h_file.h"
using namespace std;
vector<string> vec_NamesOfFunctions;
map<string, void (*)(double) > Map_NamesOfFunctionsToActualFunctions;
int main()
{
TestChosenFunctions( );
}
void TestChosenFunctions( ) {
int junk;
SetUpMapOfFunctionsToActualFunctions();
GetControlFunctionNamesFromDiskFile();
double x=1.0;
cout << "Here x = 1.0" << endl;
cout << "We selectively call only those functions whose names were written in the text file:" << endl;
for( auto itr=vec_NamesOfFunctions.begin(), end=vec_NamesOfFunctions.end() ; itr != end; ++itr)
{
if( Map_NamesOfFunctionsToActualFunctions.count(*itr) != 0 )
{
Map_NamesOfFunctionsToActualFunctions[ *itr ]( x ) ;
}
else
{
cout << "Error: Function name missing from the map!" << endl;
}
}
cout << "------------------------------------" << endl;
}
void GetControlFunctionNamesFromDiskFile( )
{
string ControlParamFileFunctionsForFunctions = "FileForNamesOfFunctions.TXT";
istringstream instream; // Declare an input string stream
ifstream inFile;
string firstStringOfLine;
string s;
inFile.open( ControlParamFileFunctionsForFunctions.c_str(), ios::in);
if ( ! inFile.is_open() )
{
cout << "Could not open Key Text Input file!" << endl;
exit(1);
}
while( getline(inFile, s) )
{
instream.clear();
instream.str(s);
instream >> firstStringOfLine;
vec_NamesOfFunctions.push_back(firstStringOfLine);
}
inFile.close();
cout << "The function names written in the disk file are: " << endl;
for( auto itr=vec_NamesOfFunctions.begin(), end =vec_NamesOfFunctions.end(); itr != end; ++itr)
{
cout<< *itr << endl; ;
}
cout << "---------------------------------------------" << endl;
}
void function0(double x) {cout<<"function0 prints 0*x = " << 0*x << endl; }
void function1(double x) {cout<<"function1 prints 1*x = " << 1*x << endl; }
void function2(double x) {cout<<"function2 prints 2*x = " << 2*x << endl; }
void function3(double x) {cout<<"function3 prints 3*x = " << 3*x << endl; }
void function4(double x) {cout<<"function4 prints 4*x = " << 4*x << endl; }
void function5(double x) {cout<<"function5 prints 5*x = " << 5*x << endl; }
void function6(double x) {cout<<"function6 prints 6*x = " << 6*x << endl; }
void SetUpMapOfFunctionsToActualFunctions()
{
Map_NamesOfFunctionsToActualFunctions["function0"]=function0;
Map_NamesOfFunctionsToActualFunctions["function1"]=function1;
Map_NamesOfFunctionsToActualFunctions["function2"]=function2;
Map_NamesOfFunctionsToActualFunctions["function3"]=function3;
Map_NamesOfFunctionsToActualFunctions["function4"]=function4;
Map_NamesOfFunctionsToActualFunctions["function5"]=function5;
Map_NamesOfFunctionsToActualFunctions["function6"]=function6;
}
////////////////////////////////////////////////////////////////////////////////////////
4) Here is the output of the program which seems to be correct:
The function names written in the disk file are:
function2
function3
function5
function6
---------------------------------------------
Here x = 1.0
We selectively call only those functions whose names were written in the text file:
function2 prints 2*x = 2
function3 prints 3*x = 3
function5 prints 5*x = 5
function6 prints 6*x = 6
------------------------------------
administrator@ubuntu:~/2013-CPPworks/Junk1$ ./a.out
The function names written in the disk file are:
function2
function3
function5
function6
---------------------------------------------
Here x = 1.0
We selectively call only those functions whose names were written in the text file:
function2 prints 2*x = 2
function3 prints 3*x = 3
function5 prints 5*x = 5
function6 prints 6*x = 6
------------------------------------
I have used the std=c++0x option of g++ for this program, by typing exactly this line:
g++ -std=c++0x MapsAndFunctions.cpp
But in a bigger program that does not use C++0x, I have similar problems. In the other program (which I am not revealing here), if I add more functions to the std::map (similar to the situation above), this time I do get a compiler error which disappears only if I add the function declarations to the actual program that uses these functions, adding the function declarations to the .h file did not prevent the compiler error at that time. (And in the latter case, the compiler error was exactly in the lines such as "MyMap["functionName"] = myFunction; ", and this error disappeared only when I added the function declaration to the .cpp file, it was not enough to put the function declaration in the .h file. But this happened only for some of the new functions used by the std::map (not all of them in the map), which makes me worry that the GVIM editor is introducing invisible characters after I made a mistake in the settings. Is this possible?)