I am writing a program to input data from a file, format it and then output to another file. For some reason when i make the file I get this error:

----jGRASP exec: make

g++ -ggdb -c Billionaire.cpp
iostream:39:0,
                 from Billionaire.cpp:7:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream: In constructor 'Billionaire::Billionaire()':
ostream:384:7: error: 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits<char>]' is protected
       basic_ostream()
       ^
Billionaire.cpp:13:26: error: within this context
 Billionaire::Billionaire()
                          ^
makefile:4: recipe for target 'Billionaire.o' failed
make: *** [Billionaire.o] Error 1

 ----jGRASP wedge2: exit code for process is 2.
 ----jGRASP: operation complete.

Can anyone shed some light on thei error? I don't understand why the constructor is prducing an error with the ostream.

My code:
BSTProject.cpp

/**
 * @file Project1A.cpp
 * @author Ashley Van Hoesen
 * @date March 26, 2014
 * This program reads data about Billionaires from a semi-colon delimited text file named
 * ForbesBillionaires.txt. It provides an overview of the program for the user. The
 * program opens the input file, displays a full report that shows details for each
 * billionaire, including the name(s), rank, net worth, age, source, and country
 * for each billionaire shown in order by name. The program makes use of the Billionaire 
 * class to store data about each billionaire object and to display the data about
 * each billionaire. The program closes the file before exiting. The file is opened
 * and closed in the main() method.   
 */
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "Billionaire.h"

using namespace std;

//Name of the input file for the program.
const string INPUT_FILENAME = "ForbesBillionaires.txt";
const string OUTPUT_FILE = "BillionaireReports.txt";

/**
 * Displays a description of the program for the user.
 */
void description(void);

int main(void)
{
description();

ifstream inData;
inData.open(INPUT_FILENAME.c_str());

if(!inData)
{
cerr << "Error: File Not Found" << endl;
}//endif

ofstream outData;
outData.open(OUTPUT_FILE.c_str());

if(!outData)
{
cout << "Error: File Not Found" << endl;
}//endif

Billionaire b;
while(!inData.eof())
{
b.inData(inData);
b.outData(outData);
}

return EXIT_SUCCESS;
}

void description(void)
{
cout << "This program reads data about billionaires from a Forbes Report" << endl
     << "The program opens the input file, displays a full report that "  << endl
     << "shows details for each billionaire, including the name(s), rank," << endl
     << "net worth, age,source, and country for each billionaire shown in"  << endl
     << "order by name." <<endl
     << "Data is input from a file named " << INPUT_FILENAME << endl << endl;
}

Billionaire.h

/**
 * @file Billionaire.h
 * @author Ashley Van Hoesen
 * @date March 26, 2014
 * @class Billionaire
*/
#ifndef BILLIONAIRE_H
#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;

class Billionaire
{
private:

/**
 * Stores the name of the billionaire.
 */
string name;

 /**
 * Stores the rank of the billionaire.
 */
int rank;

 /**
 * Stores the net worth(in Billions of dollars)
 * of the billionaire.
 */
float worth;

/**
 * Stores the age of the billionaire.
 */
int age;

/**
 * Stores the source of wealth of the billionaire.
 */
string source;

/**
 * Stores the country of citizenship of the billionaire.
 */
string country;

ifstream inFile;

ostream outFile;

public:

/**
 * Default constructor for the billionaire class.
 */
Billionaire ();

/**
 * Accesses the billionaire's name.
 * @return the billionaire's name.
 */
string getName();

/**
 * Accesses the billionaire's rank.
 * @return the billionaire's rank.
 */
int getRank();

/**
 * Accesses the billionaire's net worth.
 * @return the billionaire's net worth.
 */
float getWorth();

/**
 * Accesses the billionaire's age.
 * @return the billionaire's age.
 */
int getAge();

/**
 * Accesses the billionaire's source of wealth.
 * @return the billionaire's source of wealth.
 */
string getSource();

/**
 * Accesses the billionaire's country of citizenship.
 * @return the billionaire's country of citizenship.
 */
string getCountry();

/**
 * Reads the input file and store them in attribute
 * @param &inFile - ForbesBillionaires.txt
 */
void inData(ifstream& inFile);


/**
 * Outputs the data for a single billionaire, formatted
 */
void outData(ostream& outFile);
};

#define BILLIONAIRE_H
#endif

Billionaire.cpp

/**
 * @file Billionaire.cpp
 * @author Ashley Van Hoesen
 * @date March 26, 2014
*/
 #include <cstdlib>
 #include <iostream>
 #include <iomanip>
 #include <fstream>
 #include <string>
#include "Billionaire.h"

Billionaire::Billionaire()
{
name = ' ';
rank = 0;

}

/**
 * Accesses the billionaire's name.
 * @return the billionaire's name.
 */
string Billionaire::getName()
{
return name;
}

/**
 * Accesses the billionaire's rank.
 * @return the billionaire's rank.
 */
int Billionaire::getRank()
{
return rank;
}

/**
 * Accesses the billionaire's net worth.
 * @return the billionaire's net worth.
 */
float Billionaire::getWorth()
{
return worth;
}

/**
 * Accesses the billionaire's age.
 * @return the billionaire's age.
 */
int Billionaire::getAge()
{
return age;
}

/**
 * Accesses the billionaire's source of wealth.
 * @return the billionaire's source of wealth.
 */
string Billionaire::getSource()
{
return source;
}

/**
 * Accesses the billionaire's country of citizenship.
 * @return the billionaire's country of citizenship.
 */
string Billionaire::getCountry()
{
return country;
}

/**
 * Reads the input file and store them in attribute
 * @param &inFile - ForbesBillionaires.txt
 */
void Billionaire::inData(ifstream& inFile)
{




getline (inFile, name, ';'); //Reads file until a semi-colon, stores to name

string tempRank; //Temporary Holding for the string which will be assigned to rank
getline (inFile, tempRank, ';'); //Reads file until a semi-colon, stores to tempRank
//if (tempRank != '-')
//{
rank = atoi ((tempRank.c_str())); //Uses atoi to convert the string tempRank to an integer
//}
//else
//{
//rank = '-';
//}

string tempWorth;//Temporary Holding for the string which will be assigned to worth
getline (inFile, tempWorth, ';'); //Reads file until a semi-colon, stores to tempWorth
//if (tempWorth != '-')
//{
worth = atof((tempWorth.c_str())); //Uses atof to convert the string tempWorth to an integer
//}
//else
//{
//worth = '-';
//}


string tempAge; //Temporary Holding for the string which will be assigned to age
getline (inFile, tempAge, ';'); //Reads file until a semi-colon, stores to tempAge
//if (tempAge != '-')
//{
age = atoi ((tempAge.c_str())); //Uses atoi to convert the string tempAge to an integer
//}
//else
//{
//age = '-';
//}

getline (inFile, source, ','); //Reads file until a semi-colon, stores to source
getline (inFile, country, ','); //Reads file until a semi-colon, stores to country

}

/**
 * Outputs the data for a single billionaire, formatted
 */
void Billionaire::outData(ostream& outFile)
{
//The output data is formatted accordingly
outFile
<< setw(12) << "Name: " << getName() << endl
<< setw(17) << "Rank: " << getRank() 
<< setw(12) << "Worth:" << left << setw(8) << getWorth() << right
<< setw(17) << "Age: " << getAge() << endl
<< setw(12) << "Source: " << getSource() << endl
<< setw(17) << "Country: " << getCountry() << endl << endl;
}

Recommended Answers

All 2 Replies

ifstream and ofstream cannot be members of a class like you have them. It's probably better to just have inData() and outData() declare ifstream/ofstrem, open the file, then read/write.

void Billionaire::outData()
{
    ofstream outFile("filename.txt");
    //The output data is formatted accordingly
    outFile
        << setw(12) << "Name: " << getName() << endl
        << setw(17) << "Rank: " << getRank()
        << setw(12) << "Worth:" << left << setw(8) << getWorth() << right
        << setw(17) << "Age: " << getAge() << endl
        << setw(12) << "Source: " << getSource() << endl
        << setw(17) << "Country: " << getCountry() << endl << endl;
}

Thank you. I honeslty didn't even notice I declared them in the header file. Just needed another pair of eyes.

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.