这个项目的目的是建立自己的单元测试并使用c-strings。
项目:DNA测序链
以下是重叠时匹配的两条DNA链的示例:
Strand 1: ACGGACATAGTCATT
Strand 2: CATAGTCATTTCATG
Combined: ACGGACATAGTCATTTCATG
当我尝试实施以下内容时,我感到很茫然:
Strand
必须有一个方法Strand substrand(size_t i, size_t j) const
,该方法会返回从位置i
开始并在位置j - 1
结束的链的副本。想想“Python切片。”
更新:
我最近回答了自己的问题并更新了void test_substrand()
和Strand Strand::substrand(size_t start, size_t end) const
。我收到了分段错误。
到目前为止,这是我的代码,其文件名在其正文下方。
void test_substrand()
{
Strand s1;
s1.setStrand("Hellow World");
s1.substrand(1, 4);
std::cout << "Substrand: " << s1.getStrand() << std::endl;
}
int main()
{
test_substrand();
return 0;
}
TEST.CPP
#ifndef _STRAND_H_
#define _STRAND_H_
#include <cstdlib>
#include <string>
#include <string>
class Strand
{
public:
Strand();
Strand(const char *src);
~Strand();
Strand & operator=(const Strand &rhs);
const char *getStrand() const;
void setStrand(const char *strand);
Strand & operator+=(const Strand &rhs);
Strand operator+(const Strand &rhs) const;
Strand substrand(size_t start, size_t end) const;
protected:
char *mStrand;
};
#endif /* _STRAND_H_ */
/* Local Variables: */
/* mode:c++ */
/* End: */
Strand.h
#include "Strand.h"
#include <cstring>
#include <string>
// default constructor
Strand::Strand()
: mStrand(0)
{
}
// copy constructor
Strand::Strand(const char *src)
: mStrand(0)
{
*this = src;
}
// destructor
Strand::~Strand()
{
if(mStrand != 0)
{
delete [] mStrand;
mStrand = 0;
}
}
// assignment operator
Strand &Strand::operator=(const Strand &rhs)
{
setStrand(rhs.mStrand);
return *this;
}
// Get Strand
const char *Strand::getStrand() const
{
return mStrand;
}
// Set Strand
void Strand::setStrand(const char *strand)
{
if(mStrand)
{
delete [] mStrand;
mStrand = 0;
}
if(strand)
{
mStrand = new char [std::strlen(strand) + 1];
std::strcpy(mStrand, strand);
}
}
size_t Strand::size() const
{
return mBases;
}
// operator +=
Strand &Strand::operator+=(const Strand &rhs)
{
char *new_strand;
new_strand = new char [std::strlen(mStrand) + std::strlen(rhs.mStrand) + 1];
strcat(strcpy(new_strand, mStrand), rhs.mStrand);
setStrand(new_strand);
delete [] new_strand;
return *this;
}
// operator +
Strand Strand::operator+(const Strand &rhs) const
{
Strand value;
value = *this;
value += rhs;
return value;
}
// substrand "python" slice
Strand Strand::substrand(size_t start, size_t end) const
{
size_t k, i, size;
size = end - start;
char *new_strand;
new_strand = new char [size +1];
for (k = start, i = 0; k < end; k++, i++)
{
new_strand[i] = mStrand[k];
}
new_strand[k] = 0;
Strand setStrand(new_strand);
delete [] new_strand;
new_strand = 0;
return setStrand;
}
Strand.cpp
答案 0 :(得分:0)
固定代码中存在两个问题。
首先,测试函数忽略了substrand的结果,并始终打印原始的strand。这是固定代码:
void test_substrand()
{
Strand s1;
s1.setStrand("Hellow World");
Strand s2 = s1.substrand(1, 4);
std::cout << "Substrand: " << s2.getStrand() << std::endl;
}
其次,你的拷贝构造函数有:
*this = src;
它导致对此复制构造函数的无限递归调用,并且应用程序在堆栈耗尽时崩溃。这是一个修复
Strand::Strand(const char *src)
: mStrand(0)
{
mStrand = new char [std::strlen(src)+ 1];
std::strcpy(mStrand, src);
}