首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我收到了一个警告,阻止我运行我的程序,我不知道如何修复它

我收到了一个警告,阻止我运行我的程序,我不知道如何修复它
EN

Stack Overflow用户
提问于 2018-02-28 20:55:17
回答 1查看 55关注 0票数 0

代码应该读取文件中的输入,并将该信息放入散列表中,如果出现任何冲突,它也应该处理这些信息。我不知道如何用行int count = 0,来修复我得到的错误--这是错误警告:

代码语言:javascript
复制
non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]

。。

代码语言:javascript
复制
#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 ;
}
EN

回答 1

Stack Overflow用户

发布于 2018-03-01 21:15:14

错误信息是不言而喻的.不允许在struct声明中初始化非静态数据成员,正如代码第27行所做的那样。相反,您可以,例如:

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

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

代码语言:javascript
复制
struct Hash{
    Hash() :
        head(NULL),
        count(0)
    {}

    struct Node * head ;
    int count;
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49038377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档