2017-09-05 45 views
-3

這是我第一次使用struct。我需要在main中初始化secure :: secure(),但不知道我應該放在那裏。它期望一個默認的構造函數,它會跳過我所做的所有聲明。struct delcaration調用錯誤的構造函數

我該怎麼辦?

此外,我的代碼可能還有其他問題,我不會感到驚訝。現在,請只解釋我如何引用特定聲明而不是默認聲明。

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <string> 
#include <cctype> 
#include <algorithm> 

using namespace std; 

struct secure 
{ 
    string surname; 
    string name; 
    string age; 
    string placeofbirth; 
    string tel; 
    secure(const string& s, const string& n, string a, const string& p, const string& t); 
}; 

secure::secure(const string& s, const string& n, string a, const string& p, const string& t) 
:surname{s}, 
name{n}, 
age{a}, 
placeofbirth{p}, 
tel{t} 
{ 
    if(!(find_if(s.cbegin(), s.cend(), [](const char z) {return !isalpha(z) && z != '-';}) == s.cend())) 
     cout<<"You may not use characters that are not letters nor -."<<endl; 
    if(!(find_if(n.cbegin(), n.cend(), [](const char x) {return !isalpha(x);}) == n.cend())) 
     cout<<"You are allowed to only use letters."<<endl; 
    if(a.find_first_not_of("")) 
     cout<<"Age must be a positive integer."<<endl; 
    if((find_if(p.cbegin(), p.cend(), [](const char c) {return !isspace(c);}) == p.cend())) 
     cout<<"Spaces are not allowed."<<endl; 
    if(t.find_first_not_of("1234567890+-")) 
     cout<<"Telephone numbers consists only of digits, + and -."<<endl; 
} 

void perfection(struct secure &secure) 
{ 
    quest: 
    ofstream creation; 
    ifstream created; 

    cout<<"How many students' info will be entered?: "; 
    string uservoice; 
    getline(cin, uservoice); 

    double enterholder; 
    int sum = 0; 
    double average = 0; 
    int intage; 

    if(!uservoice.find_first_not_of("1234567890-")) 
    { 
     cout<<"You shall enter an integer."<<endl; 
     goto quest; 
    } 
     int replacement = 0; 
     replacement=stoi(uservoice); 

    creation.open("knight.txt"); 
    if(replacement>0) 
    { 
     enterholder = replacement; 
     cout<<"Enter data "<<uservoice<<" times."<<endl; 
     do 
     { 
      cout<<"Enter data as follows: "<<endl; 
      cout<<"Enter surname: "; 
      getline(cin, secure.surname); 
      cout<<"Enter name: "; 
      getline(cin, secure.name); 
      cout<<"Enter age: "; 
      getline(cin, secure.age); 
      intage = stoi(secure.age); 
      cout<<"Enter place of birth: "; 
      getline(cin, secure.placeofbirth); 
      cout<<"Enter telephone number: "; 
      getline(cin, secure.tel); 
      creation << secure.surname << '\t' << secure.name << '\t' << intage << '\t' << secure.placeofbirth << '\t' << secure.tel <<endl; 
      replacement--; 
     }while(replacement != 0); 
    } 
    else 
    { 
     cout<<"You shall enter a number bigger than 0."<<endl; 
     goto quest; 
    } 
    creation.close(); 

    bool ensuretodisplay; 
    stringstream hasstuff; 
    stringstream abegins; 

    string s, n, p, t; 
    int a; 

    created.open("knight.txt"); 
    while(created >> s >> n >> a >> p >> t) 
    { 
     ensuretodisplay = true; 
     cout << s << '\t' << n << '\t' << a << '\t' << p << '\t' << t <<endl; 
     hasstuff << s << t << endl; 
     sum+=a; 
     average = sum/enterholder; 
     if(s.find("A") == 0) 
     { 
      abegins << s << n << endl; 
     } 
    } 
    if(ensuretodisplay) 
    { 
     cout<<"Surnames and telephone numbers of all the students:\n"<<hasstuff.str()<<endl; 
     cout<<"An average age of all the students is: "<< average<<endl; 
     cout<<"Surnames and names of those whose surname begins with A:\n"<<abegins.str(); 
    } 
    created.close(); 
} 
int main() 
{ 
    struct secure &exotic; 
    perfection(exotic); 
    cin.get(); 
    return 0; 
} 
+1

*但不知道我應該放那裏* 「這個對象是這樣構造的」。所以沒有人真的可以告訴你「該放什麼東西」 - 這是你的設計,你做出決定。 – PaulMcKenzie

+0

問題是如何告訴main我將通過secure :: secure()中的語句在void中引用struct。這是我的設計,我不知道如何參考。 –

+0

順便說一句,在C++中,當聲明變量時不需要指定'struct'或'class'。 –

回答

0

您創建了一個構造函數,需要5個輸入參數(並且您沒有在代碼中的任何位置使用它)。當您在main()中創建結構實例時,它會嘗試調用不帶參數的構造函數,並且會失敗,因爲如果您沒有默認構造函數,則說明它必須以您的方式創建。只需創建不帶參數的另一個構造函數,它就可以工作。

struct secure 
{ 
    string surname; 
    string name; 
    string age; 
    string placeofbirth; 
    string tel; 
    secure(const string& s, const string& n, string a, const string& p, const string& t); 
    secure(); //Defaul constr with no args 
}; 

secure::secure() 
{ 
    //you can do something here if you want 
} 

如果你想使用你的5 arg。構造函數,你必須這樣聲明:

secure exotic(arg1, arg2, arg3, arg4, arg5); 
+0

這是怎麼回事我不使用它在void中聲明getline(cin,secure.surname)等等? –

+0

構造函數僅在創建對象時調用。在你的void函數中,你直接訪問和覆蓋你的結構的成員變量。 – zabusz

+0

有道理。這是否意味着我應該在void中創建不同的字符串,以便我可以通過已聲明的規則將東西應用於它們? –