구조체를 처음 사용했습니다. 메인의 내부에 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;
}
* 그러나 무엇을 넣어야할지 모릅니다 * - 기본 생성자가없는 인수로 생성자를 만들면 계약서를 작성했습니다 "이 물건은 이쪽으로 지어졌습니다". 따라서 아무도 실제로 "무엇을 놓을 지"를 말할 수는 없습니다. 그것은 당신의 설계이고, 당신은 결정을 내립니다. – PaulMcKenzie
질문은 secure :: secure()에있는 문에 의해 void에서 struct를 참조 할 것이라고 main에게 어떻게 알리는 지에 대한 것입니다. 참조 할 방법을 모르는 것은 제 디자인입니다. –
BTW, C++에서는 변수를 선언 할 때'struct' 또는'class'를 지정할 필요가 없습니다. –