c++ objects

A

Amira

Following is my header file:


#ifndef PROTEIN_
#define PROTEIN_

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Protein { // class Protein

private:
std::string protName_;
std::string molWeight_;
std::string protFamily_;
public:
ProtWrite(int, int, int);
~ProtWrite();

ProtRead(int, int, int);
~ProtRead();

};

#endif //PROTEIN_


++++++++++++The cpp file is as follows:

#include <string>
#include <fstream>
#include "protein.h"
using namespace std;
//using std::cout;


Protein::protWrite(int protName_, int molWeight_, int protFamily_)
{
ofstream myfile;
myfile.open ("protein.txt");
myfile << "<Protein>\n" << " <molWeight>" << molWeight_ << "</
molWeight>"
<< "\n</Protein>" << endl;
myfile.close();
};


int main(){

Protein prt;


std::cout << "Please enter the name: ";
std::getline(std::cin, prt.protName_);

std::cout << "Please enter the weight: ";
std::getline(std::cin, molWeight_);

std::cout << "Please enter the family: ";
std::getline(std::cin, protFamily_);


return 0;
}

Now, when I try to compile the code I get the error 'std::string
Protein::protName_' is private. Somebody and advice?

Cheers,
Paul
 
A

Andre Kostur

Following is my header file:


#ifndef PROTEIN_
#define PROTEIN_

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Protein { // class Protein

private:
std::string protName_;
std::string molWeight_;
std::string protFamily_;
public:
ProtWrite(int, int, int);
~ProtWrite();

ProtRead(int, int, int);
~ProtRead();

};

#endif //PROTEIN_


++++++++++++The cpp file is as follows:

#include <string>
#include <fstream>
#include "protein.h"
using namespace std;
//using std::cout;


Protein::protWrite(int protName_, int molWeight_, int
protFamily_) {
ofstream myfile;
myfile.open ("protein.txt");
myfile << "<Protein>\n" << " <molWeight>" <<
molWeight_ << "</
molWeight>"
<< "\n</Protein>" << endl;
myfile.close();
};


int main(){

Protein prt;


std::cout << "Please enter the name: ";
std::getline(std::cin, prt.protName_);

std::cout << "Please enter the weight: ";
std::getline(std::cin, molWeight_);

std::cout << "Please enter the family: ";
std::getline(std::cin, protFamily_);


return 0;
}

Now, when I try to compile the code I get the error 'std::string
Protein::protName_' is private. Somebody and advice?

Which part of "'std::string Protein::protName_' is private" don't you
understand? (Also, most compilers will tell you which line of your file
it's complaining about...)

If you look at your class, protName_ is declared as private.
 
A

Amira

Hi Andre!,
Which part of "'std::string Protein::protName_' is private" don't you
understand? (Also, most compilers will tell you which line of your file
it's complaining about...)

If you look at your class, protName_ is declared as private.

thanks a lot for your prompt reply. I have now fixed that problem but
another one occured. I described it in the new posting. Any help??

Cheers, paul
 
R

red floyd

Amira said:
Following is my header file:


#ifndef PROTEIN_
#define PROTEIN_

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

This line is bad. Very bad. While it's legal, you should never put a
"using namespace" declaration in a header file. Period.
 
P

Pronova

In the class Protein, you declared protName_ as private. This means that
you cannot access it outside of Protein. You would need to either make it
public or declare a public accessor method.

If you're going to make a class only just to encapsulate member variables
and nothing else, I would advise making these public. Otherwise, you would
have to make endless access methods for every member variable. A struct
might be the better choice if this object doesn't do anything but store
data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

c++ objects 16
memory access error 6
Character operations in C++ 2
Crossword 2
Lexical Analysis on C++ 1
TF-IDF 1
Cannot find my infinite loop 1
Crossword 14

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top