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
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)
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&)
> 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*)'
> 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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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;
}
}
// 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)
}
//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 typeStudent.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
This solution totall worked. Thank you so much!
> 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*)' > 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