error c2248, please help

error C2248: 'record::id_number' : cannot access private member declared in class 'record'
error C2248: 'record::type' : cannot access private member declared in class 'record'
error C2248: 'record::price' : cannot access private member declared in class 'record'
error C2248: 'record::num_in_stock' : cannot access private member declared in class 'record'
error C2248: 'record::title' : cannot access private member declared in class 'record'
error C2248: 'record::author_artist' : cannot access private member declared in class 'record'
error C2248: 'record::year' : cannot access private member declared in class 'record'

The program is suppose to find a certain data type and add the number in stock of the same data type.
___________________________________________________________record.h
#include <iostream>
#include <fstream>
#include <string>
#ifndef RECORD_H
#define RECORD_H
#endif
using namespace std;

class record
{
private:

int id_number;
string type;
double price;
int num_in_stock;
string title;
string author_artist;
int year;

public:

record();
record(int new_id_number, string new_type, double new_price, int new_num_in_stock,
string new_title, string new_author_artist, int new_year);
~record();

void set_id_number(int new_id_number);
void set_type(string new_type);
void set_price(double new_price);
void set_num_in_stock(int new_num_in_stock);
void set_title(string new_title);
void set_author_artist(string new_author_artist);
void set_year(int new_year);


int get_id_number();
string get_type();
double get_price();
int get_num_in_stock();
string get_title();
string get_author_artist();
int get_year();
};

___________________________________________________________record.cpp


#include "record.h"
using namespace std;

record::record()
{
id_number = 0;
type = "";
price = 0.0;
num_in_stock = 0;
title = "";
author_artist = "";
year = 0;
}
record::record(int new_id_number, string new_type, double new_price, int new_num_in_stock, string new_title, string new_author_artist, int new_year)
{
id_number = new_id_number;
type = new_type;
price = new_price;
num_in_stock = new_num_in_stock;
title = new_title;
author_artist = new_author_artist;
year = new_year;
}

record::~record()
{
}

void record::set_id_number(int new_id_number)
{
id_number = new_id_number;
}
void record::set_type(string new_type)
{
type = new_type;
}
void record::set_price(double new_price)
{
price = new_price;
}
void record::set_num_in_stock(int new_num_in_stock)
{
num_in_stock = new_num_in_stock;
}
void record::set_title(string new_title)
{
title = new_title;
}
void record::set_author_artist(string new_author_artist)
{
author_artist = new_author_artist;
}
void record::set_year(int new_year)
{
year = new_year;
}

int record::get_id_number(){return id_number;}

string record::get_type(){return type;}

double record::get_price(){return price;}

int record::get_num_in_stock(){return num_in_stock;}

string record::get_title(){return title;}

string record::get_author_artist(){return author_artist;}

int record::get_year(){return year;}

______________________________________________________main.cpp file

#include "record.cpp"


void read_all_records(record data[], int& valid);
int num_in_inventory_of_type(record data[], int& valid, string type);

int main()
{
record data[99];
int valids = 0;
string book = "book";
string cd = "cd";
string dvd = "dvd";

read_all_records(data, valids);
cout << "Book's: ";
num_in_inventory_of_type(data, valids, book);
cout << "CD's: ";
num_in_inventory_of_type(data, valids, cd);
cout << "DVD's: ";
num_in_inventory_of_type(data, valids, dvd);

return 0;
}

void read_all_records(record data[], int& valid)
{
ifstream fin;
fin.open("inventory.dat");

if(fin.fail())
{
cout << "File failed to open" << endl;
}
while(!fin.eof())
{
for(int i = 0; i < 100; i++)
{

fin >> data[i].id_number >> data[i].type >> data[i].price
>> data[i].num_in_stock >> data[i].title
>> data[i].author_artist >> data[i].year;

if(data[i].type == "book" || data[i].type == "cd" || data[i].type == "dvd")
{
valid++;
}
}
}
fin.close();
}

int num_in_inventory_of_type(record data[], int& valid, string type)
{
int num = 0;
for(int i = 0; i <= valid; i++)
{
if(data[i].type == type)
{
num += data[i].num_in_stock;
}
}
cout << num << endl;
return num;
}

_______________________________________________________inventory.dat file

123456
book
69.99
16
Problem_Solving_With_C++
Walter_Savitch
2011
123457
cd
9.99
32
Sigh_No_More
Mumford_and_Sons
2010
123458
dvd
17.99
15
Red_State
Kevin_Smith
2011
123459
cd
9.99
16
The_Church_Of_Rock_And_Roll
Foxy_Shazam
2012
123460
dvd
59.99
10
The_Walking_Dead_Season_1
Robert_Kirkman
2011
Last edited on
I didn't read all of your code... mainly because you need to use the code header files(button on the right looks like <>). I can tell you right now by looking at your errors though that:
'record::id_number', 'record::type', 'record::price', 'record::num_in_stock', 'record::title', 'record::author_artist', 'record::year'
all need to be in the PUBLIC part of your class, not in the PRIVATE.

Private is for only the class to use and is never called anywhere outside of the class. Public is called outside of the class(usually in the program)
Last edited on
You might consider making void read_all_records a friend of the class, although the more usual thing to do would be to define a function that reads one record as a friend of the class and use that within read_all_records. Or you could read the values into intermediary variables and then use the setter functions.


In num_in_inventory_of_type you just need to use the appropriate getter and setter functions.
Topic archived. No new replies allowed.