我现在得到的错误是:
的多重定义
operator<<(std::ostream&, SingleSequence& s)
错误位置位于过载运算符函数之一:
std::ostream& operator<<(std::ostream& os, const SingleSequence& s)
{
os << s.name << " " << s.seq << " " << s.length << " " << s.gccontent << " " << s.type;
return os;
}
这是驱动程序部分:
#include"Sequences.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
cout << "Assignment #1" << endl;
Sequences mysequences;
cout << mysequences;
cout << "Sorted by name" << endl;
mysequences.sortByName();
cout << mysequences;
cout << "Sorted by length" << endl;
mysequences.sortByLength();
cout << mysequences;
cout << "... done!" << endl;
}
这是Sequences.h
#ifndef SEQUENCES_H_
#define SEQUENCES_H_
#include<string.h>
#include<strings.h>
#include<string>
#include<iostream>
using namespace std;
enum sequenceType { dna, rna, protein };
struct SingleSequence{
std::string name;
std::string seq;
int length;
double gccontent;
sequenceType type;
};
class Sequences {
public:
Sequences();
virtual ~Sequences();
int getListSize() const{return datasize;}
const SingleSequence& get( int i) const{
if (i>=0 && i < datasize)
return data[i];
throw OUT_OF_BOUNDS;;//{ if (i>=0 && i < datasize)
}
// return data[i];
// throw OUT_OF_BOUNDS;} // C++ has exceptions - you can even throw ints;
void sortByName();
void sortByLength();
friend std::ostream& operator<<(std::ostream& os, const SingleSequence& s) ;
friend std::ostream& operator<<(std::ostream& os, const Sequences& seqs) ;
int datasize;
private:
/*
* Remember to keep all data members private
*/
static const int MAX_LIST_SIZE = 20;
SingleSequence data[MAX_LIST_SIZE];
static const int OUT_OF_BOUNDS = -1;
};
std::ostream& operator<<(std::ostream& os, const SingleSequence& s)
{ os << s.name << " " << s.seq << " "
<< s.length << " " << s.gccontent << " "<<s.type;
return os;
}
#endif /* SEQUENCES_H_ */
-------------------------------------------------------------
这是主要的cpp文件
#include "Sequences.h"
#include <iostream>
using namespace std;
Sequences::Sequences() {
data[0] = { "KCNH2 Primer Pair 1 Forward", "CCAACTGGTGGACCGTCATT", 20, 55.0, dna };
data[1] = { "KCNH2 Primer Pair 1 Reverse", "GACAGCCAGGTGAACATCCA", 20, 55.0, dna };
data[2] = { "KCNH2 Primer Pair 2 Forward", "TGGATGTTCACCTGGCTGTC", 20, 55.0, dna };
data[3] = { "KCNH2 Primer Pair 2 Reverse", "CCACGGAACCTCTGGCAATA", 20, 55.0, dna };
data[4] = { "KCNH2 Primer Pair 3 Forward", "GAACGGAAGTGTGCCAACTG", 20, 55.0, dna };
data[5] = { "KCNH2 Primer Pair 3 Reverse", "ACAGCCAGGTGAACATCCAG", 20, 55.0, dna };
data[6] = { "KCNH2 Primer Pair 4 Forward", "CTGGATGTTCACCTGGCTGT", 20, 55.0, dna };
data[7] = { "KCNH2 Primer Pair 4 Reverse", "ATTTCCACGGAACCTCTGGC", 20, 55.0, dna };
data[8] = { "KCNH2 Primer Pair 5 Forward", "TGAAAACCGCTCGTCTGC", 18, 55.6, dna };
data[9] = { "KCNH2 Primer Pair 5 Reverse", "GGTGGAGCATGTGTTGTT", 18, 50.0, dna };
datasize = 10;
}
void Sequences::sortByName(){
for(int i = 0; i < 10; i++){
//int flag = 1;
SingleSequence temp;
for(int j = 0; j < 9; j++){
if (data[j].name.compare(data[j+1].name) > 0){
temp = data[j+1];
data[j+1] = data[j];
data[j] = temp;
}
}
}
}
void Sequences::sortByLength(){
for(int a = 0; a < 10; a++){
SingleSequence temp1;
for(int b = 0; b < 9; b++){
if (data[b].length > data[b+1].length){
temp1 = data[b+1];
data[b+1] = data[b];
data[b] = temp1;
}
}
}
}
std::ostream& operator<<(std::ostream& os, const Sequences& seqs)
{os << " Sequences object " << endl;
for (int i=0; i < seqs.getListSize(); i++ )
os << " " << (i+1) <<": " << seqs.get( i ) << endl;
return os;
}
答案 0 :(得分:1)
在.h和.cpp中有两个相同的运算符<<
函数的定义。因此,多重定义错误。
将声明保留在.h。确保它在课堂之外
std::ostream& operator<<(std::ostream& os, const SingleSequence& s);
std::ostream& operator<<(std::ostream& os, const Sequences& seqs);
在你的.cpp文件中写下定义
std::ostream& operator<<(std::ostream& os, const Sequences& seqs)
{
os << " Sequences object " << endl;
for (int i = 0; i < seqs.getListSize(); i++)
os << " " << (i + 1) << ": " << seqs.get(i) << endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const SingleSequence& s)
{
os << s.name << " " << s.seq << " "
<< s.length << " " << s.gccontent << " " << s.type;
return os;
}