• Viernes 3 de Mayo de 2024, 06:43

Autor Tema:  conversion vector string  (Leído 866 veces)

meana

  • Miembro activo
  • **
  • Mensajes: 36
    • Ver Perfil
conversion vector string
« en: Viernes 11 de Diciembre de 2009, 11:05 »
0
Código: C++
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <stdlib.h>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. vector <string> TC (int &m){
  10.        vector <string> TC(10);
  11.        TC[2]="abc";
  12.        TC[3]="def";
  13.        TC[4]="ghi";
  14.        TC[5]="jkl";
  15.        TC[6]="mno";
  16.        TC[7]="pqrs";
  17.        TC[8]="tuv";
  18.        TC[9]="wxyz";
  19.        return TC[m];
  20.        }
  21.  
  22. void borrar(vector <string> &CPF,int k){
  23.   for (int i=k;i<CPF.size()-1;i++)
  24.     CPF[i]=CPF[i+1];
  25.   CPF.pop_back();
  26. }
  27.  
  28. vector <string> recursion(vector <int> &CD, vector <string> &CP,int j){
  29.  
  30.   vector <string> CPF;
  31.  
  32.   if(j==1){                     //CASO TRIVIAL
  33.     for (int i=0;i<CP.size();i++){      
  34.       string aux=TC(CD[i]);
  35.       for (int k=0;k<aux.size();k++)
  36.           if (CP[i][0]==aux[k]){
  37.              CPF.push_back(CP[i]);          
  38.              break;            
  39.           }    
  40.     }
  41.     return CPF;
  42.   }
  43.  
  44.   else {                        //CASO NO TRIVIAL
  45.     CPF=recursion (CD,CP,j-1);              
  46.     for (int i=0;i<CPF.size();i++){    
  47.       string aux2=TC(CD[i]);        
  48.       for (int k=0;k<aux2.size();k++)      
  49.           if(CPF[i][j-1]==aux2[k])          
  50.           break;
  51.           else {
  52.                borrar(CPF,i);
  53.                break;
  54.           }
  55.     }
  56.     return CPF;
  57.   }
  58. }
  59.  
  60. int main(int argc,char *argv[]){
  61.  
  62.   ifstream f1(argv[1]);
  63.   int n;
  64.   f1 >> n;
  65.  
  66.   vector <int> CD(n);
  67.   for (int i=0;i<n;i++) f1 >> CD[i];
  68.  
  69.   vector <string> CP;
  70.   while(!f1.eof()){
  71.     string s;
  72.     f1 >> s;
  73.     CP.push_back(s);
  74.   }
  75.  
  76.   vector <string> CPF;
  77.   CPF=recursion(CD,CP,n);
  78.  
  79.   ofstream f2(argv[2]);
  80.   for (int i=0;i<CPF.size();i++)
  81.     f2 << CPF[i] << endl;
  82.  
  83.   return 0;
  84. }
  85.  

conversion from `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to non-scalar type `std::vector<std::string, std::allocator<std::string> >' requested ---> en la primera funcion, la funcion TC me da este error al hacer el return (linea 19)

conversion from `std::vector<std::string, std::allocator<std::string> >' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested ---> dentro de la funcion recursion, en caso trivial y no trivial respectivamnt al asignar al string aux=TC(CD) (lineas 34 y 47)

ProfesorX

  • Moderador
  • ******
  • Mensajes: 796
  • Nacionalidad: mx
    • Ver Perfil
Re: conversion vector string
« Respuesta #1 en: Viernes 11 de Diciembre de 2009, 18:55 »
0
El error esta en esta funcion:

Código: C++
  1.  
  2. vector <string> TC (int &m){
  3.        vector <string> TC(10);
  4.        TC[2]="abc";
  5.        TC[3]="def";
  6.        TC[4]="ghi";
  7.        TC[5]="jkl";
  8.        TC[6]="mno";
  9.        TC[7]="pqrs";
  10.        TC[8]="tuv";
  11.        TC[9]="wxyz";
  12.        return TC[m];
  13.        }
  14.  
  15.  

Tratas de regresar un string, pero tu funcion la tienes definida como que regresa un vector<string>, debes eliminar la parte de vector<>, quedando de la siguiente manera:

Código: C++
  1.  
  2. string TC (int &m){
  3.     vector <string> TC(10);
  4.     TC[2]="abc";
  5.     TC[3]="def";
  6.     TC[4]="ghi";
  7.     TC[5]="jkl";
  8.     TC[6]="mno";
  9.     TC[7]="pqrs";
  10.     TC[8]="tuv";
  11.     TC[9]="wxyz";
  12.     return TC[m];
  13. }
  14.  
  15.  

Saludos :)

NOTA:
==================================================================
Este foro es para ayudar, aprender, compartir... usenlo para eso,
NO SE RESUELVEN DUDAS POR MENSAJE PRIVADO Y MENOS POR CORREO
==================================================================