2012-09-18 35 views
-1

我有一個任務,我試圖讀取一個文件並計算它有多少行。我試圖使用類,但我不知道如何在類中聲明一個字符串成員。以下是代碼:如何在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()的類中。如何申報?如何解決它?有任何想法嗎 ?有什麼建議麼?

回答

2

它應該是

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

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

+1

哦,我的上帝!謝謝你的答覆=)謝謝你!非常感謝 !!有效 !! = d –

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

C++是大小寫敏感的...並std::string開始以小寫s。你已經在這個文件中的許多其他位置正確地聲明瞭它,不知道爲什麼這個將你掛起。

另外,函數定義後不需要分號。

+0

感謝您的回覆,由於某種原因,這是固定的,但現在編譯器說:「在成員函數'無效Lista :: Print()': 錯誤:'類Nodo'沒有成員名爲'數據' 錯誤:'級Nodo'沒有任何成員'數據'「 –