1

我正在阅读一本 2001 年出版的《Data Structures and Algorithms in C++》一书,我认为从那时起,c++ 编译器应该发生了很大的变化,因为我发现书中的代码无法编译。

#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 帐户并在那里上传我的代码。

数据库.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";
    }
}

数据库.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

个人.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;
}

个人.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

学生.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;
}

学生.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);
        }
};

使用数据库.cpp

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

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

4 回答 4

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 */

守卫宏名称的详细选择由您决定;这就是我这些天使用的命名方案,但是使用诸如标头草稿的 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 语言中,第一个错误是最重要的。令我惊讶的是,最后一行是关键。)将第 20 行更改student.h为:

    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. 问题最小化。

我们不应该处理所有的代码;您应该能够更好地隔离问题。在您向他人报告软件问题的任何情况下,这都是一项重要的技能。消除多余的东西,并减少到最基本的东西。

于 2012-04-07T07:42:50.430 回答
3

错误报告

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

我想你需要

#include <iostream>
于 2012-04-07T06:52:16.853 回答
3

Student变化中

ostream& writeLegibly(ostream);

ostream& writeLegibly(ostream&);

这应该可以解决很多问题。通过缺少&符号,您试图复制流。

于 2012-04-07T07:11:43.857 回答
3

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

  1. 将#include <iostream> 放在您的personal.h 中。它由您有 cout 的个人.cpp 使用。

  2. student.h 中的签名

    ostream& writeLegibly(ostream);
    

    错了,应该是:

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

希望有帮助。

于 2012-04-07T07:33:46.340 回答