2014-02-22 55 views
0

我得到现在的错误是:需要运营商帮助<<重载函数

多重定义的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

你有你的功能被链接两个副本。包含头文件和包含头文件的主文件中的其他文件在实现中。 – chris

回答

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; 
} 
+0

这两个定义是为了不同的目的。一个std :: ostream&operator <<(std :: ostream&os,const SingleSequence & s);用于结构SingaleSequence。另一个用于Sequence对象。你的意思是我应该只保留一个吗? – user3339506

+0

我编辑了我的答案看到编辑。 –