Menu

Printing to file from implementation file

karmul
2008-09-20
2012-09-26
  • karmul

    karmul - 2008-09-20

    I have Dev-C++ project with three files: Student.cpp, student.h, and Main.cpp. I want to be able to print to a file from functions in the Student.cpp file, but can't.

    In the Main.cpp file I have successfully opened an output file and can print out to that output file. I want to also be able to print stuff that is happening inside a function in the implementation file (Student.cpp).

    I'm assuming that I need to somehow pass the name of the file I open in Main.cpp to the function in the implementation of the class, but I'm not sure how to do this. I've tried numerous variations of the syntax, and nothing I have tried has worked. And most examples I've seen in forums always show printing output from the Main.cpp file. How do you do this?

    Example code below:
    // Main - demonstrated an application separated
    // into two parts - the main() part

    include <cstdio>

    include <cstdlib>

    include <iostream>

    include <fstream>

    include "student.h"

    using namespace std;
    using namespace Schools;

    int main(int nArgc, char* pszArgs[])
    {
    std::ofstream out;
    out.open("out.txt", ios::out); //for writing
    if(!out) {
    cout << "Cannot open file.\n";
    return 1;
    }

    Schools::Student s(&quot;Sophie Moore&quot;, 1234, out); //attempting to pass the output file name to 
    //a function (in this example, the constructor - but really I might want to pass it to 
    //any function in the student class)
    out &lt;&lt; &quot;Student = &quot; &lt;&lt; s.display() &lt;&lt; endl; //this prints as expected to the output file
    
    out.close();
    
    system(&quot;PAUSE&quot;);
    return 0;
    

    }

    // Student.h file
    //Student class

    ifndef STUDENT

    define STUDENT

    namespace Schools
    {
    class Student
    {
    public:
    Student(char pszName, int nID);
    virtual char
    display();
    protected:
    // student's name
    char* pszName;
    int nID;
    };
    }

    endif

    // Student.cpp
    // Implement the methods of the Student class

    include <cstdio>

    include <cstdlib>

    include <iostream>

    include <string>

    include "student.h"

    namespace Schools
    {
    Student::Student(char pszNameArg, int nIDArg, fstream output) //the fstream* output
    //is what triggers the no matching function for call error (that I need to fix)

        : nID(nIDArg)
    {
        pszName = new char[strlen(pszNameArg) + 1];
        strcpy(pszName, pszNameArg);
        std::cout &lt;&lt; &quot;Student.cpp Test&quot; &lt;&lt; std::endl;
    }
    
    // display - return a description of student
    char* Student::display()
    {
        // copy the student's name into a block of heap
        // memory that we can return to the caller
        char* pReturn = new char[strlen(pszName) + 1];
        strcpy(pReturn, pszName);
        return pReturn;
    }
    

    }

    //Compile log
    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\C++\CPP_Programs\Chap22\Makefile.win"
    Executing make clean
    rm -f Student.o Main.o SeparateModules.exe

    g++.exe -D__DEBUG__ -c Student.cpp -o Student.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -I"C:/Dev-Cpp/include/c++/3.4.2/bits" -fexceptions -g3

    Student.cpp:10: error: fstream' has not been declared Student.cpp:13: error: ISO C++ forbids declaration ofoutput' with no type
    Student.cpp:13: error: prototype for Schools::Student::Student(char*, int, int*)' does not match any in classSchools::Student'
    student.h:8: error: candidates are: Schools::Student::Student(const Schools::Student&)

    student.h:10: error: Schools::Student::Student(char*, int)

    make.exe: *** [Student.o] Error 1

    Execution terminated

     
    • karmul

      karmul - 2008-09-21

      This solution totall worked. Thank you so much!

       
    • cpns

      cpns - 2008-09-21

      > I'm assuming that I need to somehow pass the name of the file I open in
      > Main.cpp to the function in the implementation of the class, but I'm not sure
      > how to do this.

      No, not the file name, but rather the ofstream object "out". Which is in fact what you have done.

      > Student.cpp:10: error: `fstream' has not been declared

      On this line you have an fstream* argument, but fstream is not declared. You need to include <fstream> and you need to use either std::ofstream or std::ifstream as necessary (std::ofstream in this case).

      > Student.cpp:13: error: prototype for Schools::Student::Student(char*, int, int*)' &gt; does not match any in classSchools::Student'

      You define the class with three parameters, but declared it in the header with only two.#

      The lines to change are marked below with /////////////////////. Note teh ise of reference arguments rather than pointers. Also you had a number fo redundant headers, and erroneously included <string> where you needed <cstring>. Although you may indeed in fact be better off using the C++ std::string class rather than the C string library. These may not be the only thisngs you need to fix in this code, I have only addressed your specific question, and the problems that were immediatly obvious.


      // Student.h file
      //Student class

      ifndef STUDENT

      define STUDENT

      include <fstream> ///////////////////////////////

      namespace Schools
      {
      class Student
      {
      public:
      Student(char* pszName, int nID, std::fstream& out ); //////////////////////////////////

      ...


      // Student.cpp
      // Implement the methods of the Student class

      include <iostream>

      include <cstring> ////////////////////////

      include <fstream> //////////////////////////

      include "student.h"

      namespace Schools
      {
      Student::Student(char* pszNameArg, int nIDArg, std::fstream& output) ////////////////////

      ...


      Clifford

       

Log in to post a comment.