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

问题描述

您好!


我遇到以下问题:

Hello!

I have problem with the following:



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

..

..

..

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

推荐答案


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




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






只需要两件事:

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


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

06-28 04:49