這是我第一次使用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;
}
*但不知道我應該放那裏* 「這個對象是這樣構造的」。所以沒有人真的可以告訴你「該放什麼東西」 - 這是你的設計,你做出決定。 – PaulMcKenzie
問題是如何告訴main我將通過secure :: secure()中的語句在void中引用struct。這是我的設計,我不知道如何參考。 –
順便說一句,在C++中,當聲明變量時不需要指定'struct'或'class'。 –