printing out to files

i Have a pure virtual function called print(). In each of my derived classed i have the function with it giving the commands

cout<<information<<number<<endl;

This works fine, but i want to print out the data to an outputfile which i have created in my main code. When i add the following to the print function to make it

cout<<information<<number<<endl;
out<<information<<number<<endl;

I get the error ' out was not declared in this scope '. Anybody know why this is and how i can correct it?
print(std::ostream &out);
i was able to get this into my classes without any errors but unfortunately when i call it in my main adn go to the output file nothing is printed.

THIS IS MY PURE VIRTUAL FUNCTION IN MY ABSTRACT BASE CLASS:

virtual void printf(std::ostream &out)=0;

THIS IS THE FUNCTION WRITTEN IN EACH OF THE DERIVED CLASSES.
void printf(std::ostream &out)
{
out<<"Student name: "<<sname<<" Student ID:"<<stdid<<endl<<endl<<lecture<<endl;
}

Withing the main i have made an out put file

const char outfile[]= "STD.txt";
ofstream out(outfile);

I attempted to call the function using

(*sdatait)->printf(out);

I am using iterators to print out. This line worked fine on the simple print() function which just printed the information to the command window.

Do you know how to correct this?
1) Please use legitimate English and correct your typos. I don't want to solve your words, I want to solve your problem.

2) The topic title is very vague. I see so many topics like this. Gee, why don't I pull out a gun and shoot everyone who makes these topics? According to Hitler, that works great for making the world a better place. But sorry mbcx7mm3 (your name? what the hell??), I'm not radical.

I get the error ' out was not declared in this scope '. Anybody know why this is and how i can correct it?


Wait are we still trying to correct that problem?
Last edited on
Apologies if i have annoyed you, mbcx7mm3 is just my username from work. And the typos, are because i am slightly dyslexic. I am relatively new to C++ and i am trying my best to learn.

Basically this is my issue,

I have a pure virtual function defined within my abstract base class 'virtual void print()=0;' . Within my derived class which is called 'physics' the function is coded as:

void print()
{

cout<<"Faculty: PHYSICS - Student name: "<<sname<<" - Student ID:"<<stdid<<" "<<endl<<endl<<lec<<endl;

}

This works fine, and prints out to the command window. However, i wish to also print out to a file and currently i can only do this by using the command within my main:

out<<physics;

i wish to however have a similar function as print() contained within my derived class which will print out to a file, calling the function printf(). Why is it not possible to write:

void printf()
{

out<<"Faculty: PHYSICS - Student name: "<<sname<<" - Student ID:"<<stdid<<" "<<endl<<endl<<lec<<endl;

}
[code] "Please use code tags" [/code]
what happen if you use (*it)->printf(std::cout); ?
Is the file created?

Post the calls and the definition of the methods
I have created the file already at the start of the main by

1
2
const char outfile[]= "STD.txt";	//Output file to print the student data to an output textfile with the name 'STD.txt'
	ofstream out(outfile);


The function to print out to this file which is in my class is (which unfortunately doesn't work)

1
2
3
4
void prinf(std::ostream &out)
	{
		out<<"|Faculty: PHYSICS\n | Student name: "<<sname<<" | Student ID:"<<stdid<<" | "<<endl<<endl<<lec<<endl;
	}


when i used (*sdataIter).second->printf(std::cout);

I got three errors talking about:-

typeinfo for abstract base class
vtable for absract base class

saying something about them being referenced from my derived classes. The errors, were really quite difficult to understand.
Paste the errors.
This works for me
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <vector>
#include <memory>

class A{
public:
	virtual void printf(std::ostream &) = 0;
};

class B:public A{
public:
	virtual void printf(std::ostream &out){
		out<<'B';
	}
};

class C:public A{
public:
	virtual void printf(std::ostream &){
		out<<'C';
	}
};

int main(int argc, char **argv){
	std::vector< A* > v;
	std::ofstream out("test");
	v.push_back( new B );
	v.push_back( new C );
	for(size_t K=0; K<v.size(); ++K)
		v[K]->printf(std::cout);
	for(std::vector<A*>::iterator it=v.begin(); it!=v.end(); ++it)
		(*it)->printf(out);

	for(size_t K=0; K<v.size(); ++K)
		delete v[K];
	return 0;
}
The errors that i got were,

"typeinfo for student", referenced from:
typeinfo for physicsin main.o
typeinfo for chemistryin main.o
typeinfo for biologyin main.o


"vtable for student", referenced from:
student::~student()in main.o
student::~student()in main.o
student::student()in main.o
student::student(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)in main.o

"student::printf(std::basic_ofstream<char, std::char_traits<char> >&)", referenced from:
vtable for chemistryin main.o
vtable for biologyin main.o


ld: symbol(s) not found
collect2: ld returned 1 exit status

thanks




"student::printf(std::basic_ofstream<char, std::char_traits<char> >&)", referenced from:

This means your student::printf function was never defined. This is usually because the coder forgot to declare it as a pure virtual function with "=0"
1
2
3
4
class student { 
public: 
    virtual void printf(std::ostream &) = 0; 
}

"vtable for student", referenced from:

Given the above error, I'd guess you also forgot to add the virtual keyword, resulting in a student class with no vtable.
"typeinfo for student", referenced from:

I'm not sure what would cause this. Correct the other errors, and then show us the entire student class if the problem is still there.
Topic archived. No new replies allowed.