-1

割り当てがあり、ファイルを読み取って、そのファイルの行数を数えようとしています。クラスを使おうとしていますが、クラスで文字列メンバーを宣言する方法がわかりません。コードは次のとおりです。

#include<iostream>
#include<cmath>
#include<fstream>
#include<istream>
#include<string>
#include<iomanip>

using namespace std;

// Clase Nodo 
class Nodo {

    Nodo* next;

public:
    Nodo() {};
    string strLinea;
string strAux1; // = "//p.";  
string strAux2; // = "//i.";  
string strAux3; // = "//d.";  
string strAux4; // = "//m.";

void SetVariables(string data)
{
       strLinea = data;
    }

//void SetData(float EPS, float DH)
void SetData(string S)
{ 
    strLinea = S;
};

void SetNext(Nodo* aNext) 
{ 
    next = aNext;
};

String Data() 
{ 
    return(strLinea);
};

Nodo* Next() 
{ 
    return next; 
};
};


class Lista {
    Nodo *head;

public:
    Lista() { head = NULL; };
    void Print();
    void Append(string strLinea);
    void Delete(string strLinea);
};



void Lista::Print() {

// ... Apuntador temporal ...
Nodo *tmp = head;

// ... No hay Nodos ...
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}

// ... Existe un Nodo in the Lista ...
if ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << " --> ";
cout << "NULL" << endl;
}
else {
// ... Recorre la lista y la imprime ...
do {
    cout << tmp->Data();
    cout << " --> ";
    tmp = tmp->Next();
}
while ( tmp != NULL );

cout << "NULL" << endl;
}
}


 void Lista::Append(string strLinea){
 // ... Aquí crea a Nodo nuevo ...
 Nodo* newNodo = new Nodo();

 newNodo->SetData(strLinea);
 newNodo->SetNext(NULL);

 // ... Crea un apuntador temporal ...
 Nodo *tmp = head;

 if ( tmp != NULL ) {
 // ... El Nodo está en la Lista ...
 // ... Recorre la Lista ...
 while ( tmp->Next() != NULL ) {
     tmp = tmp->Next();
 }
 // ... El último Nodo de la lista ...
 tmp->SetNext(newNodo);
 }
 else {
 // ... Nuevo Nodo de la lista ...
 head = newNodo;
 }
}


void Lista::Delete(string strLinea){
    // ... Crea un Nodo temporal ...
    Nodo *tmp = head;

   // ... No hay Nodos ...
   if ( tmp == NULL )
      return;

   // ... Último Nodo de la Lista ...
   if ( tmp->Next() == NULL ) {
        delete tmp;
        head = NULL;
   }
   else {
    // ... Recorre los nodos ...
    Nodo *prev;
    do {


     if(tmp->Data() == strLinea) break;

         prev = tmp;
         tmp = tmp->Next();
    } while ( tmp != NULL );

    // ... Ajusta los Nodos ...
    prev->SetNext(tmp->Next());

    // ... Borra el Nodo actual ...
   delete tmp;
   }
  }




 int main(int argc, char* argv[])
 {
        string L;
        int intLineCounter = 0;

        Lista lista;



        ifstream infile;
        infile.open(argv[1], ios::in);
        if(argc != 2)
        {
             cout << "ERROR.\n";
             return 1;
        }
       if(infile.fail())
       {
            cout << "ERROR\n";
            return 1;
       }
       else
       {
            while(!infile.eof())
            {
                   getline(infile,L);
                   intLineCounter++;
                   cout<< L + "\n";
                   lista.Append(L);
            }
            infile.close();
            cout << "Total lines: " << intLineCounter << endl;
           return 0;
      }
   }

問題は、宣言String Data()のクラスにあります。それを宣言する方法は?それを修正する方法は?何か案は ?助言がありますか?

4

2 に答える 2

3
String Data() 
{ 
    return(strLinea);
};

C++では大文字と小文字が区別されます...std::string小文字で始まりますs。このファイルの他の多くの場所ですでに正しく宣言されていますが、なぜこれがハングアップしているのかわかりません。

また、関数定義の後にセミコロンは必要ありません。

于 2012-09-18T04:33:40.547 に答える
2

そのはず

string Data() 
{ 
    return(strLinea);
};

C++は大文字と小文字を区別する言語です。

于 2012-09-18T04:40:49.277 に答える