2017-09-05 2 views
-3

구조체를 처음 사용했습니다. 메인의 내부에 secure :: secure를 초기화 할 필요가 있지만 무엇을 넣어야할지 모른다. 그것은 내가 만든 모든 선언을 건너 뛰는 기본 생성자를 기대합니다.구조체 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

질문은 secure :: secure()에있는 문에 의해 void에서 struct를 참조 할 것이라고 main에게 어떻게 알리는 지에 대한 것입니다. 참조 할 방법을 모르는 것은 제 디자인입니다. –

+0

BTW, 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 개 인수를 사용하려는 경우 당신이 이것을 다음과 같이 선언해야만합니다 :

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

getline (cin, secure.surname) 등을 무효로하는 동안 어떻게 사용합니까? –

+0

생성자는 객체를 만들 때만 호출됩니다. void 함수에서는 구조체의 멤버 변수에 직접 액세스하여 덮어 씁니다. – zabusz

+0

의미가 있습니다. 그것은 내가 선언 한 규칙에 따라 그들에게 물건을 적용 할 수 있도록 다른 문자열을 무효화해야한다는 것을 의미합니까? –

관련 문제