首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:无法将'record*‘转换为'record**’

错误:无法将'record*‘转换为'record**’
EN

Stack Overflow用户
提问于 2014-02-13 14:26:54
回答 2查看 137关注 0票数 0

我不明白为什么当我编译我的代码时会出现这个错误。我相信我做的每件事都和以前一样,但这一次不起作用。

代码语言:javascript
复制
stack.cpp:30: error: cannot convert 'record*' to 'record**' for argument '2' to 'void filename(char*, record**)'

在编译时会有更多的错误,但我会把它们找出来。(我还必须手动添加每行表示“代码”的4个空格,所以如果其他人可以告诉我如何自动完成此操作!那太好了!

代码语言:javascript
复制
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <strings.h>

using namespace std;

struct record
{
    char first [20];
    char mid   [1];
    char last  [20];
    int  ssn;
};

void filename (char ifname [], struct record* student[]);
void structfill (fstream & infile, struct  record*  student []);

int main ()
{

    system ("clear");

    fstream infile;
    char ifname [256];
    struct record * student;
    filename (ifname, student);

    return 0;
}
/*******************************************************************/
void filename (char ifname [],record* student [])
{
    fstream infile;
    cout << "Enter name of file to read from: ";
    cin.getline (ifname, 256);
    cout << endl;
    infile.open (ifname);
    if (!infile.is_open ())
    {
        cerr << "FILELOOP!: Unable to open input file " << ifname
        << endl;
        exit (1);
    }
    structfill (infile, student);
}
/*******************************************************************/
void structfill (fstream & infile, record* student [])
{

    char buffer [81];
    char buffername [81];
    char bufferfirst [81];
    int n=0;
    int x=0;
    int f=0;

    infile.getline (buffer,81);
    while (!infile.eof ())
    {
        x++;
        cout << "-----------------------" << x;
        if (strncasecmp (buffer, "<student>",9)==0)
        {
            n++;
            cout << "jess sucks" << n;
            student = new *record;
            infile.getline (buffername, 81);
            if (strncasecmp (buffername, "<first>",7)==0)
            {
                f++;
                infile.getline (bufferfirst, 81);
                strcpy (student->first, bufferfirst);
                cout << endl << "######  " << f;
            }
        }
        infile.getline (buffer, 81);
        cout << *student[n]->first;
        cout << "endendendend" << endl;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2014-02-13 14:35:15

传递时,数组衰减为指向第一个元素的指针:

代码语言:javascript
复制
void structfill (fstream & infile, record*  student);

此外,删除C-isms。不需要在这里指定"struct“。

票数 0
EN

Stack Overflow用户

发布于 2014-02-13 15:00:33

这是没有错误的代码,我编译成功了

代码语言:javascript
复制
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <strings.h>


using namespace std;

struct record
{
    char first [20];
    char mid   [1];
    char last  [20];
    int  ssn;
};

void filename (char ifname [], struct record* student);
void structfill (fstream & infile, struct  record*  student);

int main ()
{

    system ("clear");

    fstream infile;
    char ifname [256];
    struct record * student;
    filename (ifname, student);

    return 0;
}
/*******************************************************************/
void filename (char ifname [],record* student )
{
    fstream infile;
    cout << "Enter name of file to read from: ";
    cin.getline (ifname, 256);
    cout << endl;
    infile.open (ifname);
    if (!infile.is_open ())
    {
        cerr << "FILELOOP!: Unable to open input file " << ifname
        << endl;
        exit (1);
    }
    structfill (infile, student);
}
/*******************************************************************/
void structfill (fstream & infile, record* student )
{

    char buffer [81];
    char buffername [81];
    char bufferfirst [81];
    int n=0;
    int x=0;
    int f=0;

    infile.getline (buffer,81);
    while (!infile.eof ())
    {
        x++;
        cout << "-----------------------" << x;
        if (strncasecmp (buffer, "<student>",9)==0)
        {
            n++;
            cout << "jess sucks" << n;
            student = new struct record;
            infile.getline (buffername, 81);
            if (strncasecmp (buffername, "<first>",7)==0)
            {
                f++;
                infile.getline (bufferfirst, 81);
                strcpy (student->first, bufferfirst);
                cout << endl << "######  " << f;
            }
        }
        infile.getline (buffer, 81);
        cout << student[n].first;
        cout << "endendendend" << endl;
    }
}

  1. object.You运算符返回指针不应使用结构record.
  2. student能够保存一个结构记录数组。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21746795

复制
相关文章

相似问题

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