簡體   English   中英

如何在C ++中的類中聲明字符串成員

[英]How to declare a string member in a class in c++

我有一個作業,我試圖讀取一個文件並計算它有多少行。 我正在嘗試使用類,但是我不知道如何在類中聲明字符串成員。 這是代碼:

#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()中的類中。 怎么申報呢? 如何解決? 有任何想法嗎 ? 有什么建議么?

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

C ++區分大小寫...且std::string以小寫s開頭。 您已經在此文件的許多其他位置正確聲明了該文件,不確定該為什么使您掛斷電話。

另外,在函數定義之后不需要分號。

它應該是

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

C ++是區分大小寫的語言。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM