-1

このメソッドが機能することをテストするためだけに、メインの配列を印刷しました。しかし、別のクラスで同じことをしようとすると、エラーが発生します。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include "struct.h"
#include "phoneBook.h"

using namespace std;

int main()
{
    struct contacts info[256];
    phoneTools manipulate;
    fstream phoneBook ("phoneBook.txt");

    if(!phoneBook.is_open())
    {
        cout<< "The file can not be opened";
        cout<< endl;
        cout<< "Terminating!";

        exit(1);
    }

    else
    {
        //populate array of structs
        int i = 0;
        while(!phoneBook.eof())
        {
            phoneBook>> info[i].firstName;
            phoneBook>> info[i].surName;
            phoneBook>> info[i].phoneNumber;
            phoneBook>> info[i].email;
            phoneBook>> info[i].relationship;
            i++;
        }
    }

    manipulate.addContact(info, phoneBook);
}

これは、エラーが発生する他のクラスです。エラーメッセージとともに、ヘッダーを以下に示します。また、使用していないライブラリをインポートしている可能性があることも認識していますが、そうします。すべての関数の作成が完了していません。

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include "struct.h"

using namespace std;

void writeStructToDatabase(struct contacts writeContact[], fstream *phoneBook)
{
    //We are passed new array of contacts
    //write this new array to the phonebook file
    int i = 0;
    while(!phoneBook->eof())
    {
        phoneBook<< writeContact[i].firstName;
        phoneBook<< writeContact[i].surName;
        phoneBook<< writeContact[i].phoneNumber;
        phoneBook<< writeContact[i].email;
        phoneBook<< writeContact[i].relationship;
        i++;
    }
}

void showPhoneBook(struct contacts print[])
{
    int num = (sizeof(print) / sizeof(*print));

    cout<< endl;
    cout<< endl;
    for(int i = 0; i < num; i++)
    {
        cout<< print[i].firstName;
        cout<< endl;

        cout<< print[i].surName;
        cout<< endl;

        cout<< print[i].phoneNumber;
        cout<< endl;

        cout<< print[i].email;
        cout<< endl;

        cout<< print[i].relationship;
        cout<< endl;
    }
    cout<< endl;
}

void addContact(struct contacts newContact[], fstream *phoneBook)
{
    int num = (sizeof(newContact) / sizeof(*newContact));

    cout<< "First Name: ";
    cin>> newContact[num].firstName;
    cout<< endl;

    cout<< "Last Name: ";
    cin>> newContact[num].surName;
    cout<< endl;

    cout<< "Phone Number: ";
    cin>> newContact[num].phoneNumber;
    cout<< endl;

    cout<< "Email: ";
    cin>> newContact[num].email;
    cout<< endl;

    cout<< "Relationship: ";
    cin>> newContact[num].relationship;
    cout<< endl;

    writeStructToDatabase(newContact, phoneBook);
}

void deleteContact(struct contacts delContact[], fstream *phoneBook)
{
    string first;
    string last;

    cout<< "First Name: ";
    cin>> first;
    cout<< endl;

    cout<< "Last Name: ";
    cin>> last;
    cout<< endl;

    int num = (sizeof(delContact)/sizeof(*delContact));

    int exit = 0;
    int i = 0;
    while(exit == 0)
    {
        if((strcmp(delContact[i].firstName, first) == 0) &&
           (strcmp(delContact[i].surName, last) == 0))
        {
            for(int j = i; j < num; j++)
            {
                delContact[j] = delContact[j+1];
            }

            exit = 1;
        }

        i++;
    }

writeStructToDatabase(delContact, phoneBook);
}

ここに私の構造体のヘッダーがあります

#include <string.h>

using namespace std;

#ifndef STRUCT_H
#define STRUCT_H
struct contacts
{
    string firstName;
    string surName;
    string phoneNumber;
    string email;
    string relationship;
};
#endif

これが私の関数クラスのヘッダーです

#include <fstream>

using namespace std;

#ifndef PHONE_BOOK_H
#define PHONE_BOOK_H

//enter methods below this line
//ex. extern void getRandInteger(int max);

class phoneTools
{
    public:
    void addContact(contacts newContact[], fstream *phoneBook);
    void showPhoneBook(contacts print[]);
    void deleteContact(contacts delContact[], fstream *phoneBook);

    private:
    void writeStructToDatabase(contacts writeContact[], fstream *phoneBook);
};

//enter methods above this line

#endif /* __PHONE_BOOK_H */

今ここに私が得ているエラーがあります

phoneBook.cpp: In function ‘void writeStructToDatabase(contacts*, std::fstream*)’:
phoneBook.cpp:16: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::firstName’
phoneBook.cpp:17: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::surName’
phoneBook.cpp:18: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::phoneNumber’
phoneBook.cpp:19: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::email’
phoneBook.cpp:20: error: no match for ‘operator<<’ in ‘phoneBook << writeContact[i].contacts::relationship’
phoneBook.cpp: In function ‘void deleteContact(contacts*, std::fstream*)’:
phoneBook.cpp:97: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’
phoneBook.cpp:98: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’
phoneBookDriver.cpp: In function ‘int main()’:
phoneBookDriver.cpp:40: error: invalid conversion from ‘void*’ to ‘std::fstream*’
phoneBookDriver.cpp:40: error:   initializing argument 2 of ‘void phoneTools::addContact(contacts*, std::fstream*)’
i686-apple-darwin11-llvm-g++-4.2: phoneBook.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: phoneBookDriver.o: No such file or directory
i686-apple-darwin11-llvm-g++-4.2: no input files

ライブラリをインポートしたのに fstream 関数を使用できない理由を突き止めようとして、最終日を検索しました。さらに、メインでこれを行うことができます。これはここでの最初の質問なので、正しくフォーマットしたことを願っています。また、これは夏の楽しみのためだけです。宿題でも何でもない。

4

2 に答える 2

2

あなたが主張するように、コードの2番目のバージョンは同じことをしていません。ifstream*タイプの変数ではなくタイプの変数を使用しており、それからの読み取りにifstream使用していません。もしあなたが同じことをしたなら、それはうまくいったでしょうが、あなたはそれを何か違うことをするように変更し、壊れました。<<>>

このエラーは、 が のクラス タイプではないことを示しているわけではありません。もう一度よく読んでください。eoffstream

phoneBook.cpp:14: error: request for member ‘eof’ in ‘phoneBook’,
    which is of non-class type ‘std::ifstream*’

phoneBook非クラス型ifstream*(つまり、ポインター型) であり、そのメンバーを使用しようとしていると表示されます。ポインターにはメンバーがないため、これは一目瞭然です。

エラーを正しく読むだけで、何が問題なのかがわかります。コードをもっと注意深く読むと、2 番目のバージョンが最初のバージョンと同等ではないことがわかります。

また、クリスからのコメントが言うように、これをしないでください:

#ifndef __PHONE_BOOK_H

二重下線 (または下線と大文字) で始まる名前は予約されており、使用してはなりません。

そして、これをしないでください:

while(!phoneBook.eof())

ループはデータの読み取りに失敗したに終了しますが、これはあなたが望むものではありません。これはあなたが望むものです:

while(*phoneBook >> writeContact[i].firstName >> writeContact[i].surName
      >> writeContact[i].phoneNumber>> writeContact[i].email
      >> writeContact[i].relationship)

>>( notを使用することに注意してください<<)

しかし、読みやすく定義しやすくして、簡単にoperator>>(std::istream&, contacts&)できるようにしてみませんか。

while (*phoneBook >> writeContact[i])

また、ポインターではなく参照によって渡すこともできます。そうifstreamすれば、ポインターを正しく使用しないという問題を回避できます。

于 2013-06-15T16:58:31.680 に答える