tags:

views:

101

answers:

3

My code:

Functionality: It is a function expects a three arguments and create a file.

void performLog(string strStoredProcName, int nCount, double time)

{
    int tme=(int) time;

    int hour=tme/3600;

    tme=tme%3600;

    int min=tme/60;

    tme=tme%60;
    int sec=tme;

    char *StrLen;
    int len = 0;
    int lenpass = 0;

    StrLen = &strStoredProcName[0];
    len = strlen(StrLen);

    lenpass = 41 - len;

       fstream outFile( "Perform.out", ios_base::out | ios_base::app );

        if ( ! outFile ) 
        {
            cerr << "Cannot open 'Perform.out' for output" << "\n" << endl;
            exit(-1);
        }

    if (paramLogCreation == false)
    {

        outFile << "**************Performance Log*********************" << "\n" << endl; 
        outFile << "DB Type: MYSQL" << "\n" << endl;
        outFile << "-----------------------------------------------------------------------------------------" << "\n" << "\t" << "\t" << "\t" << "\t" << "Stored Procedure Statitics" << "\n" << endl;
        outFile << "-----------------------------------------------------------------------------------------" << "\n" << endl;
        outFile << "Store Procedure Name" << setw(30) << "Execution Count" << setw(30) << "Time Taken to Execute" << "\n" << endl; 
        paramLogCreation = true ;        
    }


    outFile << strStoredProcName << setw(lenpass) << nCount << setw(20) <<hour<<"::"<<min<<"::"<<sec <<"\n" << endl;
    outFile.close();
}

Here i am writing a unit test cases, for the code , which i have written, This function is one of the functions in that. Please help me , how to resolve this issue. Please i am very new one to the C++ and need to know the where i committed mistake.

+1  A: 

I think this is the error:

void performLog(string strStoredProcName, int nCount, double time)
[...]
char *StrLen;
[...]
StrLen = &strStoredProcName[0];

To convert a string to char*, you should use c_str:

StrLen = new char [strStoredProcName.size()+1];
strcpy (StrLen, strStoredProcName.c_str());
schnaader
thanks a lot for you valuable suggestion
+1  A: 

Assuming by string you mean std:;string then when you say:

StrLen = &strStoredProcName[0];
len = strlen(StrLen);

there is no guarantee that the proc name contains a null-terminated string for strlen() to work on. If you want the length of the string, use the size() member function:

len = strStoredProcName.size();

However, this should not cause the error message you are getting, so please indicate in your post where the error is!!!

anon
A: 

It looks like the error you're getting is in the header file (DM_simtoolTest.h), not the source file, which you appear to have pasted into the question.

The compiler is complaining that your code tries to use a function pointer (that's the strange int (*)(int) syntax) to get a string. Without seeing the code in question, my first (wild, speculative) guess would be that you forgot to quote the name of the function you're trying to test. I'd need to see the source of the header as well to properly identify the problem.

Commodore Jaeger
Exactly , you are right friend ,I missed th quote thanks a lot