Joe Li
Joe Li

Reputation: 21

need help on operator << overloading functions

The error I'm getting right now is:

multiple definition of operator<<(std::ostream&, SingleSequence& s)

the error location is at one of the overload operator function:

std::ostream& operator<<(std::ostream& os, const SingleSequence& s)
{
    os << s.name << " " << s.seq << " " << s.length << " " << s.gccontent << " " << s.type;
    return os;
}

This is the driver part:

 #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;
 }

This is the 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_ */
 -------------------------------------------------------------

This is the main cpp file

#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;

  }

Upvotes: 0

Views: 62

Answers (1)

Kenneth Bastian
Kenneth Bastian

Reputation: 883

You have two definition of the same operator << function in .h and .cpp. Hence, multi-definition error.

Keep the declaration in .h. Makes sure it is outside of the class

std::ostream& operator<<(std::ostream& os, const SingleSequence& s);
std::ostream& operator<<(std::ostream& os, const Sequences& seqs);

And write the definition in you .cpp file

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

Upvotes: 1

Related Questions