Compiling the following the class in the main program returns error C2784.

What could be causing the problem?

#include <iostream> // Include input/output stream
#include <cmath>

class threevector
{

public:
	double xcoord, ycoord, zcoord;
	
	// Default constructor
	threevector()
	{
		xcoord = 0.0;
		ycoord = 0.0;
		zcoord = 0.0;
	}
    
	// Cartesian constructor
	threevector(double x, double y, double z)
	{
		xcoord = x;
		ycoord = y;
		zcoord = z;
	}

	// Method to print out contents to screen
	void print()
	{
		std::cout << xcoord << '\t'
           		      << ycoord << '\t'
				  << zcoord << std::endl;
	}

	//Method to print out contents to file
	void print(std::ofstream fout)
	{
		fout << xcoord << '\t'
			 << ycoord << '\t'
			 << zcoord << std::endl;
	}
	
};

Dani AI

Generated

Short answer: the compiler error came from trying to pass a stream object by value. As suggested, and as discovered when creating the file stream in main, stream objects (std::ifstream/std::ofstream and their base classes) are not copyable, so a parameter declared as std::ofstream fout forces a copy and the compiler rejects it.

Why that matters: iostream objects own OS resources (file descriptors), internal buffers and state (position, error flags). Allowing a plain copy would create ambiguous ownership and lead to resource-management bugs (double-close, confused file positions). For that reason copy constructors/assignments for streams are disabled (private or deleted depending on the standard). Since C++11 most standard libraries provide move semantics for streams, but copying is still forbidden — passing by reference is the idiomatic solution.

A simple, flexible pattern is to accept the base stream type by non-const reference so the same function can print to cout, a file, or a stringstream:

void print(std::ostream &out)
{
    out << xcoord << '\t' << ycoord << '\t' << zcoord << '\n';
}

Usage: v.print(std::cout); or v.print(fout);. Note the reference must be non-const: streaming operators modify the stream (flags, position). Prefer '\n' over std::endl unless an immediate flush is required. If the function needs to own the file itself, make the std::ofstream a member (or accept and store a moved stream / unique_ptr) rather than attempting to copy it.

Recommended Answers

All 5 Replies

Unfortunately, most of us don't know what an error is just by the number. Since it starts with a 'C' we know it's a compiler error, not a linker error, but that's really it. What specifically is the error reported (not just the number, the text description)?

I suspect the problem is with your threevector::print() function. Your argument really should be a reference to an existing file stream, not a copy of one.

Thanks for the point!

I have modified the type of the argument in the print function to ofstream&.
Even then, the code does not compile. (I guess that's because the the argument does not refer to any ofstream object in the main program. Am I right?)

Next, I defined an ofstream object in the main program and then the code compiles.

My question is:
why should my argument be a reference to an existing file stream, not a copy of one? Please reply.

A reference allows you to make modifications to the argument/parameter and have those modifications be reflected in the original scope of the variable, which is necessary for the data stream to work properly.

"A reference allows you to make modifications to the argument/parameter and have those modifications be reflected in the original scope of the variable":

Ok! This I understand.

"which is necessary for the data stream to work properly.":

Why is it necessary for the data stream to work properly?

Inside a data stream, especially a file stream, there are pointers that keep track of your current read and write locations. If you can't modify the stream, the pointers will get messed up and break your stream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.