473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

c++ objects

-------------->>>>>This is my header file:

#ifndef PROTEIN_
#define PROTEIN_

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

using namespace std;

class Protein { // class Protein

public:
std::string protName_;
std::string molWeight_;
std::string protFamily_;

ProtWrite(int, int, int);
~ProtWrite();

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

};

#endif //PROTEIN_

-------------->>>>>>>>>>>>>>< And this is the cpp file:

#include <iostream>
#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, prt.molWeight_);

std::cout << "Please enter the family: ";
std::getline(std::cin, prt.protFamily_);
return 0;
}

Now, when I try to compile it I get the following error:

protein.h:17: error: ISO C++ forbids declaration of 'ProtWrite' with
no type
protein.h:18: error: expected class-name before '(' token
protein.h:20: error: ISO C++ forbids declaration of 'ProtRead' with no
type
protein.h:21: error: expected class-name before '(' token
data.c++:9: error: ISO C++ forbids declaration of 'ProtWrite' with no
type
Any suggestions????

Cheers,
Paul

Feb 4 '07 #1
16 2226
In article <11**********************@j27g2000cwj.googlegroups .com>,
pa********@gmail.com says...

[ ... ]
class Protein { // class Protein

public:
std::string protName_;
std::string molWeight_;
std::string protFamily_;

ProtWrite(int, int, int);
~ProtWrite();

ProtRead(int, int, int);
~ProtRead();
You really need to pick up a decent book on C++. You're trying hard, but
a lot of the problems you're encountering are ones a newsgroup can't
really deal with very well.

The only kind of function that doesn't need a return type is a
constructor. A constructor always has the same name as its class.
Likewise, the only function name that starts with '~' is the destructor.
The remainder of the name must also be the same as the class name. You
can have multiple constructors, but when you do, you're overloading a
function, which means each must have a unique signature (different
parameters and/or qualifications, such as 'const'). With one minor
exception we'll ignore for the moment, a class can only have one
destructor -- since a destructor normally takes no arguments and has not
qualifiers, you can't overload it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 4 '07 #2
Amira wrote:
-------------->>>>>This is my header file:

#ifndef PROTEIN_
#define PROTEIN_

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

using namespace std;

class Protein { // class Protein

public:
std::string protName_;
std::string molWeight_;
std::string protFamily_;

ProtWrite(int, int, int);
~ProtWrite();

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

};

#endif //PROTEIN_

-------------->>>>>>>>>>>>>>< And this is the cpp file:

#include <iostream>
#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, prt.molWeight_);

std::cout << "Please enter the family: ";
std::getline(std::cin, prt.protFamily_);
return 0;
}

Now, when I try to compile it I get the following error:

protein.h:17: error: ISO C++ forbids declaration of 'ProtWrite' with
no type
protein.h:18: error: expected class-name before '(' token
protein.h:20: error: ISO C++ forbids declaration of 'ProtRead' with no
type
protein.h:21: error: expected class-name before '(' token
data.c++:9: error: ISO C++ forbids declaration of 'ProtWrite' with no
type
Any suggestions????

Cheers,
Paul
Unfortunately the basic problem is that your code doesn't make any
sense. It's not easily correctable. For instance you have written a
method called ProtWrite which actually *reads* from a file (and doesn't
do that correctly either). Add all the conceptual and syntactic mistakes
and what you have is not a reasonable basis to build on.

Try explaining what you are actually trying to write. For instance a
class that can be read from and writen to an external source (such as a
file) is bread and butter. If that is what you are tring to write then
say so. It's a simple piece of code and I could post it for you easily
enough.

John
Feb 4 '07 #3
Hi Jerry,
>
You really need to pick up a decent book on C++. You're trying hard, but
a lot of the problems you're encountering are ones a newsgroup can't
really deal with very well.
you are so damn right and this is not supposed to be ironic. I
apologise. I am not a programmer but more into molecular biology and
need urgently to finish up this code. This is why I stressed the
patience of this newsgroup! It's the small pieces that a book does not
give any clues about. I have three c++ books in front of mine and
still confused :-(

cheers, paul

Feb 4 '07 #4
Amira wrote:
-------------->>>>>This is my header file:

#ifndef PROTEIN_
#define PROTEIN_

#include <iostream>
#include <fstream>
Best not to drag in headers your file doesn't require.
#include <string>

using namespace std;
NEVER EVER put a using namespace in a header!
class Protein { // class Protein
Avoid superfluous comments, they distract the reader without adding value.
public:
std::string protName_;
std::string molWeight_;
std::string protFamily_;

ProtWrite(int, int, int);
~ProtWrite();

ProtRead(int, int, int);
~ProtRead();
What are these four supposed to be?
};

#endif //PROTEIN_

--
Ian Collins.
Feb 4 '07 #5
Hey John!,

your kindness will be remembered!

Actually, I need one header file (protein.h) and one code file
(protein.c). Then I need to define two operators or functions.

-one that will read a file such as the one below and extract the
values for protName_, protFamily_, molWeight_

-and another one that will ask for those above values to be entered
and writes them into a file with the following format:

<Protein>
<Name>...</Name>
<Family>...</Family>
<Weight>...<Weight>
</Protein>

If you could help further I will appreciate since I am just before a
nerve crisis!

cheers, paul

Feb 4 '07 #6
Hi Ian!,
#ifndef PROTEIN_
Best not to drag in headers your file doesn't require.
#include <string>
using namespace std;

NEVER EVER put a using namespace in a header!
Do I understand it correctly? I do not need any header files in
protein.h or is it just about the string header file. Ok, I won't use
namespace again!

>
class Protein { // class Protein

Avoid superfluous comments, they distract the reader without adding value.
public:
std::string protName_;
std::string molWeight_;
std::string protFamily_;
ProtWrite(int, int, int);
~ProtWrite();
ProtRead(int, int, int);
~ProtRead();
As John described above this is my crappy imagination. I wanted to
write just one constructor. I changed those four lines to Protein(int,
int, int); and ~Protein();

cheers,
paul

Feb 4 '07 #7
Amira wrote:
Hey John!,

your kindness will be remembered!

Actually, I need one header file (protein.h) and one code file
(protein.c). Then I need to define two operators or functions.

-one that will read a file such as the one below and extract the
values for protName_, protFamily_, molWeight_

-and another one that will ask for those above values to be entered
and writes them into a file with the following format:

<Protein>
<Name>...</Name>
<Family>...</Family>
<Weight>...<Weight>
</Protein>

If you could help further I will appreciate since I am just before a
nerve crisis!

cheers, paul
OK paul, what you are asking for is actually extremely complicated. You
have really been asked to extract values from an XML file? I can't quite
believe it, that's far too hard for a beginner. So the code below is a
bit simpler, its reads the protein from values typed in by the user,
then writes them out as described.

Code could be improved, molWeight should probably be a double for
instance, but there are some complexities with that, so I haven't
bothered. Hopefully it's good enough to give you a start.

Generally speaking I think you need to get some concepts right; what is
a constructor, what is a destructor, what is a method, what is member
variable, what is a local variable, when you would use all these things,
what is the point of public and private? But this is all basic concepts
that every beginner has to learn, it is daunting at first.

// protein.h
#ifndef PROTEIN_H
#define PROTEIN_H

#include <string>
#include <iostream>

class Protein
{
private:
std::string protName;
std::string molWeight;
std::string protFamily;
public:
void read();
void write(std::ostream& output) const;
};

#endif

//protein.cpp
#include "protein.h"
#include <fstream>

// read protein from standard input
void Protein::read()
{
std::cout << "Please enter the name: ";
std::getline(std::cin, protName);

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

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

// write out protein object as XML
void Protein::write(std::ostream& output) const
{
output << "<protein>\n";
output << " <Name>" << protName << "</Name>\n";
output << " <Family>" << protFamily << "</Family>\n";
output << " <Weight>" << molWeight << "</Weight>\n";
output << "</protein>\n";
}

int main()
{
Protein p;
p.read();
std::ofstream file("protein.txt");
p.write(file);
}
Feb 4 '07 #8
Amira wrote:
Hi Ian!,

>>>#ifndef PROTEIN_

>>Best not to drag in headers your file doesn't require.

>>>#include <string>
>>>using namespace std;

NEVER EVER put a using namespace in a header!


Do I understand it correctly? I do not need any header files in
protein.h or is it just about the string header file. Ok, I won't use
namespace again!
You don't need headers that define things you don't use, in this case,
your class doesn't use any streams. You do use std::string, so you
should include <string>.

--
Ian Collins.
Feb 4 '07 #9
John Harrison wrote:
Amira wrote:
>Hey John!,

your kindness will be remembered!

Actually, I need one header file (protein.h) and one code file
(protein.c). Then I need to define two operators or functions.

-one that will read a file such as the one below and extract the
values for protName_, protFamily_, molWeight_

-and another one that will ask for those above values to be entered
and writes them into a file with the following format:
Perhaps I didn't read your description closely enough. I've written two
*methods*, I haven't written any operators or functions, but these terms
are somewhat ambiguous so maybe I've done the right thing.

Also my two methods *together* perform exactly what your second
description says. And as I said before I haven't written anything like
what you first description says because that is way too complicated, and
I would guess you've misunderstood what you are supposed to do.

John
Feb 4 '07 #10
Hi John!,

no, it's absolutely perfect. I wasn't supposed to write an XML parser
but only a small piece that will serve as a building unit.

I copied both, protein.h and protein.c into the same folder under suse
linux. Now, when I try to compile there are several errors:
protein.c:6: error: no 'void Protein::read()' member function declared
in class 'Protein'
protein.c: In member function 'void Protein::read()':
protein.c:9: error: 'protName' is not a member of 'Protein'
protein.c:12: error: 'molWeight' was not declared in this scope
protein.c:15: error: 'protFamily' was not declared in this scope
protein.c: At global scope:
protein.c:20: error: no 'void Protein::write(std::ostream&) const'
member function declared in class 'Protein'
protein.c: In member function 'void Protein::write(std::ostream&)
const':
protein.c:23: error: 'protName' was not declared in this scope
protein.c:24: error: 'protFamily' was not declared in this scope
protein.c:25: error: 'molWeight' was not declared in this scope

Could it be that I should give the path for the header file?

cheers, paul

Feb 4 '07 #11
Hi John!,

no, it's absolutely perfect. I wasn't supposed to write an XML parser
but only a small piece that will serve as a building unit.

I copied both, protein.h and protein.c into the same folder under suse
linux. Now, when I try to compile there are several errors:
protein.c:6: error: no 'void Protein::read()' member function declared
in class 'Protein'
protein.c: In member function 'void Protein::read()':
protein.c:9: error: 'protName' is not a member of 'Protein'
protein.c:12: error: 'molWeight' was not declared in this scope
protein.c:15: error: 'protFamily' was not declared in this scope
protein.c: At global scope:
protein.c:20: error: no 'void Protein::write(std::ostream&) const'
member function declared in class 'Protein'
protein.c: In member function 'void Protein::write(std::ostream&)
const':
protein.c:23: error: 'protName' was not declared in this scope
protein.c:24: error: 'protFamily' was not declared in this scope
protein.c:25: error: 'molWeight' was not declared in this scope

Could it be that I should give the path for the header file?

cheers, paul

Feb 4 '07 #12
Hi John!,

no, it's absolutely perfect. I wasn't supposed to write an XML parser
but only a small piece that will serve as a building unit.

I copied both, protein.h and protein.c into the same folder under suse
linux. Now, when I try to compile there are several errors:
protein.c:6: error: no 'void Protein::read()' member function declared
in class 'Protein'
protein.c: In member function 'void Protein::read()':
protein.c:9: error: 'protName' is not a member of 'Protein'
protein.c:12: error: 'molWeight' was not declared in this scope
protein.c:15: error: 'protFamily' was not declared in this scope
protein.c: At global scope:
protein.c:20: error: no 'void Protein::write(std::ostream&) const'
member function declared in class 'Protein'
protein.c: In member function 'void Protein::write(std::ostream&)
const':
protein.c:23: error: 'protName' was not declared in this scope
protein.c:24: error: 'protFamily' was not declared in this scope
protein.c:25: error: 'molWeight' was not declared in this scope

Could it be that I should give the path for the header file?

cheers, paul

Feb 4 '07 #13
John!

everything works smoothly and I learned pretty much a lot after
fighting the whole weekend with this piece of code.

I really appreciate what you've done for me and I am most obliged. You
really were of incredible help! Thanks a lot!!!!

Cheers,
Paul

Feb 5 '07 #14
Amira wrote:
Hi John!,

no, it's absolutely perfect. I wasn't supposed to write an XML parser
but only a small piece that will serve as a building unit.

I copied both, protein.h and protein.c into the same folder under suse
linux. Now, when I try to compile there are several errors:
protein.c:6: error: no 'void Protein::read()' member function declared
in class 'Protein'
protein.c: In member function 'void Protein::read()':
protein.c:9: error: 'protName' is not a member of 'Protein'
protein.c:12: error: 'molWeight' was not declared in this scope
protein.c:15: error: 'protFamily' was not declared in this scope
protein.c: At global scope:
protein.c:20: error: no 'void Protein::write(std::ostream&) const'
member function declared in class 'Protein'
protein.c: In member function 'void Protein::write(std::ostream&)
const':
protein.c:23: error: 'protName' was not declared in this scope
protein.c:24: error: 'protFamily' was not declared in this scope
protein.c:25: error: 'molWeight' was not declared in this scope

Could it be that I should give the path for the header file?

cheers, paul
I have no trouble compiling John Harrison's code. Are you doing
anything exotic on the command line when you are compiling? Are you
SURE you copied the code correctly? Also, why did you name the source
file protein.c instead of protein.cpp (.c is usually used for C source
files rather than C++)?

From your setup I assume you are using g++ as your compiler. Version
4.1.1 is compiling it fine with:
$ g++ -Wall -ansi -pedantic protein.cpp

--
Alan Johnson
Feb 5 '07 #15
In article <11*********************@v45g2000cwv.googlegroups. com>,
pa********@gmail.com says...
Hey John!,

your kindness will be remembered!

Actually, I need one header file (protein.h) and one code file
(protein.c). Then I need to define two operators or functions.

-one that will read a file such as the one below and extract the
values for protName_, protFamily_, molWeight_

-and another one that will ask for those above values to be entered
and writes them into a file with the following format:

<Protein>
<Name>...</Name>
<Family>...</Family>
<Weight>...<Weight>
</Protein>
This isn't quite what you asked for, but it's reasonably close.

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iterator>
#include <limits>

// The name we'll use for each record.
char record_name[] = "protein";

// This defines the names of the fields we read/store/display. For most
// practical purposes, the remainder of the program has no knowledge of
// anything related to proteins, what we store about them, etc.
char const *field_names[] = {"Name", "Family", "Weight"};

// There are better ways to do this, but this works for our purposes.
#define elements(array) (sizeof(array)/sizeof(array[0]))

// The real heart of things: a field that can be read, written, entered
// or displayed. Reading and writing are for XML-format files. Enter and
// display are interactive.
// WARNING: field::read does NOT read most XML files -- it reads what
// field::write produces, but most other valid XML files will NOT be
// read correctly. field::write writes out all data verbatim, so entry
// of incorrect data (e.g. some punctuation marks) will produce invalid
// XML.
//
class field {
std::string name;
std::string value;

virtual std::istream &read(std::istream &is) {
std::string temp;
std::getline(is, temp);
int start_name = temp.find_first_of("<")+1;
int end_name = temp.find_first_of(">");
int start_value=end_name+1;
int end_value=temp.find_first_of("<", start_value);
name = std::string(temp, start_name, end_name-start_name);
value = std::string(temp, start_value, end_value-start_value);
return is;
}

virtual std::ostream &write(std::ostream &os) const {
os << "\t<" << name << ">";
os << value;
os << "</" << name << ">\n";
return os;
}
public:
field(std::string const &s) : name(s) {}
field() { }

void get_interactive(std::ostream &os, std::istream &is) {
os << "Please enter " << name << ": ";
is >value;
}

std::ostream &show_interactive(std::ostream &os) const {
return os << name << ": " << value << "\n";
}

friend std::ostream &operator<<(std::ostream &os, field const &f) {
return f.write(os);
}

friend std::istream &operator>>(std::istream &is, field &f) {
return f.read(is);
}
virtual ~field() {}
};

// A collection of fields, the number and names of which are found in
// the field_names array. The current version has read and write that
// attempt to produce XML( albeit with no header), but read and write
// could be overridden to allow reading and writing other formats.
//
template<class field, char const *record_name>
class record {
std::vector<fieldfields;

virtual std::istream &read(std::istream &is) {
std::string temp;
std::getline(is, temp);
for (int i=0; i<fields.size(); i++)
is >fields[i];
std::getline(is, temp);
return is;
}

virtual std::ostream &write(std::ostream &os) const {
os << "<" << record_name << ">\n";
std::copy(fields.begin(), fields.end(),
std::ostream_iterator<field>(os));
os << "</" << record_name << ">\n";
return os;
}

public:
record() {
for (int i=0; i<elements(field_names); i++)
fields.push_back(field(field_names[i]));
}

std::ostream & show_interactive(std::ostream &os) const {
os << "\n";
for (int i=0; i<fields.size(); i++)
fields[i].show_interactive(os);
return os;

}

std::istream &get_interactive(std::ostream &os, std::istream &is) {
for (int i=0; i<fields.size(); i++)
fields[i].get_interactive(os, is);
return is;
}

friend std::ostream &operator<<(std::ostream &os, record const &r){
return r.write(os);
}

friend std::istream &operator>>(std::istream &is, record &r) {
return r.read(is);
}

virtual ~record() {}
};

typedef record<field, record_namerec;

I'm not sure if you really need the 'show' functions, but they were easy
and might be needed (I used them in testing, so I left them). The read
and write functions do XML (with considerable limitations, as noted in
the comments), and the enter allows you to enter new data interactively.
This code has been minimally tested, but has no error handling, so it's
not exactly a model of robust code.

Note that rec is a single record -- you'll typically want a vector<rec>
or something on that order.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 5 '07 #16
John!,

thanks so much for the code. I really appreciate!! Thanks!

It's fine. The code works perfectly fine. Thanks guys for all the
assistance!

Cheers,
Paul

Feb 5 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
3
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements -...
8
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.