CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    32

    need help to passing string in function

    I am trying to pass a string called fildir in a function closeFile(filedir)

    I have provided the header file and cpp file. I am new to classes I need some help passing a string

    -----------------------------------------------------------------------------
    header file


    #include <string>

    using std::string;
    /////////////////////////////////////////////////////////////////////////////
    // CStrengthCoachDlg dialog

    class CStrengthCoachDlg : public CDialog
    {
    // Construction
    public:
    CStrengthCoachDlg(CWnd* pParent = NULL); // standard constructor
    //void closeFile(char tempFilename[255]); //fm
    void closeFile(string* filedir);


    -----------------------------------------------------------------------
    .cpp file



    //void CStrengthCoachDlg::closeFile(char tempFilename[]) //close out file
    void CStrengthCoachDlg::closeFile(string& filedir) //close out file
    {
    ofstream out_stream;
    out_stream.close();
    string systemcall = "START " + filedir;
    system(systemcall.c_str());

    return;
    }



    ----------------------------------------------------------
    compile errors:

    Compiling...
    StrengthCoachDlg.cpp
    C:\Documents and Settings\fm\Desktop\Working_beta\Copy (2) of beta_strengthcoach_a\StrengthCoachDlg.cpp(776) : error C2664: 'closeFile' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocato
    r<char> >' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    C:\Documents and Settings\fm\Desktop\Working_beta\Copy (2) of beta_strengthcoach_a\StrengthCoachDlg.cpp(4677) : error C2511: 'closeFile' : overloaded member function 'void (class std::basic_string<char,struct std::char_traits<char>,class std::alloca
    tor<char> > &)' not found in 'CStrengthCoachDlg'
    c:\documents and settings\fm\desktop\working_beta\copy (2) of beta_strengthcoach_a\strengthcoachdlg.h(17) : see declaration of 'CStrengthCoachDlg'
    Error executing cl.exe.

  2. #2
    Join Date
    Mar 2001
    Posts
    439

    Re: need help to passing string in function

    I believe these don't match, and the compiler expects them to.
    Code:
    header file
    ---------------
    void closeFile(string* filedir);
    
    .cpp file
    ---------------
    closeFile(string& filedir)
    Choose either the "*" or the "&" and use the same in both, depending on how you want to pass the string.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: need help to passing string in function

    1) your prototype and definition of closeFile are different

    Code:
    void closeFile(string* filedir);   // in header file
    
    void CStrengthCoachDlg::closeFile(string& filedir) // in cpp file

    2) how do you call the closeFile function ?

    3) Your closeFile function ... I am confused on what file you are
    closing. You declare a new ofstream and immediate call close on it
    (without ever doing an open).
    Last edited by Philip Nicoletti; February 7th, 2007 at 07:50 AM.

  4. #4
    Join Date
    Nov 2006
    Posts
    32

    Re: need help to passing string in function

    I am trying to close a file using a function I created called closeFile

    I tried to pass in filedir but it did not work. I tried the code below and it does not work. Can somebody help me here. The main works and If I move the code that is closeFile to main it works but does not in the function.

    need some help here

    Thanks in advance.


    here is part of my main:
    // fileOutpName is entered from the GUI

    string filedir = "C:\\training_output\\" + FileOutputName;

    out_stream.open (filedir.c_str());

    globalfile = filedir; // set filedir to a global string
    ....
    ...
    ....
    closeFile(); // call closeFile


    void CStrengthCoachDlg::closeFile(void) //close out file
    {
    ofstream out_stream;
    out_stream.close();
    string systemcall = "START " + globalfile;

    system(systemcall.c_str());


    return;
    }

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: need help to passing string in function

    in closeFile, you created a new ofstream object, and called close
    on it. You need the existing ofstream object.

  6. #6
    Join Date
    Nov 2006
    Posts
    32

    Re: need help to passing string in function

    Thank You. I learn so much from you.

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