数组和类的问题 [英] problem with array and classes

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

问题描述

您好!


我遇到以下问题:

Hello!

I have problem with the following:

来自班级''Transaktion'':
- -------------------------------------------------- -------------------------------------------------- -

..

..

..


Transaktion :: Transaktion(const Transaktion & t)

:typ(t.typ),datum(t.datum),namn(t.namn),belopp(t.belopp),

ant_kompisar (t.ant_kompisar)

{

if(ant_kompisar> 0){

kompisar = new string [ant_kompisar];

for(int i = 0; i< ant_kompisar; i ++)

kompisar [i] = t.kompisar [i];

}

其他

kompisar = 0;


}


..

..

..


bool Transaktion :: finnsKompis(string namnet){

for(int i = 0; i< ant_kompisar; i ++)

if(kompisar [i] == namnet)

返回true;

返回false;

}

..

..

..

来自班级'' TransaktionsLista'':
From the class ''Transaktion'': ------------------------------------------------------------------------------------------------------
..
..
..

Transaktion :: Transaktion(const Transaktion &t)
: typ(t.typ), datum(t.datum), namn(t.namn), belopp(t.belopp),
ant_kompisar(t.ant_kompisar)
{
if (ant_kompisar > 0) {
kompisar = new string[ant_kompisar];
for (int i = 0; i < ant_kompisar; i++)
kompisar[i] = t.kompisar[i];
}
else
kompisar = 0;

}

..
..
..

bool Transaktion :: finnsKompis(string namnet) {
for (int i = 0; i < ant_kompisar; i++)
if (kompisar[i] == namnet)
return true;
return false;
}
..
..
..
From the class ''TransaktionsLista'':



---------------------------------- -------------------------------------------------- ------------------

..

..

..

double TransaktionsLista :: aerSkyldig(string namnet){

double belopp = 0.0;

for(int i = 0;我< antalTrans; i ++){

Transaktion temp = Transaktion(trans [i]);

if(temp.finnsKompis(namnet))

belopp + = (trans [i] .haemta_belopp()/

(trans [i] .haemta_ant_kompisar()+ 1));

}

返回belopp;

}

..

..

..


我想在类''TransaktionsLista'中的Transaktion-object上使用来自类

''Transaktion''的方法finnsKompis(string namnet)。

代码编译正常但在运行时生成错误。我怀疑它是





''Transaktion''中的矩阵''kompisar []''有关但我不明白什么是错的。我在Transaktion课程中使用了一个

复制构造函数,但似乎没有

帮助。


我非常感谢任何建议!

提前致谢!

Fredrik


PS。对不起,我的代码是瑞典语...


------------------------------------------------------------------------------------------------------
..
..
..
double TransaktionsLista :: aerSkyldig(string namnet) {
double belopp = 0.0;
for (int i = 0; i < antalTrans; i++) {
Transaktion temp = Transaktion(trans[i]);
if ( temp.finnsKompis(namnet) )
belopp += ( trans[i].haemta_belopp() /
(trans[i].haemta_ant_kompisar() + 1) );
}
return belopp;
}
..
..
..

I want to use the method finnsKompis(string namnet) from the class
''Transaktion'' on a Transaktion-object in the class ''TransaktionsLista''.
The code compiles fine but generates an error on runtime. I suspect it
has
something to do with the matrix ''kompisar[]'' in the
''Transaktion''-class but I don''t understand what''s wrong. I do use a
copy-constructor in the class ''Transaktion'' but it doesn''t seem to
help.

I''m very greatful for any suggestions!
Thanks in advance!
Fredrik

PS. I''m sorry that the code is in swedish...

推荐答案



Fredda ; < FR **** @ home.se>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...

"Fredda" <fr****@home.se> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
你好!

我有以下问题:
Hello!

I have problem with the following:
来自班级''Transaktion'':
From the class ''Transaktion'':


-------------------------------------------------- -------------------------------------------------- -




Transaktion :: Transaktion(const Transaktion& t)
:typ(t.typ),datum( t.datum),namn(t.namn),belopp(t.belopp),
ant_kompisar(t.ant_kompisar)
{
if(ant_kompisar> 0){
kompisar =新字符串[ant_kompisar];
for(int i = 0; i< ant_kompisar; i ++)
kompisar [i] = t.kompisar [i];
}
其他
kompisar = 0;

}




bool Transaktion :: finnsKompis(string namnet){
for(int i = 0; i< ant_kompisar; i ++)
if(kompisar [i] == namnet)返回true;
返回false;
}




------------------------------------------------------------------------------------------------------
.
.
.

Transaktion :: Transaktion(const Transaktion &t)
: typ(t.typ), datum(t.datum), namn(t.namn), belopp(t.belopp),
ant_kompisar(t.ant_kompisar)
{
if (ant_kompisar > 0) {
kompisar = new string[ant_kompisar];
for (int i = 0; i < ant_kompisar; i++)
kompisar[i] = t.kompisar[i];
}
else
kompisar = 0;

}

.
.
.

bool Transaktion :: finnsKompis(string namnet) {
for (int i = 0; i < ant_kompisar; i++)
if (kompisar[i] == namnet)
return true;
return false;
}
.
.
.

来自班级'' TransaktionsLista'':
From the class ''TransaktionsLista'':


-------------------------------------- -------------------------------------------------- --------------



双TransaktionsLista :: aerSkyldig(字符串namnet){
double belopp = 0.0;
for(int i = 0;我< antalTrans; i ++){
Transaktion temp = Transaktion(trans [i]);
if(temp.finnsKompis(namnet))
belopp + =(trans [i] .haemta_belopp()/
(trans [i] .haemta_ant_kompisar()+ 1));
}
返回belopp;
}




我想在类''TransaktionsLista''中的Transaktion-object上使用类
''Transaktion''中的方法finnsKompis(string namnet)。
代码编译很好,但在运行时生成错误。我怀疑

''Transaktion''中的矩阵''kompisar []'有关 - 但是我不明白什么是错的。我在Transaktion课程中使用了一个
复制构造函数,但它似乎没有帮助。

我对任何建议都很满意!
提前致谢!
Fredrik

PS。对不起,代码是瑞典语...


------------------------------------------------------------------------------------------------------
.
.
.
double TransaktionsLista :: aerSkyldig(string namnet) {
double belopp = 0.0;
for (int i = 0; i < antalTrans; i++) {
Transaktion temp = Transaktion(trans[i]);
if ( temp.finnsKompis(namnet) )
belopp += ( trans[i].haemta_belopp() /
(trans[i].haemta_ant_kompisar() + 1) );
}
return belopp;
}
.
.
.

I want to use the method finnsKompis(string namnet) from the class
''Transaktion'' on a Transaktion-object in the class ''TransaktionsLista''.
The code compiles fine but generates an error on runtime. I suspect it
has
something to do with the matrix ''kompisar[]'' in the
''Transaktion''-class but I don''t understand what''s wrong. I do use a
copy-constructor in the class ''Transaktion'' but it doesn''t seem to
help.

I''m very greatful for any suggestions!
Thanks in advance!
Fredrik

PS. I''m sorry that the code is in swedish...




您还没有提供足够的信息。我们至少需要

你的'Transaktion'和''''TransaktionsLista''

类的定义。


Best是一个我们可以编译的小型完整程序。但是

一件事:你有没有观察到'3规则'? (这表明

如果你的类需要以下任何一个:复制构造函数,赋值

运算符(operator =())或析构函数,它很可能需要所有

三。)我怀疑这可能是你的问题。但是我只是在没有看到更多代码的情况下猜测。


-Mike



You have not supplied enough information. We need at least
the definitions of your ''Transaktion'' and ''''TransaktionsLista''
classes.

Best would be a small complete program which we can compile. But
one thing: have you observed the ''rule of 3''? (This states that
if your class needs any one of: copy constructor, assignment
operator (operator=()) or destructor, it very likely needs all
three.) I suspect this could be your problem. But I''m only
guessing without seeing more code.

-Mike


你好再次!


这是一个包含部分代码的可编译程序。

我在使用析构函数时遇到了很多问题

不要。

删除NULL指针不应该是一个问题,但有时我只是

得到

与下面的问题相同的错误。这就是为什么我已经取消了

重要部分

的析构函数。


感谢您的帮助!

Fredrik


#include< iostream>

#include< fstream>

#include< string>


使用命名空间std;


class Transaktion

{

private :

string datum;

string typ;

string namn;

double belopp;

int ant_kompisar;

string * kompisar;


public:

Transaktion();

Transaktion(const Transaktion& t);

~Transaktion();

//字符串haemta_namn();

double haemta_belopp( );

int haemta_ant_kompisar();

bool finnsKompis(字符串namnet);

// bool laesEnTrans(istream& is);

void skrivEnTrans(ostream& os);

}; //结束Transaktion


// Standardkonstruktor

Transaktion :: Transaktion()

:datum(" 060118"), typ(" Food"),namn(" Mike"),belopp(10.0),

ant_kompisar(3)

{

kompisar = new string [ant_kompisar];

kompisar [0] =" Fredrik";

kompisar [1] =" Karl";

kompisar [2] =" Richard";

kompisar [3] =" Linn";

}


// Kopieringskonstruktor

Transaktion :: Transaktion(const Transaktion& t)

:typ(t.typ),datum(t.datum),namn(t。 namn),belopp(t.belopp),

ant_kompisar(t.ant_kompisar)

{

if(ant_kompisar> 0){

kompisar =新字符串[ant_kompisar];

for(int i = 0; i< ant_kompisar; i ++)

kompisar [i] = t.kompisar [i];

}

其他

kompisar = 0;


}


// Destruktor

//删除[] kompisar;

}


double Transaktion :: haemta_belopp(){

返回belopp;

}


int Transaktion :: haemta_ant_kompisar(){

返回ant_kompisar;

}


void Transaktion :: skrivEnTrans(ostream& os){

if(ant_kompisar> 0){

if(os){

os<<数据<< " \t" << typ<< " \t" << namn<< " \t" << belopp

<< " \t" << ant_kompisar;

for(int i = 0; i< ant_kompisar; i ++)

os<< " \t" << kompisar [i];

os<< endl;

}

其他cout<< 输出流错误;

}

}


bool Transaktion :: finnsKompis(string namnet){

for(int i = 0; i< ant_kompisar; i ++)

if(kompisar [i] == namnet)

return true;

返回false;

}

类TransaktionsLista

{

private:

int antalTrans;

Transaktion * trans;


public:

TransaktionsLista();

~TransaktionsLista();

// void laesin(istream& is);

void skrivut(ostream& os);

void laggTill(Transaktion& t);

// double totalkostnad();

// double liggerUteMed(string namnet);

double aerSkyldig(string namnet);

// PersonLista FixaPersoner();

}; //结束TransaktionsLista


TransaktionsLista :: TransaktionsLista()

:antalTrans(0),trans(0)

{; }


TransaktionsLista :: ~TransaktionsLista(){

//删除[] trans;

}
<如果(antalTrans> 0){

Transaktion * temp = 0; br />
temp = new Transaktion [antalTrans + 1];

for(int i = 0; i< antalTrans; i ++)

temp [i ] = trans [i];

temp [antalTrans] = t;

delete [] trans;

trans = temp;

antalTrans ++;

}

else {

trans = new Transaktion [1];

trans [0] = t;

antalTrans ++;

}

}


//这个一个人不工作!

double TransaktionsLista :: aerSkyldig(string namnet){

double belopp = 0.0;

for(int i = 0; i< antalTrans; i ++){

Transaktion temp = Transaktion(trans [i]);

if(temp.finnsKompis(namnet))

belopp + =(trans [i] .haemta_belopp()/

(t rans [i] .haemta_ant_kompisar()+ 1));

}

返回belopp;

}


//这个也不行! - 同样的问题??

void TransaktionsLista :: skrivut(ostream& os){

for(int i = 0; i< antalTrans; i ++){

Transaktion temp = Transaktion(trans [i]);

temp.skrivEnTrans(os);

}

}


int main(){

Transaktion a;

Transaktion b;

Transaktion c;

TransaktionsLista tl;

tl.laggTill(a);

tl.laggTill(b);

tl .laggTill(c);

tl.aerSkyldig(" Karl"); //< - 这不起作用

ofstream os(" temp.txt");

tl.skrivut(os); //< - 也不是......

os.close();

返回(0);

}

Hi again!

Here comes a compilable program with parts of the code.
I''ve had quite a few problems whith when to use a destructor and when
not to.
It shouldn''t be a problem to delete a NULL-pointer but sometimes I just
get the
same error as in the problem below. Thats why I''ve inactivated the
vital parts
of the destructors.

Thanks for your help!
Fredrik

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

using namespace std;

class Transaktion
{
private:
string datum;
string typ;
string namn;
double belopp;
int ant_kompisar;
string *kompisar;

public:
Transaktion();
Transaktion(const Transaktion &t);
~Transaktion();
//string haemta_namn();
double haemta_belopp();
int haemta_ant_kompisar();
bool finnsKompis( string namnet );
//bool laesEnTrans( istream &is );
void skrivEnTrans( ostream & os );
}; //End Transaktion

// Standardkonstruktor
Transaktion :: Transaktion()
: datum("060118"), typ("Food"), namn("Mike"), belopp(10.0),
ant_kompisar(3)
{
kompisar = new string[ant_kompisar];
kompisar[0] = "Fredrik";
kompisar[1] = "Karl";
kompisar[2] = "Richard";
kompisar[3] = "Linn";
}

// Kopieringskonstruktor
Transaktion :: Transaktion(const Transaktion &t)
: typ(t.typ), datum(t.datum), namn(t.namn), belopp(t.belopp),
ant_kompisar(t.ant_kompisar)
{
if (ant_kompisar > 0) {
kompisar = new string[ant_kompisar];
for (int i = 0; i < ant_kompisar; i++)
kompisar[i] = t.kompisar[i];
}
else
kompisar = 0;

}

// Destruktor
Transaktion :: ~Transaktion() {
//delete [] kompisar;
}

double Transaktion :: haemta_belopp() {
return belopp;
}

int Transaktion :: haemta_ant_kompisar() {
return ant_kompisar;
}

void Transaktion :: skrivEnTrans(ostream &os) {
if (ant_kompisar > 0) {
if (os) {
os << datum << "\t" << typ << "\t" << namn << "\t" << belopp
<< "\t" << ant_kompisar;
for (int i = 0; i < ant_kompisar; i++)
os << "\t" << kompisar[i];
os << endl;
}
else cout << "output stream error";
}
}

bool Transaktion :: finnsKompis(string namnet) {
for (int i = 0; i < ant_kompisar; i++)
if (kompisar[i] == namnet)
return true;
return false;
}
class TransaktionsLista
{
private:
int antalTrans;
Transaktion *trans;

public:
TransaktionsLista();
~TransaktionsLista();
//void laesin( istream & is );
void skrivut( ostream & os );
void laggTill( Transaktion & t );
//double totalkostnad();
//double liggerUteMed( string namnet );
double aerSkyldig( string namnet );
//PersonLista FixaPersoner();
};// End TransaktionsLista

TransaktionsLista :: TransaktionsLista()
: antalTrans(0), trans(0)
{ ; }

TransaktionsLista :: ~TransaktionsLista() {
//delete [] trans;
}

void TransaktionsLista :: laggTill(Transaktion &t) {
if (antalTrans > 0) {
Transaktion *temp = 0;
temp = new Transaktion[antalTrans+1];
for (int i = 0; i < antalTrans; i++)
temp[i] = trans[i];
temp[antalTrans] = t;
delete [] trans;
trans = temp;
antalTrans++;
}
else {
trans = new Transaktion[1];
trans[0] = t;
antalTrans++;
}
}

// This one doesn''t work!
double TransaktionsLista :: aerSkyldig(string namnet) {
double belopp = 0.0;
for (int i = 0; i < antalTrans; i++) {
Transaktion temp = Transaktion(trans[i]);
if ( temp.finnsKompis(namnet) )
belopp += ( trans[i].haemta_belopp() /
(trans[i].haemta_ant_kompisar() + 1) );
}
return belopp;
}

// This one doesn''t work either! - Same problem??
void TransaktionsLista :: skrivut(ostream &os) {
for (int i = 0; i < antalTrans; i++) {
Transaktion temp = Transaktion(trans[i]);
temp.skrivEnTrans(os);
}
}

int main () {
Transaktion a;
Transaktion b;
Transaktion c;
TransaktionsLista tl;
tl.laggTill(a);
tl.laggTill(b);
tl.laggTill(c);
tl.aerSkyldig("Karl"); // <-- This doesn''t work
ofstream os("temp.txt");
tl.skrivut(os); // <-- Nor this...
os.close();
return (0);
}


Fredda schrieb:
Fredda schrieb:
再次嗨!

这是一个包含部分代码的可编译程序。<我有很多问题,什么时候使用析构函数,什么时候不使用。
删除NULL指针不应该是一个问题,但有时我只是<得到与下面问题相同的错误。这就是为什么我已经灭活了析构函数的重要部分。

[代码剪辑]
Hi again!

Here comes a compilable program with parts of the code.
I''ve had quite a few problems whith when to use a destructor and when
not to.
It shouldn''t be a problem to delete a NULL-pointer but sometimes I just
get the
same error as in the problem below. Thats why I''ve inactivated the
vital parts
of the destructors.

[code snipped]




只需要两件事:

1.学会使用std :: vector

2.学习使用std :: vector< std :: string>


/ S.

-

Stefan Naewe

naewe.s_AT_atlas_DOT_de



Just two things:
1. learn to use std::vector
2. learn to use std::vector<std::string>

/S.
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de


这篇关于数组和类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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