c ++对象 [英] c++ objects

查看:68
本文介绍了c ++对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-------------->>>>>这是我的头文件:


#ifndef PROTEIN_

#define PROTEIN_


#include< iostream>

#include< fstream>

# include< string>


使用命名空间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_


-------------->>>>>>>>>>>>>>>< ;这是cpp文件:


#include< iostream>

#include< string>

#include< ; fstream>

#include" protein.h"

using namespace std;

//使用std :: cout;

Protein :: ProtWrite(int protName_,int molWeight_,int protFamily_)

{

ofstream myfile;

myfile.open (" protein.txt");

myfile<< "<蛋白质> \\\
" << " < molWeight>" << molWeight_<< "< /

molWeight>"

<< " \\\
< /蛋白质>" << endl;

myfile.close();

};

int main(){


蛋白质prt;

std :: cout<< 请输入名称:" ;;

std :: getline(std :: cin,prt.protName_);


std :: cout< ;< 请输入重量:;

std :: getline(std :: cin,prt.molWeight_);


std :: cout< ;< 请输入家庭:;

std :: getline(std :: cin,prt.protFamily_);

返回0;

}


现在,当我尝试编译它时,我收到以下错误:


protein.h:17:错误:ISO C ++禁止声明''ProtWrite''

没有类型

protein.h:18:错误:'''''之前的预期类名>
protein.h:20:错误:ISO C ++禁止声明''ProtRead''没有

类型

protein.h:21:错误:在''之前预期的类名称('''令牌

data.c ++:9:错误:ISO C ++禁止声明''ProtWrite''没有

类型

任何建议????


干杯,

保罗

解决方案

在文章< 11 ********************** @ j27g2000cwj.googlegroups .com>,
< a href =mailto:pa ******** @ gmail.com> pa ******** @ gmail.com 说......


[。 ..]


class Protein {// class Protein


public:

std: :string protName_;

std :: string molWeight_;

std :: string protFamily_;


ProtWrite(int,int, int);

~ProtWrite();


ProtRead(int,int,int);

~ProtRead();



你真的需要在C ++上找到一本不错的书。你正在努力,但是你要遇到的很多问题都是新闻组无法实现的.b $ b真的非常好。


唯一一种不需要返回类型的函数是

构造函数。构造函数的名称始终与其类相同。

同样,唯一以''〜'开头的函数名是析构函数。

名称的其余部分也必须与班级名称相同。你可以拥有多个构造函数,但是当你这样做时,你会重载一个

函数,这意味着每个函数都必须有一个独特的签名(不同的

参数和/或资格,例如''const'')。有一个小的

异常,我们暂时忽略,一个类只能有一个

析构函数 - 因为析构函数通常不带参数而且没有

限定符,你不能超载它。


-

后来,

杰瑞。


宇宙是自己想象的虚构。


Amira写道:


-------------->>>>>这是我的头文件:


#ifndef PROTEIN_

#define PROTEIN_


#include< iostream>

#include< fstream>

#include< string>


使用命名空间std;


class Protein {// class Protein

public:

std :: string protName_;

std :: string molWeight_;

std :: string protFamily _;


ProtWrite(int,int,int);

~ProtWrite();


Prot Read(int,int,int);

~ProtRead();


};


#endif // PROTEIN_


-------------->>>>>>>>>>> ;>>><这是cpp文件:


#include< iostream>

#include< string>

#include< ; fstream>

#include" protein.h"

using namespace std;

//使用std :: cout;


Protein :: ProtWrite(int protName_,int molWeight_,int protFamily_)

{

ofstream myfile;

myfile.open(" protein.txt");

myfile<< "<蛋白质> \\\
" << " < molWeight>" << molWeight_<< "< /

molWeight>"

<< " \\\
< /蛋白质>" << endl;

myfile.close();

};


int main(){


蛋白质prt;


std :: cout<< 请输入名称:" ;;

std :: getline(std :: cin,prt.protName_);


std :: cout< ;< 请输入重量:;

std :: getline(std :: cin,prt.molWeight_);


std :: cout< ;< 请输入家庭:;

std :: getline(std :: cin,prt.protFamily_);


返回0;

}


现在,当我尝试编译它时,我收到以下错误:


protein.h:17 :错误:ISO C ++禁止声明''ProtWrite''

没有类型

protein.h:18:错误:预期的类名称''''''令牌

protein.h:20:错误:ISO C ++禁止声明''ProtRead''没有

类型

protein.h: 21:错误:在''之前预期的类名(''令牌

data.c ++:9:错误:ISO C ++禁止声明''ProtWrite''没有

类型


任何建议????


干杯,

Paul


不幸的是,基本的问题是你的代码没有任何感觉。它不容易纠正。例如你写了一个

方法称为ProtWrite实际上*从文件中读取*(并且没有
正确地执行此操作)。添加所有概念和语法错误

以及你所拥有的不是合理的基础。


尝试解释你实际上要写的内容。例如,可以从外部源(例如

文件)读取和写入的

类是面包和黄油。如果那就是你要写的话那么

这么说。这是一段简单的代码,我可以轻松地为你发布

够了。


John

嗨Jerry,


>

你真的需要拿一本关于C ++的好书。你正在努力,但是你要遇到的很多问题都是新闻组无法实现的.b $ b真的非常好。



你真是太棒了,这不应该是具有讽刺意味的。我向b $ b道歉。我不是程序员,而是更多的分子生物学和

迫切需要完成这段代码。这就是为什么我强调这个新闻组的耐心
!这是一本小书,而不是b $ b给出任何线索。我面前有三本c ++书籍和

仍然困惑:-(


欢呼,保罗


-------------->>>>>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

解决方案

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.


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


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


这篇关于c ++对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆