包含fstream.h标头的错误

时间:2012-04-07 06:39:10

标签: c++

我正在读一本2001年出版的“C ++中的数据结构和算法”一书,我认为c ++编译器应该从那时起改变很多,因为我发现书中的代码无法编译。 / p>

#include <fstream.h>
#include <string.h>

所以我谷歌的答案,并将代码更改为

#include <fstream>
#include <cstring>
using namespace std;

但是当我尝试编译代码时,我得到了一些我从未见过的错误:

oo@oo:~/raf$ g++ database.cpp personal.cpp student.cpp useDatabase.cpp -o useDatabase
In file included from /usr/include/c++/4.6/ios:45:0,
                 from /usr/include/c++/4.6/istream:40,
                 from /usr/include/c++/4.6/fstream:40,
                 from personal.h:4,
                 from student.h:1,
                 from student.cpp:1:
/usr/include/c++/4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.6/istream:41:0,
                 from /usr/include/c++/4.6/fstream:40,
                 from personal.h:4,
                 from student.h:1,
                 from student.cpp:1:
/usr/include/c++/4.6/ostream: In copy constructor ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’:
/usr/include/c++/4.6/ostream:57:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here 
In file included from student.cpp:1:0:
student.h: In function ‘std::ostream& operator<<(std::ostream&, Student&)’:
student.h:17:39: note: synthesized method ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ first required here 
student.h:15:18: error:   initializing argument 1 of ‘std::ostream& Student::writeLegibly(std::ostream)’
student.cpp: At global scope:
student.cpp:24:10: error: prototype for ‘std::ostream& Student::writeLegibly(std::ostream&)’ does not match any in class ‘Student’
student.h:15:18: error: candidate is: std::ostream& Student::writeLegibly(std::ostream)
student.cpp: In member function ‘std::istream& Student::readFromConsole(std::istream&)’:
student.cpp:34:5: error: ‘cout’ was not declared in this scope
In file included from /usr/include/c++/4.6/ios:45:0,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from useDatabase.cpp:1:
/usr/include/c++/4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.6/iostream:40:0,
                 from useDatabase.cpp:1:
/usr/include/c++/4.6/ostream: In copy constructor ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’:
/usr/include/c++/4.6/ostream:57:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here 
In file included from useDatabase.cpp:2:0:
student.h: In function ‘std::ostream& operator<<(std::ostream&, Student&)’:
student.h:17:39: note: synthesized method ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ first required here 
student.h:15:18: error:   initializing argument 1 of ‘std::ostream& Student::writeLegibly(std::ostream)’
oo@oo:~/raf$ 

我花了很多时间给谷歌寻求答案,但却犯了更多错误。也许我应该注册一个Github帐户并在那里上传我的代码。

database.cpp

#include "database.h"

template<class T> Database<T>::Database() {
    cout << "File name: ";
    cin >> fName;
}

template<class T> void Database<T>::add(T& d){
    database.open(fName, ios::in|ios::out|ios::binary);
    database.seekp(0, ios::end);
    d.writeToFile(database);
    database.close();
}

template<class T> void Database<T>::modify(const T& d){
    T tmp;
    database.open(fName, ios::in|ios::out|ios::binary);
    while(!database.eof()){
        tmp.readFromFile(database);
        if (tmp == d){
            cin >> tmp;
            database.seekp(-d.size(), ios::cur);
            tmp.writeToFile(database);
            database.close();
            return;
        }
    }
    database.close();
    cout << "The record to be modified is not in the database\n";
}

template<class T> bool Database<T>::find(const T& d){
    T tmp;
    database.open(fName, ios::in|ios::binary);
    while(!database.eof()){
        tmp.readFromFile(database);
        if (tmp == d){
            database.close();
            return true;
        }
    }
    database.close();
    return false;
}

template<class T> ostream& Database<T>::print(ostream& out){
    T tmp;
    database.open(fName, ios::in|ios::binary);
    while(1){
        tmp.readFromFile(database);
        if (database.eof())
            break;
        out << tmp << endl;
    }
    database.close();
    return out;
}

template<class T> void Database<T>::run() {
    char option[5];
    T rec;
    cout << "1.Add 2.Find 3.Modify a record 4.Exit\n";
    cout << "Enter an option: ";
    cin.getline(option, 4);
    while (cin.getline(option, 4)){
        if (*option == '1'){
            cin >> rec;
            add(rec);
        }
        else if (*option == '2'){
            rec.readKey();
            cout << "The record is ";
            if (find(rec) == false)
                cout << "not ";
            cout << "in the database\n";
        }
        else if (*option == '3'){
            rec.readKey();
            modify(rec);
        }
        else if (*option != '4'){
            cout << "Wrong option\n";
        }
        else return;
        cout << *this;
        cout << "Enter an option";
    }
}

database.h

#ifndef DATABASE
#define DATABASE
#include <fstream>
#include <iostream>
using namespace std;

template<class T> class Database{
    public:
        Database();
        void run();
    private:
        fstream database;
        char fName[20];
        ostream& print(ostream&);
        void add(T&);
        bool find(const T&);
        void modify(const T&);
        friend ostream& operator<<(ostream& out, Database& db) {
            return db.print(out);
        }
};
#endif

personal.cpp

#include "personal.h"
#include <iostream>

Personal::Personal() : nameLen(10), cityLen(10) {
    name = new char[nameLen + 1];
    city = new char[cityLen + 1];
}

Personal::Personal(char *ssn, char *n, char *c, int y, long s) : nameLen(10), cityLen(10) {
    name = new char[nameLen + 1];
    city = new char[cityLen + 1];
    strcpy(SSN, ssn);
    strcpy(name, n);
    strcpy(city, c);
    year = y;
    salary = s;
}

void Personal::writeToFile(fstream& out) const {
    out.write(SSN, 9);
    out.write(name, nameLen);
    out.write(city, cityLen);
    out.write(reinterpret_cast<const char*>(&year), sizeof(int));
    out.write(reinterpret_cast<const char*>(&salary), sizeof(int));
}

void Personal::readFromFile(fstream& in) {
    in.read(SSN, 9);
    in.read(name, nameLen);
    in.read(city, cityLen);
    in.read(reinterpret_cast<char *>(&year), sizeof(int));
    in.read(reinterpret_cast<char *>(&salary), sizeof(int));
}

void Personal::readKey() {
    char s[80];
    cout << "Enter SSN: ";
    cin.getline(s, 80);
    strncpy(SSN, s, 9);
}

ostream& Personal::writeLegibly(ostream& out){
    SSN[9] = name[nameLen] = city[cityLen] = '\0';
    out << "SSN = " << SSN << ", name = " << name
        << ", city = " << city << ", year = " << year
        << ", salary = " << salary;
    return out;
}

istream& Personal::readFromConsole(istream& in){
    char s[80];
    cout << "SSN: ";
    in.getline(s, 80);
    strncpy(SSN, s, 9);
    cout << "Name: ";
    in.getline(s, 80);
    strncpy(name, s, nameLen);
    cout << "City: ";
    in.getline(s, 80);
    strncpy(city, s, cityLen);
    cout << "Birthyear: ";
    in >> year;
    cout << "Salary: ";
    in >> salary;
    in.getline(s, 80); //get '\n'
    return in;
}

personal.h

#ifndef PERSONAL
#define PERSONAL

#include <fstream>
#include <cstring>
using namespace std;

class Personal {
    public:
        Personal();
        Personal(char*, char*, char*, int, long);
        void writeToFile(fstream&) const;
        void readFromFile(fstream&);
        void readKey();
        int size() const {
            return 9 + nameLen + cityLen + sizeof(year) + sizeof(salary);
        }
        bool operator==(const Personal& pr) const{
            return strcmp(pr.SSN, SSN) == 0;
        }

    protected:
        const int nameLen, cityLen;
        char SSN[10], *name, *city;
        int year;
        long salary;
        ostream& writeLegibly(ostream&);
        friend ostream& operator<<(ostream& out, Personal& pr){
            return pr.writeLegibly(out);
        }
        istream& readFromConsole(istream&);
        friend istream& operator>>(istream& in, Personal& pr){
            return pr.readFromConsole(in);
        }
};

#endif

student.cpp

#include "student.h"

Student::Student() : majorLen(10) {
    Personal();
    major = new char[majorLen + 1];
}

Student::Student(char *ssn, char *n, char *c, int y, long s, char *m): majorLen(11){
    Personal(ssn, n, c, y, s);
    major = new char[majorLen + 1];
    strcpy(major, m);
}

void Student::writeToFile(fstream& out) const {
    Personal::writeToFile(out);
    out.write(major, majorLen);
}

void Student::readFromFile(fstream& in) {
    Personal::readFromFile(in);
    in.read(major, majorLen);
}

ostream& Student::writeLegibly(ostream &out){
    Personal::writeLegibly(out);
    major[majorLen] = '\0';
    out << ", major = " << major;
    return out;
}

istream& Student::readFromConsole(istream& in){
    Personal::readFromConsole(in);
    char s[80];
    cout << "Major: ";
    in.getline(s, 80);
    strncpy(major, s, 9);
    return in;
}

student.h

#include "personal.h"

class Student : public Personal {
    public:
        Student();
        Student(char*, char*, char*, int, long, char*);
        void writeToFile(fstream&) const;
        void readFromFile(fstream&);
        int size() const{
            return Personal::size() + majorLen;
        }
    protected:
        char *major;
        const int majorLen;
        ostream& writeLegibly(ostream);
        friend ostream& operator<<(ostream& out, Student& sr){
            return sr.writeLegibly(out);
        }
        istream& readFromConsole(istream&);
        friend istream& operator>>(istream& in, Student& sr){
            return sr.readFromConsole(in);
        }
};

useDatabase.cpp

#include <iostream>
#include "student.h"
#include "personal.h"
#include "database.h"

int main(){
    Database<Personal> db;
    db.run();
}

4 个答案:

答案 0 :(得分:4)

这是一个很大的 - 可能太大 - 在SO问题中要分析的文件集。您需要学习一些减少问题大小的方法,以便呈现给SO(或技术支持)。

C或C ++的第一步是确保您创建的标头干净利落地编译。如果标题不干净,您将无法编译使用标题的代码,因此必须首先排序标题。

为了帮助我,我有一个我称之为chkhdr的脚本:

#!/bin/sh
# Check whether headers compile

tmp=chkhdr-$$
trap 'rm -f $tmp.?; exit 1' 0 1 2 3  13 15

cat > $tmp.c <<EOF
#include HEADER /* Check self-containment */
#include HEADER /* Check idempotence */
int main(void) { return 0; }
EOF

options=
for file in "$@"
do
    case "$file" in
    (-*) options="$options $file";;
    (*)  echo "$file"
         ${CC:-gcc} $options -DHEADER="\"$file\"" -c $tmp.c
         ;;
    esac
done

rm -f $tmp.?
trap 0

我用它来检查标题是自包含的还是幂等的。可以包含一个自包含的头文件,而不包含任何其他头文件,并编译它。这意味着它可以在任何需要其服务的地方使用而不会更加轻松。可以多次包含幂等标头而不会造成麻烦。 (我主要使用C语言,因此默认编译器是GCC而不是G ++。但我可以在环境中设置CC=g++以切换到C ++工作。)

您的student.h标题不是幂等的;我立即在顶部和底部添加了标准节:

#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED

...original contents of student.h...

#endif /* STUDENT_H_INCLUDED */

guard macro name的详细选择取决于您;这是我现在使用的命名方案,但是使用像标题草稿的MD5校验和这样的东西来给你一个准随机的保护宏有一些优点。

仅编译student.h标题的输出是:

In file included from chkhdr-8120.c:1:
/usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.2.1/iosfwd:55: error: within this context
/usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’:
/usr/include/c++/4.2.1/iosfwd:64: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
student.h: In function ‘std::ostream& operator<<(std::ostream&, Student&)’:
student.h:20: note: synthesized method ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’ first required here 
student.h:20: error:   initializing argument 1 of ‘std::ostream& Student::writeLegibly(std::ostream)’

错误消息的最后一行牢牢指出了这个问题;其他消息有点切向,并引导至关重要的消息。 (通常情况下,特别是在C中,第一个错误是最重要的。对我来说,最后一行是关键错误令人惊讶。)将student.h的第20行更改为:

    ostream& writeLegibly(ostream&);

解决了这个问题,并且干净地编译了student.h标头。其他标题也很干净。然后编译源文件是一件简单的事情。只有student.cpp有问题:

student.cpp: In member function ‘std::istream& Student::readFromConsole(std::istream&)’:
student.cpp:34: error: ‘cout’ was not declared in this scope

这听起来像'应该使用std::cout代替',但添加导致:

student.cpp: In member function ‘std::istream& Student::readFromConsole(std::istream&)’:
student.cpp:34: error: ‘cout’ is not a member of ‘std’

通过加入#include <iostream>来解决这个问题。然后代码清晰地编译在:

g++ -c *.cpp

我无法联系;我遇到了对数据库代码的未定义引用,但这并不奇怪。


摘要

修复的细节并不是非常重要。重要的是技术。这里的关键技术是:

  1. 确保标题可以自行完成(chkhdr)。
  2. 一次处理一个文件。
  3. 问题最小化。
  4. 我们不应该处理所有代码;你应该能够更好地隔离问题。在您向其他人报告软件问题的任何情况下,这都是一项重要技能。消除无关紧要的事物,并减少到必需品。

答案 1 :(得分:3)

错误报告

student.cpp:34:5: error: ‘cout’ was not declared in this scope

我认为你需要

#include <iostream>

答案 2 :(得分:3)

Student更改

ostream& writeLegibly(ostream);

ostream& writeLegibly(ostream&);

这应该解决很多问题。错过了&符号,你试图复制流。

答案 3 :(得分:3)

虽然编译器已经改变,但C ++和C是向后兼容的。语言的优势之一。无论如何,我已将您的代码复制到VS2010中,因为我更容易用它来分析问题。

  1. 把#include&lt; iostream&gt;在你的personal.h中。它由personal.cpp使用,你有一个cout。

  2. 的student.h中的签名

    ostream& writeLegibly(ostream);
    

    错了,应该是:

    ostream& writeLegibly(ostream&);
    
  3. 我已将database.cpp中的实现代码移到database.h中。 #endif上方简单的剪切和粘贴。由于模板性质,编译器不喜欢它在单独的实现cpp。

  4. 希望有所帮助。