我收到阻止我运行程序的警告,但不确定如何解决
I'm getting a warning that is preventing me from running my program and I'm not sure how to fix it
该代码应该从文件中读取输入并将该信息放入哈希表中,如果出现任何碰撞,它也应该处理这些信息。我不确定如何修复我使用该行 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 agen";
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行中所做的那样,不允许您在结构声明中初始化非静态数据成员。相反,您可以:
- 定义
Hash
struct的构造函数,并在C-TOR中初始化count
成员。 - 在实例化
Hash
结构对象后,每次初始化计数成员。我不建议使用此解决方案,尤其是如果您坚持在全球范围内宣布Hash H[37]
数组(我也不建议,这是另一个问题。(。
从点1的意义上。上面,Hash
类声明看起来像这样:
struct Hash{
Hash() :
head(NULL),
count(0)
{}
struct Node * head ;
int count;
};
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- IpOpt拒绝解决不受约束的问题
- 不确定如何装饰我的C++库代码以在 C 中使用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 为什么以下代码是不确定的?
- 如何解决不明确的运算符过载问题?
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 我不确定为什么代码没有运行,它在编译时抛出错误,说它无法找到 PrintArray()
- 我收到阻止我运行程序的警告,但不确定如何解决
- 未解决的内容包括在Eclipse CDT中,其中路径名不确定
- 我只需要一些调试帮助(C++)。我遇到了多个不确定如何解决的问题
- 我不断收到错误"变量'b'未初始化正在使用,我不确定如何解决它
- 不断收到有关类型匹配的错误.不确定这意味着什么或如何解决它
- 什么是C++中的不确定行为?它与未定义的行为有何不同
- 也许快速错误但我不确定如何解决
- 错误LNK2019 -不确定如何修复(1未解决的外部)
- C++程序-不确定是否,我提供了正确的解决方案