2014-02-22 69 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

我編輯了我的答案看到編輯。 –