C++ ifstream/private variable

Classes.cpp:


#include "Classes.h"

Car::Car()
{
}

int Car::GetYear()
{
return year;
}
int Car::GetSpeed()
{
return speed;
}
string Car::GetColor()
{
return color;
}

void Car::SetYear(int newYear)
{
year = newYear;
}
void Car::SetSpeed(int newSpeed)
{
speed = newSpeed;
}
void Car::SetColor(string newColor)
{
color = newColor;
}

void Car::Read(string filename)
{
string extra;
string word = " ";
Car words;
words.SetColor(" ");

ifstream file;
int i = 0;

cout << "Enter file name to read in." << endl;
cin >> filename;

file.open(filename);

if (file.is_open())
{
file >> words.GetColor;

}




}

Classes.h:

#ifndef CLASSES_H
#define CLASSES_H


#include <iostream>
#include <fstream>
#include <string>
#include "OtherClass.h"

using namespace std;

class Car
{
private: // Member varaibles
int year;
int speed;
string color;

public: // Member functions
int GetYear();
int GetSpeed();
string GetColor();

Car();

void SetYear(int newYear);
void SetSpeed(int newSpeed);
void SetColor(string newColor);

void Read(string filename);
};

#endif

Hello Thetersures,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

And your question is? or the answer is 42.

Andy
Hello Thetersures,

When I could read your code in the header file you do not need the include files. These should be in a ".cpp" file. With the exception of "OtherClass.h" file. And never put the line "using namespace std;" in a header file. Read this:
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ That using statement should not be used in the first place, but is useful in a limited scope.

Hope that helps,

Andy
Topic archived. No new replies allowed.