Domanda

Ho il seguente frammento:

string base= tag1[j];

Questo dà l'errore di conversione non valido.

Cosa c'è che non va nel mio codice qui sotto? Come posso superarlo.

Il codice completo è qui:

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <time.h>
using namespace std;


int main  ( int arg_count, char *arg_vec[] ) {
    if (arg_count < 3 ) {
        cerr << "expected one argument" << endl;
        return EXIT_FAILURE;
    }

    // Initialize Random Seed
    srand (time(NULL));

    string line;
    string tag1     = arg_vec[1];
    string tag2     = arg_vec[2];

    double SubsRate = 0.003;
    double nofTag   = static_cast<double>(atoi(arg_vec[3])); 

    vector <string> DNA;
      DNA.push_back("A");
      DNA.push_back("C");
      DNA.push_back("G");
      DNA.push_back("T");


      for (unsigned i=0; i < nofTag ; i++) {

          int toSub = rand() % 1000 + 1;

          if (toSub <= (SubsRate * 1000)) {
              // Mutate
              cout << toSub << " Sub" << endl;

              int mutateNo = 0;
              for (int j=0; j < tag1.size(); j++) {

                  mutateNo++;


                  string base = tag1[j]; // This fail

                  int dnaNo = rand() % 4;

                  if (mutateNo <= 3) {
                     // Mutation happen at most at 3 position
                        base = DNA[dnaNo];
                  }

                  cout << tag1[j] << " " << dnaNo << " " << base  <<  endl;
                  //cout << base;

              }
               cout << endl;

          }
          else {
              // Don't mutate
              //cout << tag1 << endl;
          }

      }
    return 0;
}

Perché ricevo una conversione non valida da char a const char* quando eseguo il ciclo su una stringa?

È stato utile?

Soluzione

std::string operator [] restituisce un singolo carattere. la stringa non può essere istanziata con un solo carattere.

Usa:

string base = string( 1, tag1[j] ) invece

Altri suggerimenti

Cambialo in

char base = tag1[j];

string tag1 = arg_vec [1];

tag1 è una stringa letterale.

string base = tag1[j]; è inizializzato con char anziché char *.

Prova, char base = tag1[j];

Non esiste un costruttore per string che richiede solo un char (che è ciò che tag1[j] è). Hai un paio di opzioni:

string base;  // construct a default string
base  = tag1[j]; // set it to a char (there is an 
                 //   assignment from char to string, 
                 //   even if there's no constructor

o

string base( 1, tag1[j]);  // create a string with a single char 

O come Josh citato , puoi definire base come DNA poiché non esegui comunque alcuna operazione su di essa. Se decidi di farlo, devi modificare vector<char> in <=> (e modificare l'inizializzazione di <=> in utilizzare i caratteri anziché le stringhe).

Un problema è che il messaggio di errore dice che il programma prevede un argomento quando ne richiede effettivamente due. Probabilmente dovresti seguire le convenzioni Unix e mostrare anche l'uso richiesto (o invece):

if (arg_count != 3) {
    cerr << "Usage: " << arg_vec[0] << " tag1 tag2";
    return EXIT_FAILURE;
}

I nomi 'argc' e 'argv' sono molto convenzionali (e l'unica alternativa importante che ho visto è 'ac' e 'av'). Potrebbe valere la pena attenersi a questo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top