gpt4 book ai didi

c++ - 我收到一条警告,阻止我运行我的程序,我不确定如何修复它

转载 作者:行者123 更新时间:2023-11-28 04:47:13 25 4
gpt4 key购买 nike

该代码应该从文件中读取输入并将该信息放入哈希表中,如果出现任何冲突,它也应该处理这些冲突。我不确定如何解决我在行 int count = 0 中遇到的错误,这是错误警告:

non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

..

#include <iostream>
#include<stdlib.h>
#include<string.h>
#include<map>
#include <ctime>
#include<bits/stdc++.h>
using namespace std ;

struct record{
char last_name[15] ;
char first_name[15] ;
unsigned int account_num ;
unsigned int month ;
unsigned int day ;
unsigned int year ;
float annual_salary ;
char dept_code ;
char phone_num[12] ;
};

struct Node{
record p ;
struct Node * next ;
};
struct Hash{
struct Node * head ;
int count = 0 ;
};
Hash H[37] ;
/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(struct Node** head_ref,record new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

struct Node *last = *head_ref; /* used in step 5*/

/* 2. put in the data */
new_node->p = new_data;

/* 3. This new node is going to be the last node, so make next of
it as NULL*/
new_node->next = NULL;

/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}

/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;

/* 6. Change the next of last node */
last->next = new_node;
return;
}

void insert(record r)
{
int x = r.account_num %37 ;
append(&(H[x].head),r) ;
}
map<int,string> m ;
void printdata()
{
time_t now = time(0);
char* dt = ctime(&now) ;
int z = strlen(dt) ;
int yea = 0 , temp = 1;
z--;
while(dt[z]!=' ')
{
yea += temp*(dt[z]-'0');
temp = temp*10 ;
z--;
}
int mont ;
char ch[3] ;
ch[0] = dt[4] ;
ch[1] = dt[5] ;
ch[2] = dt[6] ;
for(int i=1;i<=12;i++)
{
if(m[i]==ch)
{
mont = i ;
break ;
}
}
m[1] = "Jan" ;
m[2] = "Feb" ;
m[3] = "Mar" ;
m[4] = "Apr" ;
m[5] = "May" ;
m[6] = "Jun" ;
m[7] = "Jul" ;
m[8] = "Aug" ;
m[9] = "Sep" ;
m[10] = "Oct" ;
m[11] = "Nov" ;
m[12] = "Dec" ;
struct Node * q ;
cout << "LAST Name Acct Number date of birth annual salary department code age\n";
for(int i=0;i<=36;i++)
{
q = (H[i].head);
int cnt = 0 ;
while(q!=NULL)
{
cnt++;
q = q->next ;
}
temp = q->p.year - yea + (q->p.month - mont>=6 ? 1:0) ;
q = H[i].head ;
cout << q->p.last_name <<" "<<q->p.account_num ;
if(cnt>1)cout<<"*";
cout <<" "<<m[q->p.month]<<". "<<q->p.day<<","<<q->p.year<<" "<<q->p.annual_salary<<" "<<q->p.dept_code<<" "<<temp<<"\n" ;;
if(q==NULL)continue ;
q = q->next ;
while(q!=NULL)
{
temp = q->p.year - yea + (q->p.month - mont>=6 ? 1:0) ;
cout << q->p.last_name <<" "<<q->p.account_num ;
cout <<" "<<m[q->p.month]<<". "<<q->p.day<<","<<q->p.year<<" "<<q->p.annual_salary<<" "<<q->p.dept_code<<" "<<temp<<"\n" ;;
q = q->next ;
}
}
return ;
}
int main()
{

record r ;
freopen("data.txt","r",stdin);
while(cin >> r.last_name)
{
cin >> r.first_name >> r.account_num >> r.month >> r.day >> r.year >> r.annual_salary >> r.dept_code ;
if(cin >> r.phone_num) ;
insert(r) ;
}

return 0 ;
}

最佳答案

错误消息是不言自明的。不允许像在代码的第 27 行中那样在结构声明中初始化非静态数据成员。相反,您可以,例如:

  1. 定义Hash 结构的构造函数并初始化c-tor 中的count 成员。
  2. 每次实例化Hash 结构的对象后初始化count 成员。我不推荐这个解决方案,特别是如果你坚持在全局范围内声明 Hash H[37] 数组(我也不推荐,但这完全是另一个问题。)。

在上面第 1 点的意义上,Hash 类声明可能如下所示:

struct Hash{
Hash() :
head(NULL),
count(0)
{}

struct Node * head ;
int count;
};

关于c++ - 我收到一条警告,阻止我运行我的程序,我不确定如何修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49038377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com