-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 回答