-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 函数,而且我可以在我的 main.js 文件中执行此操作。这是我在这里的第一个问题,所以我希望我的格式正确。这也只是为了在夏天玩耍。不是功课什么的。

4

2 回答 2

2

正如您所声称的,您的第二版代码没有做同样的事情。它使用类型的变量而ifstream*不是类型的变量,ifstream并且您使用<<它来读取它 not >>。如果你做了同样的事情,它会起作用,但是你改变了它来做一些不同的事情并且坏了。

错误并没有eofis not a class type of fstream,再仔细阅读一遍:

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

它说phoneBook是非类类型ifstream*(即指针类型),并且您正在尝试使用它的成员,这是不言自明的,因为指针没有成员。

只需正确阅读错误,它就会告诉您出了什么问题。仔细阅读你的代码,你会发现第二个版本并不等同于第一个。

此外,正如克里斯的评论所说,要这样做:

#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 回答
0

phoneBook 是一个指针,使用 -> 而不是 . ifstream 表示“输入文件流”,因此它只能从文件接收输入到您的应用程序

您必须使用“ofstream”或它们的父级“fstream” http://www.cplusplus.com/reference/fstream/fstream/

于 2013-06-15T16:51:47.140 回答