2017-09-05 55 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中创建不同的字符串,以便我可以通过已声明的规则将东西应用于它们? –